Last active
July 18, 2018 00:40
-
-
Save yongs2/80ac9b55b761bd7748de1ec033fee61c to your computer and use it in GitHub Desktop.
my_strtok_r 함수, delimitor 사이의 공백도 리턴함
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
char *my_strtok_r(char *text, const char *delimitor, char **save_this) | |
{ | |
int nLen = 0; | |
if (delimitor == NULL) { | |
return NULL; | |
} | |
nLen = strlen(delimitor); | |
if (nLen <= 0) { | |
return NULL; | |
} | |
if(save_this == NULL) | |
{ | |
return NULL; | |
} | |
if (text != NULL) | |
{ | |
/* New text. */ | |
int i = 0; | |
while(text[i] != '\0') | |
{ | |
if(strncmp(text+i, delimitor, nLen) == 0) | |
{ | |
text[i] = '\0'; | |
*save_this = &text[i + nLen]; | |
//printf("[%s:%s:%d] i=%d, return save_this=0x%p\n", __FILE__, __func__, __LINE__, i, *save_this); | |
return text; | |
} | |
i++; | |
} | |
} | |
else if ((save_this != NULL) && (*save_this != NULL)) | |
{ | |
/* Old text. */ | |
int i = 0; | |
char *start = *save_this; | |
while((*save_this)[i] != '\0') | |
{ | |
//printf("[%s:%s:%d] i=%d, *save_this[%d]=[%c]\n", __FILE__, __func__, __LINE__, i, i, (*save_this)[i]); | |
if(strncmp(*save_this+i, delimitor, nLen) == 0) | |
{ | |
(*save_this)[i] = '\0'; | |
*save_this = &((*save_this)[i + nLen]); | |
//printf("[%s:%s:%d] i=%d, return start=0x%p\n", __FILE__, __func__, __LINE__, i, start); | |
return start; | |
} | |
i++; | |
} | |
*save_this = NULL; | |
save_this = NULL; | |
return start; | |
} | |
return NULL; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment