Skip to content

Instantly share code, notes, and snippets.

@owlfox
Last active August 29, 2015 14:28
Show Gist options
  • Save owlfox/3ead77b12a30bc7ba03b to your computer and use it in GitHub Desktop.
Save owlfox/3ead77b12a30bc7ba03b to your computer and use it in GitHub Desktop.
Summer 2015 part2 Day3 quiz2, about reentrancy, should add some mechanism to prevent out-of-boundary access...?
#include <stdio.h>
#include <string.h>
//wrapper function of strtok
/*strIn, can be NULL, then the result output will use lastState to manipulate */
char* strtok_tj_r(char* restrict strIn, const char* restrict tokens, char** restrict lastState)
{
char* result;
if(strIn!=NULL){
result = strtok(strIn,tokens);//the string has been modified by strok with token char replace by eof
*lastState = result+1+strlen(result);//save the next position in the state string, +1 for the token(now eof)
}
else{
if((*lastState)==NULL){
return NULL;
}
result = strtok(*lastState, tokens);
*lastState = result+1+strlen(result);
if(strlen(*lastState)==0){
*lastState=NULL;
}
}
return result;
}
int main(void)
{
char test[80]={0};//need to initialize these with 0... otherwise get char '?' behind eof ... cause unexpected behavior
char blah[80]={0};
char *sep = "\\/:;=-";
char *word, *phrase, *brkt, *brkb;
strcpy(test, "This;is.a:test:of=the/string\\tokenizer-function.");
for (word = strtok_tj_r(test, sep, &brkt);
word;
word = strtok_tj_r(NULL, sep, &brkt))
{
strcpy(blah, "blah:blat:blab:blag");
for (phrase = strtok_tj_r(blah, sep, &brkb);
phrase;
phrase = strtok_tj_r(NULL, sep, &brkb))
{
printf("So far we're at %s:%s, brkt:%s, brkb:%s\n", word, phrase, brkt, brkb);
}
}
return 0;
}
@owlfox
Copy link
Author

owlfox commented Aug 23, 2015

the output:
So far we're at This:blah, brkt:is.a:test:of=the/string\tokenizer-function., brkb:blat:blab:blag
So far we're at This:blat, brkt:is.a:test:of=the/string\tokenizer-function., brkb:blab:blag
So far we're at This:blab, brkt:is.a:test:of=the/string\tokenizer-function., brkb:blag
So far we're at This:blag, brkt:is.a:test:of=the/string\tokenizer-function., brkb:(null)
So far we're at is.a:blah, brkt:test:of=the/string\tokenizer-function., brkb:blat:blab:blag
So far we're at is.a:blat, brkt:test:of=the/string\tokenizer-function., brkb:blab:blag
So far we're at is.a:blab, brkt:test:of=the/string\tokenizer-function., brkb:blag
So far we're at is.a:blag, brkt:test:of=the/string\tokenizer-function., brkb:(null)
So far we're at test:blah, brkt:of=the/string\tokenizer-function., brkb:blat:blab:blag
So far we're at test:blat, brkt:of=the/string\tokenizer-function., brkb:blab:blag
So far we're at test:blab, brkt:of=the/string\tokenizer-function., brkb:blag
So far we're at test:blag, brkt:of=the/string\tokenizer-function., brkb:(null)
So far we're at of:blah, brkt:the/string\tokenizer-function., brkb:blat:blab:blag
So far we're at of:blat, brkt:the/string\tokenizer-function., brkb:blab:blag
So far we're at of:blab, brkt:the/string\tokenizer-function., brkb:blag
So far we're at of:blag, brkt:the/string\tokenizer-function., brkb:(null)
So far we're at the:blah, brkt:string\tokenizer-function., brkb:blat:blab:blag
So far we're at the:blat, brkt:string\tokenizer-function., brkb:blab:blag
So far we're at the:blab, brkt:string\tokenizer-function., brkb:blag
So far we're at the:blag, brkt:string\tokenizer-function., brkb:(null)
So far we're at string:blah, brkt:tokenizer-function., brkb:blat:blab:blag
So far we're at string:blat, brkt:tokenizer-function., brkb:blab:blag
So far we're at string:blab, brkt:tokenizer-function., brkb:blag
So far we're at string:blag, brkt:tokenizer-function., brkb:(null)
So far we're at tokenizer:blah, brkt:function., brkb:blat:blab:blag
So far we're at tokenizer:blat, brkt:function., brkb:blab:blag
So far we're at tokenizer:blab, brkt:function., brkb:blag
So far we're at tokenizer:blag, brkt:function., brkb:(null)
So far we're at function.:blah, brkt:(null), brkb:blat:blab:blag
So far we're at function.:blat, brkt:(null), brkb:blab:blag
So far we're at function.:blab, brkt:(null), brkb:blag
So far we're at function.:blag, brkt:(null), brkb:(null)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment