8 int ksprintf(kstring_t *s, const char *fmt, ...)
13 l = vsnprintf(s->s + s->l, s->m - s->l, fmt, ap); // This line does not work with glibc 2.0. See `man snprintf'.
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, ap);
27 char *kstrtok(const char *str, const char *sep, ks_tokaux_t *aux)
29 const char *p, *start;
30 if (sep) { // set up the table
31 if (str == 0 && (aux->tab[0]&1)) return 0; // no need to set up if we have finished
35 aux->tab[0] = aux->tab[1] = aux->tab[2] = aux->tab[3] = 0;
36 for (p = sep; *p; ++p) aux->tab[*p>>6] |= 1ull<<(*p&0x3f);
37 } else aux->sep = sep[0];
39 if (aux->finished) return 0;
40 else if (str) aux->p = str - 1, aux->finished = 0;
42 for (p = start = aux->p + 1; *p; ++p)
43 if (aux->tab[*p>>6]>>(*p&0x3f)&1) break;
45 for (p = start = aux->p + 1; *p; ++p)
46 if (*p == aux->sep) break;
48 aux->p = p; // end of token
49 if (*p == 0) aux->finished = 1; // no more tokens
53 // s MUST BE a null terminated string; l = strlen(s)
54 int ksplit_core(char *s, int delimiter, int *_max, int **_offsets)
56 int i, n, max, last_char, last_start, *offsets, l;
57 n = 0; max = *_max; offsets = *_offsets;
60 #define __ksplit_aux do { \
64 max = max? max<<1 : 2; \
65 offsets = (int*)realloc(offsets, sizeof(int) * max); \
67 offsets[n++] = last_start; \
71 for (i = 0, last_char = last_start = 0; i <= l; ++i) {
73 if (isspace(s[i]) || s[i] == 0) {
74 if (isgraph(last_char)) __ksplit_aux; // the end of a field
76 if (isspace(last_char) || last_char == 0) last_start = i;
79 if (s[i] == delimiter || s[i] == 0) {
80 if (last_char != 0 && last_char != delimiter) __ksplit_aux; // the end of a field
82 if (last_char == delimiter || last_char == 0) last_start = i;
87 *_max = max; *_offsets = offsets;
91 /**********************
92 * Boyer-Moore search *
93 **********************/
95 typedef unsigned char ubyte_t;
97 // reference: http://www-igm.univ-mlv.fr/~lecroq/string/node14.html
98 static int *ksBM_prep(const ubyte_t *pat, int m)
100 int i, *suff, *prep, *bmGs, *bmBc;
101 prep = (int*)calloc(m + 256, sizeof(int));
102 bmGs = prep; bmBc = prep + m;
104 for (i = 0; i < 256; ++i) bmBc[i] = m;
105 for (i = 0; i < m - 1; ++i) bmBc[pat[i]] = m - i - 1;
107 suff = (int*)calloc(m, sizeof(int));
112 for (i = m - 2; i >= 0; --i) {
113 if (i > g && suff[i + m - 1 - f] < i - g)
114 suff[i] = suff[i + m - 1 - f];
118 while (g >= 0 && pat[g] == pat[g + m - 1 - f]) --g;
125 for (i = 0; i < m; ++i) bmGs[i] = m;
126 for (i = m - 1; i >= 0; --i)
127 if (suff[i] == i + 1)
128 for (; j < m - 1 - i; ++j)
131 for (i = 0; i <= m - 2; ++i)
132 bmGs[m - 1 - suff[i]] = m - 1 - i;
138 void *kmemmem(const void *_str, int n, const void *_pat, int m, int **_prep)
140 int i, j, *prep = 0, *bmGs, *bmBc;
141 const ubyte_t *str, *pat;
142 str = (const ubyte_t*)_str; pat = (const ubyte_t*)_pat;
143 prep = (_prep == 0 || *_prep == 0)? ksBM_prep(pat, m) : *_prep;
144 if (_prep && *_prep == 0) *_prep = prep;
145 bmGs = prep; bmBc = prep + m;
148 for (i = m - 1; i >= 0 && pat[i] == str[i+j]; --i);
150 int max = bmBc[str[i+j]] - m + 1 + i;
151 if (max < bmGs[i]) max = bmGs[i];
153 } else return (void*)(str + j);
155 if (_prep == 0) free(prep);
159 char *kstrstr(const char *str, const char *pat, int **_prep)
161 return (char*)kmemmem(str, strlen(str), pat, strlen(pat), _prep);
164 char *kstrnstr(const char *str, const char *pat, int n, int **_prep)
166 return (char*)kmemmem(str, n, pat, strlen(pat), _prep);
169 /***********************
170 * The main() function *
171 ***********************/
181 s = (kstring_t*)calloc(1, sizeof(kstring_t));
183 ksprintf(s, " abcdefg: %d ", 100);
184 printf("'%s'\n", s->s);
186 fields = ksplit(s, 0, &n);
187 for (i = 0; i < n; ++i)
188 printf("field[%d] = '%s'\n", i, s->s + fields[i]);
191 for (p = kstrtok("ab:cde:fg/hij::k", ":/", &aux); p; p = kstrtok(0, 0, &aux)) {
192 kputsn(p, aux.p - p, s);
197 free(s->s); free(s); free(fields);
200 static char *str = "abcdefgcdgcagtcakcdcd";
201 static char *pat = "cd";
204 while ((ret = kstrstr(s, pat, &prep)) != 0) {
205 printf("match: %s\n", ret);