13 l = vsnprintf(s->
s + s->
l, s->
m - s->
l, fmt, args);
15 if (l + 1 > s->
m - s->
l) {
18 s->
s = (
char*)realloc(s->
s, s->
m);
20 l = vsnprintf(s->
s + s->
l, s->
m - s->
l, fmt, args);
39 const char *p, *start;
41 if (str == 0 && (aux->
tab[0]&1))
return 0;
45 aux->
tab[0] = aux->
tab[1] = aux->
tab[2] = aux->
tab[3] = 0;
46 for (p = sep; *p; ++p) aux->
tab[*p>>6] |= 1ull<<(*p&0x3f);
47 }
else aux->
sep = sep[0];
50 else if (str) aux->
p = str - 1, aux->
finished = 0;
52 for (p = start = aux->
p + 1; *p; ++p)
53 if (aux->
tab[*p>>6]>>(*p&0x3f)&1)
break;
55 for (p = start = aux->
p + 1; *p; ++p)
56 if (*p == aux->
sep)
break;
64 int ksplit_core(
char *s,
int delimiter,
int *_max,
int **_offsets)
66 int i, n,
max, last_char, last_start, *offsets, l;
67 n = 0; max = *_max; offsets = *_offsets;
70 #define __ksplit_aux do { \
75 max = max? max<<1 : 2; \
76 if ((tmp = (int*)realloc(offsets, sizeof(int) * max))) { \
84 offsets[n++] = last_start; \
88 for (i = 0, last_char = last_start = 0; i <= l; ++i) {
90 if (isspace(s[i]) || s[i] == 0) {
93 if (isspace(last_char) || last_char == 0) last_start = i;
96 if (s[i] == delimiter || s[i] == 0) {
97 if (last_char != 0 && last_char != delimiter)
__ksplit_aux;
99 if (last_char == delimiter || last_char == 0) last_start = i;
104 *_max =
max; *_offsets = offsets;
115 static int *ksBM_prep(
const ubyte_t *pat,
int m)
117 int i, *suff, *prep, *bmGs, *bmBc;
118 prep = (
int*)calloc(m + 256,
sizeof(
int));
119 bmGs = prep; bmBc = prep + m;
121 for (i = 0; i < 256; ++i) bmBc[i] = m;
122 for (i = 0; i < m - 1; ++i) bmBc[pat[i]] = m - i - 1;
124 suff = (
int*)calloc(m,
sizeof(
int));
129 for (i = m - 2; i >= 0; --i) {
130 if (i > g && suff[i + m - 1 - f] < i - g)
131 suff[i] = suff[i + m - 1 - f];
135 while (g >= 0 && pat[g] == pat[g + m - 1 - f]) --g;
142 for (i = 0; i < m; ++i) bmGs[i] = m;
143 for (i = m - 1; i >= 0; --i)
144 if (suff[i] == i + 1)
145 for (; j < m - 1 - i; ++j)
148 for (i = 0; i <= m - 2; ++i)
149 bmGs[m - 1 - suff[i]] = m - 1 - i;
155 void *
kmemmem(
const void *_str,
int n,
const void *_pat,
int m,
int **_prep)
157 int i, j, *prep = 0, *bmGs, *bmBc;
160 prep = (_prep == 0 || *_prep == 0)? ksBM_prep(pat, m) : *_prep;
161 if (_prep && *_prep == 0) *_prep = prep;
162 bmGs = prep; bmBc = prep + m;
165 for (i = m - 1; i >= 0 && pat[i] == str[i+j]; --i);
167 int max = bmBc[str[i+j]] - m + 1 + i;
168 if (max < bmGs[i]) max = bmGs[i];
170 }
else return (
void*)(str + j);
172 if (_prep == 0) free(prep);
176 char *
kstrstr(
const char *str,
const char *pat,
int **_prep)
178 return (
char*)
kmemmem(str, strlen(str), pat, strlen(pat), _prep);
181 char *
kstrnstr(
const char *str,
const char *pat,
int n,
int **_prep)
183 return (
char*)
kmemmem(str, n, pat, strlen(pat), _prep);
201 printf(
"'%s'\n", s->
s);
203 fields = ksplit(s, 0, &n);
204 for (i = 0; i < n; ++i)
205 printf(
"field[%d] = '%s'\n", i, s->
s + fields[i]);
208 for (p =
kstrtok(
"ab:cde:fg/hij::k",
":/", &aux); p; p =
kstrtok(0, 0, &aux)) {
209 kputsn(p, aux.
p - p, s);
214 free(s->
s); free(s); free(fields);
217 static char *str =
"abcdefgcdgcagtcakcdcd";
218 static char *pat =
"cd";
221 while ((ret =
kstrstr(s, pat, &prep)) != 0) {
222 printf(
"match: %s\n", ret);