annotate ezBAMQC/src/htslib/kstring.c @ 11:5bfcc6c131ed

Uploaded
author cshl-bsr
date Wed, 30 Mar 2016 12:14:21 -0400
parents dfa3745e5fd8
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
1 /* The MIT License
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
2
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
3 Copyright (C) 2011 by Attractive Chaos <attractor@live.co.uk>
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
4
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
5 Permission is hereby granted, free of charge, to any person obtaining
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
6 a copy of this software and associated documentation files (the
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
7 "Software"), to deal in the Software without restriction, including
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
8 without limitation the rights to use, copy, modify, merge, publish,
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
9 distribute, sublicense, and/or sell copies of the Software, and to
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
10 permit persons to whom the Software is furnished to do so, subject to
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
11 the following conditions:
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
12
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
13 The above copyright notice and this permission notice shall be
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
14 included in all copies or substantial portions of the Software.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
15
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
17 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
18 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
19 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
20 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
21 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
22 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
23 SOFTWARE.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
24 */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
25
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
26 #include <stdarg.h>
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
27 #include <stdio.h>
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
28 #include <ctype.h>
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
29 #include <string.h>
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
30 #include <stdint.h>
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
31 #include "htslib/kstring.h"
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
32
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
33 int kvsprintf(kstring_t *s, const char *fmt, va_list ap)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
34 {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
35 va_list args;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
36 int l;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
37 va_copy(args, ap);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
38 l = vsnprintf(s->s + s->l, s->m - s->l, fmt, args); // This line does not work with glibc 2.0. See `man snprintf'.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
39 va_end(args);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
40 if (l + 1 > s->m - s->l) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
41 s->m = s->l + l + 2;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
42 kroundup32(s->m);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
43 s->s = (char*)realloc(s->s, s->m);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
44 va_copy(args, ap);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
45 l = vsnprintf(s->s + s->l, s->m - s->l, fmt, args);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
46 va_end(args);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
47 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
48 s->l += l;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
49 return l;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
50 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
51
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
52 int ksprintf(kstring_t *s, const char *fmt, ...)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
53 {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
54 va_list ap;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
55 int l;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
56 va_start(ap, fmt);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
57 l = kvsprintf(s, fmt, ap);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
58 va_end(ap);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
59 return l;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
60 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
61
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
62 char *kstrtok(const char *str, const char *sep, ks_tokaux_t *aux)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
63 {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
64 const char *p, *start;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
65 if (sep) { // set up the table
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
66 if (str == 0 && (aux->tab[0]&1)) return 0; // no need to set up if we have finished
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
67 aux->finished = 0;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
68 if (sep[1]) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
69 aux->sep = -1;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
70 aux->tab[0] = aux->tab[1] = aux->tab[2] = aux->tab[3] = 0;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
71 for (p = sep; *p; ++p) aux->tab[*p>>6] |= 1ull<<(*p&0x3f);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
72 } else aux->sep = sep[0];
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
73 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
74 if (aux->finished) return 0;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
75 else if (str) aux->p = str - 1, aux->finished = 0;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
76 if (aux->sep < 0) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
77 for (p = start = aux->p + 1; *p; ++p)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
78 if (aux->tab[*p>>6]>>(*p&0x3f)&1) break;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
79 } else {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
80 for (p = start = aux->p + 1; *p; ++p)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
81 if (*p == aux->sep) break;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
82 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
83 aux->p = p; // end of token
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
84 if (*p == 0) aux->finished = 1; // no more tokens
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
85 return (char*)start;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
86 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
87
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
88 // s MUST BE a null terminated string; l = strlen(s)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
89 int ksplit_core(char *s, int delimiter, int *_max, int **_offsets)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
90 {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
91 int i, n, max, last_char, last_start, *offsets, l;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
92 n = 0; max = *_max; offsets = *_offsets;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
93 l = strlen(s);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
94
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
95 #define __ksplit_aux do { \
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
96 if (_offsets) { \
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
97 s[i] = 0; \
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
98 if (n == max) { \
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
99 int *tmp; \
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
100 max = max? max<<1 : 2; \
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
101 if ((tmp = (int*)realloc(offsets, sizeof(int) * max))) { \
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
102 offsets = tmp; \
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
103 } else { \
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
104 free(offsets); \
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
105 *_offsets = NULL; \
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
106 return 0; \
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
107 } \
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
108 } \
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
109 offsets[n++] = last_start; \
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
110 } else ++n; \
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
111 } while (0)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
112
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
113 for (i = 0, last_char = last_start = 0; i <= l; ++i) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
114 if (delimiter == 0) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
115 if (isspace(s[i]) || s[i] == 0) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
116 if (isgraph(last_char)) __ksplit_aux; // the end of a field
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
117 } else {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
118 if (isspace(last_char) || last_char == 0) last_start = i;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
119 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
120 } else {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
121 if (s[i] == delimiter || s[i] == 0) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
122 if (last_char != 0 && last_char != delimiter) __ksplit_aux; // the end of a field
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
123 } else {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
124 if (last_char == delimiter || last_char == 0) last_start = i;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
125 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
126 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
127 last_char = s[i];
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
128 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
129 *_max = max; *_offsets = offsets;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
130 return n;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
131 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
132
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
133 /**********************
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
134 * Boyer-Moore search *
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
135 **********************/
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
136
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
137 typedef unsigned char ubyte_t;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
138
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
139 // reference: http://www-igm.univ-mlv.fr/~lecroq/string/node14.html
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
140 static int *ksBM_prep(const ubyte_t *pat, int m)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
141 {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
142 int i, *suff, *prep, *bmGs, *bmBc;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
143 prep = (int*)calloc(m + 256, sizeof(int));
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
144 bmGs = prep; bmBc = prep + m;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
145 { // preBmBc()
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
146 for (i = 0; i < 256; ++i) bmBc[i] = m;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
147 for (i = 0; i < m - 1; ++i) bmBc[pat[i]] = m - i - 1;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
148 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
149 suff = (int*)calloc(m, sizeof(int));
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
150 { // suffixes()
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
151 int f = 0, g;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
152 suff[m - 1] = m;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
153 g = m - 1;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
154 for (i = m - 2; i >= 0; --i) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
155 if (i > g && suff[i + m - 1 - f] < i - g)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
156 suff[i] = suff[i + m - 1 - f];
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
157 else {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
158 if (i < g) g = i;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
159 f = i;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
160 while (g >= 0 && pat[g] == pat[g + m - 1 - f]) --g;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
161 suff[i] = f - g;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
162 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
163 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
164 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
165 { // preBmGs()
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
166 int j = 0;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
167 for (i = 0; i < m; ++i) bmGs[i] = m;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
168 for (i = m - 1; i >= 0; --i)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
169 if (suff[i] == i + 1)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
170 for (; j < m - 1 - i; ++j)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
171 if (bmGs[j] == m)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
172 bmGs[j] = m - 1 - i;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
173 for (i = 0; i <= m - 2; ++i)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
174 bmGs[m - 1 - suff[i]] = m - 1 - i;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
175 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
176 free(suff);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
177 return prep;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
178 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
179
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
180 void *kmemmem(const void *_str, int n, const void *_pat, int m, int **_prep)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
181 {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
182 int i, j, *prep = 0, *bmGs, *bmBc;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
183 const ubyte_t *str, *pat;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
184 str = (const ubyte_t*)_str; pat = (const ubyte_t*)_pat;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
185 prep = (_prep == 0 || *_prep == 0)? ksBM_prep(pat, m) : *_prep;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
186 if (_prep && *_prep == 0) *_prep = prep;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
187 bmGs = prep; bmBc = prep + m;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
188 j = 0;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
189 while (j <= n - m) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
190 for (i = m - 1; i >= 0 && pat[i] == str[i+j]; --i);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
191 if (i >= 0) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
192 int max = bmBc[str[i+j]] - m + 1 + i;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
193 if (max < bmGs[i]) max = bmGs[i];
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
194 j += max;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
195 } else return (void*)(str + j);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
196 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
197 if (_prep == 0) free(prep);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
198 return 0;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
199 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
200
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
201 char *kstrstr(const char *str, const char *pat, int **_prep)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
202 {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
203 return (char*)kmemmem(str, strlen(str), pat, strlen(pat), _prep);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
204 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
205
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
206 char *kstrnstr(const char *str, const char *pat, int n, int **_prep)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
207 {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
208 return (char*)kmemmem(str, n, pat, strlen(pat), _prep);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
209 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
210
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
211 /***********************
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
212 * The main() function *
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
213 ***********************/
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
214
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
215 #ifdef KSTRING_MAIN
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
216 #include <stdio.h>
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
217 int main()
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
218 {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
219 kstring_t *s;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
220 int *fields, n, i;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
221 ks_tokaux_t aux;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
222 char *p;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
223 s = (kstring_t*)calloc(1, sizeof(kstring_t));
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
224 // test ksprintf()
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
225 ksprintf(s, " abcdefg: %d ", 100);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
226 printf("'%s'\n", s->s);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
227 // test ksplit()
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
228 fields = ksplit(s, 0, &n);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
229 for (i = 0; i < n; ++i)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
230 printf("field[%d] = '%s'\n", i, s->s + fields[i]);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
231 // test kstrtok()
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
232 s->l = 0;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
233 for (p = kstrtok("ab:cde:fg/hij::k", ":/", &aux); p; p = kstrtok(0, 0, &aux)) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
234 kputsn(p, aux.p - p, s);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
235 kputc('\n', s);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
236 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
237 printf("%s", s->s);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
238 // free
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
239 free(s->s); free(s); free(fields);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
240
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
241 {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
242 static char *str = "abcdefgcdgcagtcakcdcd";
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
243 static char *pat = "cd";
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
244 char *ret, *s = str;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
245 int *prep = 0;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
246 while ((ret = kstrstr(s, pat, &prep)) != 0) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
247 printf("match: %s\n", ret);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
248 s = ret + prep[0];
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
249 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
250 free(prep);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
251 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
252 return 0;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
253 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
254 #endif