Created
December 3, 2012 22:53
-
-
Save keiya/4198873 to your computer and use it in GitHub Desktop.
split()っぽいC実装
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
| #include <stdio.h> | |
| #include "kstring.h" | |
| int main () { | |
| char text[] = "aaaasdf*#qwer*#oioio*#"; | |
| char delim[] = "*#"; | |
| t_split *result = ksplit(text,delim); | |
| int i; | |
| for (i=0; i < result->count; ++i) { | |
| printf("%d:%s\n",i,result->splited_ary[i]); | |
| } | |
| return 0; | |
| } |
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
| #include <stdio.h> | |
| #include <string.h> | |
| #include <stdlib.h> | |
| #include "kstring.h" | |
| /* | |
| * kstring by Keiya Chinen | |
| * 1.0a | |
| * */ | |
| t_split* ksplit(char *text,char *delim) { | |
| char **splited_ary; | |
| t_split *result; | |
| char *chop_begin,*chop_end; | |
| char *offset = text; | |
| splited_ary = (char **)malloc(0); | |
| int i = 0; | |
| while(1) { | |
| chop_begin = strstr(offset,delim); | |
| if (chop_begin == NULL) break; | |
| chop_end = chop_begin + strlen(delim); | |
| int chop_length = chop_begin - offset; | |
| char *part = malloc(chop_length); | |
| strncpy(part,offset,chop_length); | |
| splited_ary[i] = (char *)malloc(strlen(part)); | |
| strcpy(splited_ary[i],part); | |
| offset = chop_end; | |
| ++i; | |
| } | |
| result = (t_split *)malloc(sizeof(t_split *)); | |
| result->splited_ary = splited_ary; | |
| result->count = i; | |
| return result; | |
| } |
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
| typedef struct { | |
| char **splited_ary; | |
| int count; | |
| } t_split; | |
| t_split* ksplit(char *text,char *delim); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment