annotate PsiCLASS-1.0.2/samtools-0.1.19/kstring.c @ 0:903fc43d6227 draft default tip

Uploaded
author lsong10
date Fri, 26 Mar 2021 16:52:45 +0000
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1 #include <stdarg.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
2 #include <stdio.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
3 #include <ctype.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
4 #include <string.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
5 #include <stdint.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
6 #include "kstring.h"
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
7
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
8 int ksprintf(kstring_t *s, const char *fmt, ...)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
9 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
10 va_list ap;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
11 int l;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
12 va_start(ap, fmt);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
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'.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
14 va_end(ap);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
15 if (l + 1 > s->m - s->l) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
16 s->m = s->l + l + 2;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
17 kroundup32(s->m);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
18 s->s = (char*)realloc(s->s, s->m);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
19 va_start(ap, fmt);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
20 l = vsnprintf(s->s + s->l, s->m - s->l, fmt, ap);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
21 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
22 va_end(ap);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
23 s->l += l;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
24 return l;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
25 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
26
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
27 char *kstrtok(const char *str, const char *sep, ks_tokaux_t *aux)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
28 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
29 const char *p, *start;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
30 if (sep) { // set up the table
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
31 if (str == 0 && (aux->tab[0]&1)) return 0; // no need to set up if we have finished
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
32 aux->finished = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
33 if (sep[1]) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
34 aux->sep = -1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
35 aux->tab[0] = aux->tab[1] = aux->tab[2] = aux->tab[3] = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
36 for (p = sep; *p; ++p) aux->tab[*p>>6] |= 1ull<<(*p&0x3f);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
37 } else aux->sep = sep[0];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
38 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
39 if (aux->finished) return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
40 else if (str) aux->p = str - 1, aux->finished = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
41 if (aux->sep < 0) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
42 for (p = start = aux->p + 1; *p; ++p)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
43 if (aux->tab[*p>>6]>>(*p&0x3f)&1) break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
44 } else {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
45 for (p = start = aux->p + 1; *p; ++p)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
46 if (*p == aux->sep) break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
47 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
48 aux->p = p; // end of token
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
49 if (*p == 0) aux->finished = 1; // no more tokens
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
50 return (char*)start;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
51 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
52
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
53 // s MUST BE a null terminated string; l = strlen(s)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
54 int ksplit_core(char *s, int delimiter, int *_max, int **_offsets)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
55 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
56 int i, n, max, last_char, last_start, *offsets, l;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
57 n = 0; max = *_max; offsets = *_offsets;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
58 l = strlen(s);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
59
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
60 #define __ksplit_aux do { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
61 if (_offsets) { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
62 s[i] = 0; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
63 if (n == max) { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
64 max = max? max<<1 : 2; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
65 offsets = (int*)realloc(offsets, sizeof(int) * max); \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
66 } \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
67 offsets[n++] = last_start; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
68 } else ++n; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
69 } while (0)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
70
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
71 for (i = 0, last_char = last_start = 0; i <= l; ++i) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
72 if (delimiter == 0) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
73 if (isspace(s[i]) || s[i] == 0) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
74 if (isgraph(last_char)) __ksplit_aux; // the end of a field
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
75 } else {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
76 if (isspace(last_char) || last_char == 0) last_start = i;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
77 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
78 } else {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
79 if (s[i] == delimiter || s[i] == 0) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
80 if (last_char != 0 && last_char != delimiter) __ksplit_aux; // the end of a field
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
81 } else {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
82 if (last_char == delimiter || last_char == 0) last_start = i;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
83 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
84 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
85 last_char = s[i];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
86 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
87 *_max = max; *_offsets = offsets;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
88 return n;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
89 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
90
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
91 /**********************
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
92 * Boyer-Moore search *
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
93 **********************/
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
94
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
95 typedef unsigned char ubyte_t;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
96
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
97 // reference: http://www-igm.univ-mlv.fr/~lecroq/string/node14.html
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
98 static int *ksBM_prep(const ubyte_t *pat, int m)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
99 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
100 int i, *suff, *prep, *bmGs, *bmBc;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
101 prep = (int*)calloc(m + 256, sizeof(int));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
102 bmGs = prep; bmBc = prep + m;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
103 { // preBmBc()
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
104 for (i = 0; i < 256; ++i) bmBc[i] = m;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
105 for (i = 0; i < m - 1; ++i) bmBc[pat[i]] = m - i - 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
106 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
107 suff = (int*)calloc(m, sizeof(int));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
108 { // suffixes()
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
109 int f = 0, g;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
110 suff[m - 1] = m;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
111 g = m - 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
112 for (i = m - 2; i >= 0; --i) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
113 if (i > g && suff[i + m - 1 - f] < i - g)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
114 suff[i] = suff[i + m - 1 - f];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
115 else {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
116 if (i < g) g = i;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
117 f = i;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
118 while (g >= 0 && pat[g] == pat[g + m - 1 - f]) --g;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
119 suff[i] = f - g;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
120 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
121 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
122 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
123 { // preBmGs()
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
124 int j = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
125 for (i = 0; i < m; ++i) bmGs[i] = m;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
126 for (i = m - 1; i >= 0; --i)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
127 if (suff[i] == i + 1)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
128 for (; j < m - 1 - i; ++j)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
129 if (bmGs[j] == m)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
130 bmGs[j] = m - 1 - i;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
131 for (i = 0; i <= m - 2; ++i)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
132 bmGs[m - 1 - suff[i]] = m - 1 - i;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
133 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
134 free(suff);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
135 return prep;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
136 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
137
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
138 void *kmemmem(const void *_str, int n, const void *_pat, int m, int **_prep)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
139 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
140 int i, j, *prep = 0, *bmGs, *bmBc;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
141 const ubyte_t *str, *pat;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
142 str = (const ubyte_t*)_str; pat = (const ubyte_t*)_pat;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
143 prep = (_prep == 0 || *_prep == 0)? ksBM_prep(pat, m) : *_prep;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
144 if (_prep && *_prep == 0) *_prep = prep;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
145 bmGs = prep; bmBc = prep + m;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
146 j = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
147 while (j <= n - m) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
148 for (i = m - 1; i >= 0 && pat[i] == str[i+j]; --i);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
149 if (i >= 0) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
150 int max = bmBc[str[i+j]] - m + 1 + i;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
151 if (max < bmGs[i]) max = bmGs[i];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
152 j += max;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
153 } else return (void*)(str + j);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
154 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
155 if (_prep == 0) free(prep);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
156 return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
157 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
158
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
159 char *kstrstr(const char *str, const char *pat, int **_prep)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
160 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
161 return (char*)kmemmem(str, strlen(str), pat, strlen(pat), _prep);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
162 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
163
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
164 char *kstrnstr(const char *str, const char *pat, int n, int **_prep)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
165 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
166 return (char*)kmemmem(str, n, pat, strlen(pat), _prep);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
167 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
168
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
169 /***********************
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
170 * The main() function *
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
171 ***********************/
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
172
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
173 #ifdef KSTRING_MAIN
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
174 #include <stdio.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
175 int main()
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
176 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
177 kstring_t *s;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
178 int *fields, n, i;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
179 ks_tokaux_t aux;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
180 char *p;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
181 s = (kstring_t*)calloc(1, sizeof(kstring_t));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
182 // test ksprintf()
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
183 ksprintf(s, " abcdefg: %d ", 100);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
184 printf("'%s'\n", s->s);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
185 // test ksplit()
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
186 fields = ksplit(s, 0, &n);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
187 for (i = 0; i < n; ++i)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
188 printf("field[%d] = '%s'\n", i, s->s + fields[i]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
189 // test kstrtok()
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
190 s->l = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
191 for (p = kstrtok("ab:cde:fg/hij::k", ":/", &aux); p; p = kstrtok(0, 0, &aux)) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
192 kputsn(p, aux.p - p, s);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
193 kputc('\n', s);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
194 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
195 printf("%s", s->s);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
196 // free
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
197 free(s->s); free(s); free(fields);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
198
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
199 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
200 static char *str = "abcdefgcdgcagtcakcdcd";
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
201 static char *pat = "cd";
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
202 char *ret, *s = str;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
203 int *prep = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
204 while ((ret = kstrstr(s, pat, &prep)) != 0) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
205 printf("match: %s\n", ret);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
206 s = ret + prep[0];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
207 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
208 free(prep);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
209 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
210 return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
211 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
212 #endif