Skip to content

Instantly share code, notes, and snippets.

@MXP2095onetechguy
Created May 13, 2022 08:24
Show Gist options
  • Select an option

  • Save MXP2095onetechguy/a3155cb63d679aa53d1a749ed42ad142 to your computer and use it in GitHub Desktop.

Select an option

Save MXP2095onetechguy/a3155cb63d679aa53d1a749ed42ad142 to your computer and use it in GitHub Desktop.
A thread safe strtok function compatible with the POSIX strtok_r
#ifdef __cplusplus
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cstddef>
#else
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stddef.h>
#endif
char* mystrtokr(char* s, char* token, char** sp){
char* e;
if(s == NULL){
s = *sp;
}
if(*s == '\0'){
*sp = s;
return NULL;
}
s += strspn(s, token);
if(*s == '\0'){
*sp = s;
return NULL;
}
e = s + strcspn(s, token);
if(*e == '\0'){
*sp = e;
return s;
}
*e = '\0';
*sp = e + 1;
return s;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment