Skip to content

Instantly share code, notes, and snippets.

@sushlala
Created December 21, 2017 05:21
Show Gist options
  • Save sushlala/3172c37f2947e96977b6a0682ec55a60 to your computer and use it in GitHub Desktop.
Save sushlala/3172c37f2947e96977b6a0682ec55a60 to your computer and use it in GitHub Desktop.
An implementation of strtok
#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);
}
}
@ManuCiao10
Copy link

ManuCiao10 commented Oct 23, 2022

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