Created
December 21, 2017 05:21
-
-
Save sushlala/3172c37f2947e96977b6a0682ec55a60 to your computer and use it in GitHub Desktop.
An implementation of strtok
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stddef.h> | |
#include <stdbool.h> | |
#include <stdio.h> | |
bool is_delim(char c, char *delim) | |
{ | |
while(*delim != '\0') | |
{ | |
if(c == *delim) | |
return true; | |
delim++; | |
} | |
return false; | |
} | |
char *strtok1(char *s, char *delim) | |
{ | |
static char *p; // start of the next search | |
if(!s) | |
{ | |
s = p; | |
} | |
if(!s) | |
{ | |
// user is bad user | |
return NULL; | |
} | |
// handle beginning of the string containing delims | |
while(1) | |
{ | |
if(is_delim(*s, delim)) | |
{ | |
s++; | |
continue; | |
} | |
if(*s == '\0') | |
{ | |
return NULL; // we've reached the end of the string | |
} | |
// now, we've hit a regular character. Let's exit the | |
// loop, and we'd need to give the caller a string | |
// that starts here. | |
break; | |
} | |
char *ret = s; | |
while(1) | |
{ | |
if(*s == '\0') | |
{ | |
p = s; // next exec will return NULL | |
return ret; | |
} | |
if(is_delim(*s, delim)) | |
{ | |
*s = '\0'; | |
p = s + 1; | |
return ret; | |
} | |
s++; | |
} | |
} | |
int main() | |
{ | |
// char s[] = " 'hi;;;there;';how'are you'''"; | |
char s[] = "'''hi there"; | |
char *delim = "; '"; | |
char *p = strtok1(s, delim); | |
while(p) | |
{ | |
printf("%s\n", p); | |
p = strtok1(NULL, delim); | |
} | |
} |
What is the license for this code?
license driver
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What is the license for this code?