Created
May 13, 2022 08:24
-
-
Save MXP2095onetechguy/a3155cb63d679aa53d1a749ed42ad142 to your computer and use it in GitHub Desktop.
A thread safe strtok function compatible with the POSIX strtok_r
This file contains hidden or 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
| #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