Skip to content

Instantly share code, notes, and snippets.

@keiya
Created December 3, 2012 22:53
Show Gist options
  • Select an option

  • Save keiya/4198873 to your computer and use it in GitHub Desktop.

Select an option

Save keiya/4198873 to your computer and use it in GitHub Desktop.
split()っぽいC実装
#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;
}
#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;
}
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