Strtok source code profiling bit operations and space compression

Source: Internet
Author: User
Tags strtok

Address: http://blog.csdn.net/morewindows/article/details/8740315. please refer to the following link for help. Thank you.

Welcome to Weibo: http://weibo.com/MoreWindows

Strtok source code analysis

The strtok function can be used to separate strings. I recently read the source code of this function.

unsigned char map[32];/* Clear control map */for (count = 0; count < 32; count++)map[count] = 0;/* Set bits in delimiter table */do {map[*ctrl >> 3] |= (1 << (*ctrl & 7));} while (*ctrl++);

This code is very interesting. At first glance, it may not be clear why the unsigned char map [32]; array is used to save the delimiter. The following map [* Ctrl> 3] | = (1 <(* CTRL & 7); is more odd. I checked it online and didn't have any articles to explain it. So I wrote a blog to explain it.

This length of 32 array and the back of the left shift, the right shift operation looks confused, in fact, if you have read the "bit operation basic bit operation comprehensive summary" (http://blog.csdn.net/morewindows/article/details/7354571) "Bit operations and space compression" is not difficult to think of, here is actually a space compression technique for single-bit operations. Therefore, char data only ranges from 0 to 255. Therefore, a hash table is created to record which characters need to be split. If this is the case, it is marked as 1. Otherwise, it is marked as 0, when the string is separated, you can directly determine whether the position of the string is to be separated. For details, see the implementation of mystrtok.

// Strtok source code analysis bit operation and space compression // http://blog.csdn.net/morewindows/article/details/8740315//By morewindows (http://blog.csdn.net/MoreWindows) # include <stdio. h> // strtok source code analysis char * _ cdecl mystrtok (char * string, const char * Control) {unsigned char * STR; const unsigned char * CTRL = (const unsigned char *) control; static unsigned char * _ token = NULL; // note that the static type is used here, the actual strtok function uses TLS for thread safety // because the char type occupies a byte value range of 0 ~ 255 // Therefore, you can create a hash table such as bool flag [255] // to record which characters are delimiter characters // then, based on the complete summary of bit operations in BIT operations bitwise operations and space compression // using char map [32]; int count; // clear control mapfor (COUNT = 0; count <32; count ++) map [count] = 0; // set bits in delimiter tabledo {// map [* Ctrl> 3] | = (1 <(* CTRL & 7 )); // strtok original code map [* CTRL/8] |=( 1 <(* CTRL % 8 ));} While (* CTRL ++); // initialize STR // If string is null, set STR to the saved pointer // (I. E ., continue breaking tokens out of the string from the last strtok call) if (string! = NULL) STR = (unsigned char *) string; elsestr = (unsigned char *) _ token; // find beginning of token (skip over leading delimiters ). note that // There is no token IFF this loop sets STR to point to the terminal // null (* STR = '\ 0 ') // while (Map [* STR> 3] & (1 <(* STR & 7) & * Str) // strtok original Code while (Map [* str/8] & (1 <(* STR % 8) & * Str) STR ++; string = (char *) STR; // find the end of Token. if it is not the end of the string, // put a null there. for (; * STR; STR ++) {// If (Map [* STR> 3] & (1 <(* STR & 7 ))) // strtok original code if (Map [* str/8] & (1 <(* STR % 8) {* STR ++ = '\ 0 '; break ;}// update nextoken (or the corresponding field in the per-thread data structure_token = STR; // determine if a token has been found. if (string = (char *) Str) return NULL; elsereturn string;} in T Main () {printf ("strtok source code profiling bit operations and space compression \ n"); printf ("-http://blog.csdn.net/morewindows/article/details/8740315-\ n "); printf ("-by morewindows (http://blog.csdn.net/MoreWindows)-\ n"); // char sztext [] = "morewindows (by http://blog.csdn.net/MoreWindows )"; // char szfind [] = ""; char sztext [] = "AB, c... D (e) f (g) HJ "; char szfind [] = ",. () "; printf (" original string: % s \ n ", sztext); printf (" separated by: \ n "); char * ptok En = mystrtok (sztext, szfind); While (ptoken! = NULL) {printf ("% s \ n", ptoken); ptoken = mystrtok (null, szfind);} return 0 ;}

Running results (Images cannot be opened, please visit http://blog.csdn.net/morewindows/article/details/8740315)

 

Address: http://blog.csdn.net/morewindows/article/details/8740315. please refer to the following link for help. Thank you.

Welcome to Weibo: http://weibo.com/MoreWindows

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.