String splitting is a very important function. It takes a while to use it. So here we will first talk about the strtok processing method:
Strtok first filters out all the characters that belong to the delimiter string set, then, scan and replace the characters that match the delimiter string with the null Terminator '/0, in this way, you can directly use the function to return and directly read the first token (because it will end when it reads '/0', very good !!!!). Then, when getting the remaining tokens, pass null to strtok as the first parameter to get the next token.
If you use a lot of resources, or use the above description, you will be confused. How is the Code implemented? Why can I get the next token by passing a null value? The returned pointer value must have been moved to the next place (a position after the previous token). How can I know where to stop each scan? Because the program does not know the first token to be processed, I simply passed a null value.
It turns out that a static variable is used here. The operation on the static variable ensures the modification of the pointer (based on the record of the previous token). In addition, another bad thing about strtok is that it modifies the original string and replaces the corresponding position with '/0', which is why the first parameter of this function cannot be const char.
The following code is implemented by strtok:
Static char * Ts = NULL; <br/> char * strtok (char * s, const char * CT) {<br/> assert (s! = NULL & CT! = NULL); <br/> S = ts; // last position <br/> If (! S & * s = 0) <br/> return NULL; <br/> char * t; <br/> do {<br/> for (t = CT, TS = NULL; * t; t ++) {<br/> If (* t = * s) {<br/> If (* (TS = ++ s) break; <br/> return Ts = NULL; <br/>}< br/>}while (TS); <br/> for (TS = s; * ts; TS ++) <br/> for (t = CT; * t; t ++) <br/> If (* Ts = * t) {<br/> * ts ++ = '/0'; <br/> return s; <br/>}< br/> TS = NULL; <br/> return s; <br/>}
Pay attention to the above Code carefully. The following content is important:
Because the strtok function uses Global static variables, this function cannot be reentrant and is not thread-safe, therefore, problems may occur when you use the function multiple times within the same time.
This produces the reentrant version of the function: strrtok, an extra r meaning reentrant (reentrant), the advantages and disadvantages of these two functions and the analysis of the Instance effect, see: http://blog.chinaunix.net/u2/66402/showart_1168731.html, there is a detailed comparison.
How does strrtok implement its re-entry? It only has an extra parameter. The function prototype of strrtok is:
Char * strtok_r (char * s, const char * delim, char ** ptrptr );
It turns out that the subsequent ptrptr is used to pass a pointer reference. Therefore, before using this function, you need to define a pointer variable outside, this variable can replace the static variables in strtok. Therefore, different use procedures define different pointer variables to be passed in, thus achieving good reusability: maintain your own variables.
The following is a source code of strrtok for your reference:
Char * strtok_r (char * S1, const char * S2, char ** lasts) <br/>{< br/> char * ret; <br/> If (S1 = NULL) <br/> S1 = * Lasts; <br/> while (* S1 & strchr (S2, * S1 )) // remove the front separator <br/> + + S1; <br/> If (* S1 = '/0') <br/> return NULL; <br/> ret = S1; <br/> while (* S1 &&! Strchr (S2, * S1) <br/> + S1; <br/> If (* S1) <br/> * S1 ++ = '/0 '; <br/> * Lasts = S1; <br/> return ret; <br/>}
For the reentrant and thread-safe concepts of functions, see: http://liuaigui.blog.sohu.com/86494742.html