forked from wym6912/sreformat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsre_string.c
527 lines (469 loc) · 13.9 KB
/
sre_string.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
/*****************************************************************
* HMMER - Biological sequence analysis with profile HMMs
* Copyright (C) 1992-2003 Washington University School of Medicine
* All Rights Reserved
*
* This source code is distributed under the terms of the
* GNU General Public License. See the files COPYING and LICENSE
* for details.
*****************************************************************/
/* sre_string.c
*
* my library of extra string functions. Some for portability
* across UNIXes
*
* CVS $Id: sre_string.c,v 1.14 2003/05/26 16:21:50 eddy Exp $
*/
#include "squidconf.h"
#include "squid.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <ctype.h>
/* Function: Strdup()
*
* Purpose: Implementation of the common (but non-ANSI) function
* strdup(). Robust against being passed a NULL pointer.
*
*/
char *
Strdup(char *s)
{
char *new;
if (s == NULL) return NULL;
if ((new = (char *) malloc (strlen(s) +1)) == NULL) return NULL;
strcpy(new, s);
return new;
}
/* Function: StringChop()
* Date: SRE, Wed Oct 29 12:10:02 1997 [TWA 721]
*
* Purpose: Chop trailing whitespace off of a string.
*/
void
StringChop(char *s)
{
int i;
i = strlen(s) - 1; /* set i at last char in string */
while (i >= 0 && isspace((int) s[i])) i--; /* i now at last non-whitespace char, or -1 */
s[i+1] = '\0';
}
int
Strinsert(char *s1, /* string to insert a char into */
char c, /* char to insert */
int pos) /* position in s1 to insert c at */
{
char oldc;
char *s;
for (s = s1 + pos; c; s++)
{
/* swap current char for inserted one */
oldc = *s; /* pick up current */
*s = c; /* put down inserted one */
c = oldc; /* old becomes next to insert */
}
*s = '\0';
return 1;
}
int
Strdelete(char *s1, /* string to delete a char from */
int pos) /* position of char to delete 0..n-1 */
{
char *s;
for (s = s1 + pos; *s; s++)
*s = *(s + 1);
return 1;
}
void
s2lower(char *s)
{
for (; *s != '\0'; s++)
*s = sre_tolower((int) *s);
}
void
s2upper(char *s)
{
for (; *s != '\0'; s++)
*s = sre_toupper((int) *s);
}
void *
sre_malloc(char *file, int line, size_t size)
{
void *ptr;
SQD_DPRINTF3(("MALLOC: %d bytes (file %s line %d)\n", size, file, line));
if ((ptr = malloc (size)) == NULL)
Die("malloc of %ld bytes failed: file %s line %d", size, file, line);
return ptr;
}
void *
sre_realloc(char *file, int line, void *p, size_t size)
{
void *ptr;
if ((ptr = realloc(p, size)) == NULL)
Die("realloc of %ld bytes failed: file %s line %d", size, file, line);
return ptr;
}
/* Function: Free2DArray(), Free3DArray()
* Date: SRE, Tue Jun 1 14:47:14 1999 [St. Louis]
*
* Purpose: Convenience functions for free'ing 2D
* and 3D pointer arrays. Tolerates any of the
* pointers being NULL, to allow "sparse"
* arrays.
*
* Args: p - array to be freed
* dim1 - n for first dimension
* dim2 - n for second dimension
*
* e.g. a 2d array is indexed p[0..dim1-1][]
* a 3D array is indexed p[0..dim1-1][0..dim2-1][]
*
* Returns: void
*
* Diagnostics: (void)
* "never fails"
*/
void
Free2DArray(void **p, int dim1)
{
int i;
if (p != NULL) {
for (i = 0; i < dim1; i++)
if (p[i] != NULL) free(p[i]);
free(p);
}
}
void
Free3DArray(void ***p, int dim1, int dim2)
{
int i, j;
if (p != NULL) {
for (i = 0; i < dim1; i++)
if (p[i] != NULL) {
for (j = 0; j < dim2; j++)
if (p[i][j] != NULL) free(p[i][j]);
free(p[i]);
}
free(p);
}
}
/* Function: RandomSequence()
*
* Purpose: Generate an iid symbol sequence according
* to some alphabet, alphabet_size, probability
* distribution, and length. Return the
* sequence.
*
* Args: alphabet - e.g. "ACGT"
* p - probability distribution [0..n-1]
* n - number of symbols in alphabet
* len - length of generated sequence
*
* Return: ptr to random sequence, or NULL on failure.
*/
char *
RandomSequence(char *alphabet, float *p, int n, int len)
{
char *s;
int x;
s = (char *) MallocOrDie (sizeof(char) * (len+1));
for (x = 0; x < len; x++)
s[x] = alphabet[FChoose(p,n)];
s[x] = '\0';
return s;
}
/* Function: sre_fgets()
* Date: SRE, Thu May 13 10:56:28 1999 [St. Louis]
*
* Purpose: Dynamic allocation version of fgets(),
* capable of reading unlimited line lengths.
*
* Args: buf - ptr to a string (may be reallocated)
* n - ptr to current allocated length of buf,
* (may be changed)
* fp - open file ptr for reading
*
* Before the first call to sre_fgets(),
* buf should be initialized to NULL and n to 0.
* They're a linked pair, so don't muck with the
* allocation of buf or the value of n while
* you're still doing sre_fgets() calls with them.
*
* Returns: ptr to the buffer on success.
* NULL on EOF (buf isn't to be used in this case)
* sre_fgets() *always* results in an allocation
* in buf.
*
* The reason to have it return a ptr to buf
* is that it makes wrapper macros easy; see
* MSAFileGetLine() for an example.
*
* Example: char *buf;
* int n;
* FILE *fp;
*
* fp = fopen("my_file", "r");
* buf = NULL;
* n = 0;
* while (sre_fgets(&buf, &n, fp) != NULL)
* {
* do stuff with buf;
* }
*/
char *
sre_fgets(char **buf, int *n, FILE *fp)
{
char *s;
int len;
int pos;
if (*n == 0)
{
*buf = MallocOrDie(sizeof(char) * 128);
*n = 128;
}
/* Simple case 1. We're sitting at EOF, or there's an error.
* fgets() returns NULL, so we return NULL.
*/
if (fgets(*buf, *n, fp) == NULL) return NULL;
/* Simple case 2. fgets() got a string, and it reached EOF.
* return success status, so caller can use
* the last line; on the next call we'll
* return the 0 for the EOF.
*/
if (feof(fp)) return *buf;
/* Simple case 3. We got a complete string, with \n,
* and don't need to extend the buffer.
*/
len = strlen(*buf);
if ((*buf)[len-1] == '\n') return *buf;
/* The case we're waiting for. We have an incomplete string,
* and we have to extend the buffer one or more times. Make
* sure we overwrite the previous fgets's \0 (hence +(n-1)
* in first step, rather than 128, and reads of 129, not 128).
*/
pos = (*n)-1;
while (1) {
*n += 128;
*buf = ReallocOrDie(*buf, sizeof(char) * (*n));
s = *buf + pos;
if (fgets(s, 129, fp) == NULL) return *buf;
len = strlen(s);
if (s[len-1] == '\n') return *buf;
pos += 128;
}
/*NOTREACHED*/
}
/* Function: sre_strcat()
* Date: SRE, Thu May 13 09:36:32 1999 [St. Louis]
*
* Purpose: Dynamic memory version of strcat().
* appends src to the string that dest points to,
* extending allocation for dest if necessary.
*
* One timing experiment (100 successive appends of
* 1-255 char) shows sre_strcat() has about a 20%
* overhead relative to strcat(). However, if optional
* length info is passed, sre_strcat() is about 30%
* faster than strcat().
*
* Args: dest - ptr to string (char **), '\0' terminated
* ldest - length of dest, if known; or -1 if length unknown.
* src - string to append to dest, '\0' terminated
* lsrc - length of src, if known; or -1 if length unknown.
*
* dest may be NULL, in which case this is
* the equivalent of dest = Strdup(src).
*
* src may also be NULL, in which case
* dest is unmodified (but why would you want to pass
* a NULL src?)
*
* if both dest and src are NULL, dest is
* unmodified; it stays NULL.
*
* the length parameters are optional. If a -1
* is passed, sre_strcat() will call strlen() to
* determine the length itself. Passing length
* info saves the strlen() calls and can speed things
* up if lots of successive appends need to be done.
*
* Returns: new length of dest (>=0 on success);
* dest is (probably) reallocated, and modified
* to a longer string, '\0' terminated.
*/
int
sre_strcat(char **dest, int ldest, char *src, int lsrc)
{
int len1, len2;
if (ldest < 0) len1 = ((*dest == NULL) ? 0 : strlen(*dest));
else len1 = ldest;
if (lsrc < 0) len2 = (( src == NULL) ? 0 : strlen(src));
else len2 = lsrc;
if (len2 == 0) return len1;
if (*dest == NULL) *dest = MallocOrDie(sizeof(char) * (len2+1));
else *dest = ReallocOrDie(*dest, sizeof(char) * (len1+len2+1));
memcpy((*dest)+len1, src, len2+1);
return len1+len2;
}
/* Function: sre_strtok()
* Date: SRE, Wed May 19 16:30:20 1999 [St. Louis]
*
* Purpose: Thread-safe version of strtok().
*
* Returns ptr to next token in a string: skips
* until it reaches a character that is not in the delim
* string, and sets beginning of token. Skips to
* next delim character (or '\0') to set the end; replaces that
* character with '\0'.
* If there's still more string left, sets s to point to next
* character after the '\0' that was written, so successive
* calls extract tokens in succession. If there was no string
* left, s points at the terminal '\0'.
*
* If no token is found, returns NULL.
*
* Also returns the length of the token, which
* may save us a strlen() call in some applications.
*
* Limitations:
* *s can't be a constant string, since we write to it.
*
* Example:
* char *tok;
* int len;
* char *s;
* char buf[50] = "This is a sentence.";
*
* s = buf;
* tok = sre_strtok(&s, " ", &len);
* tok is "This"; s is "is a sentence."; len is 4.
* tok = sre_strtok(&s, " ", &len);
* tok is "is"; s is " a sentence."; len is 2.
* tok = sre_strtok(&s, " ", &len);
* tok is "a"; s is "sentence."; len is 1.
* tok = sre_strtok(&s, " ", &len);
* tok is "sentence."; s is "\0"; len is 9.
* tok = sre_strtok(&s, " ", &len);
* tok is NULL; s is "\0", len is undefined.
*
* Args: s - a tmp, modifiable ptr to string
* delim - characters that delimits tokens
* len - RETURN: length of token; pass NULL if not wanted
*
* Returns: ptr to next token, or NULL if there aren't any.
*/
char *
sre_strtok(char **s, char *delim, int *len)
{
char *begin, *end;
int n;
begin = *s;
begin += strspn(begin, delim);
if (! *begin) return NULL;
n = strcspn(begin, delim);
end = begin + n;
if (*end == '\0') { *s = end;}
else {
*end = '\0';
*s = end+1;
}
if (len != NULL) *len = n;
return begin;
}
/* Function: sre_strdup()
* Date: SRE, Wed May 19 17:57:28 1999 [St. Louis]
*
* Purpose: A version of the common but non-ANSI strdup()
* function. Can pass len, if known, to save a
* strlen() call.
*
* Args: s - string to duplicate
* n - length of string, if known; -1 if unknown.
*
* Returns: allocated copy of string.
* NULL on failure.
*/
char *
sre_strdup(char *s, int n)
{
char *new;
if (s == NULL) return NULL;
if (n < 0) n = strlen(s);
new = MallocOrDie (sizeof(char) * (n+1));
strcpy(new, s);
return new;
}
/* Function: sre_strncpy()
* Date: SRE, Tue Jun 22 10:10:46 1999 [Sanger Centre]
*
* Purpose: a strncpy() that makes sure it adds a trailing \0.
*
* Args: s1 - string to copy to (allocated n+1 or larger)
* s2 - string to copy from
* n - number of chars to copy
*
* Returns: s1.
* Done only for consistency with strncpy(). Not clear
* why it's useful for a strncpy() to return s1.
*/
char *
sre_strncpy(char *s1, char *s2, int n)
{
strncpy(s1,s2,n);
s1[n] = '\0';
return s1;
}
/* Function: IsBlankline()
* Date: SRE, Fri Jun 18 14:36:08 1999 [St. Louis]
*
* Purpose: Returns TRUE if string consists solely of whitespace.
*
* Args: s - string to check
*/
int
IsBlankline(char *s)
{
for (; *s != '\0'; s++)
if (! isspace((int) *s)) return FALSE;
return TRUE;
}
#ifdef CUBS_WIN
/* A timing test for sre_strcat()
* cc -O2 -g sre_string.c sre_ctype.c sqerror.c sre_math.c hsregex.c -lm
* 15.200u - 5.360u = 9.84u if sre_strcat() with no length info passed
* 13.660u - 5.360u = 8.30u if strcat(), with a single malloc().
* 11.370u - 5.360u = 6.01u if sre_strcat() with length info passed.
*/
int main(void)
{
float p[4] = {0.25, 0.25, 0.25, 0.25};
int buflen;
int len;
int nappends;
int nstrings;
char *s1 = NULL;
char *s2;
int i;
nappends = 100;
nstrings = 1000;
while (nstrings--)
{
/* s1 = malloc(sizeof(char) * (255*nappends+1));
s1[0] = '\0';
*/
s1 = NULL;
len = 0;
for (i = 0; i < nappends; i++)
{
buflen = CHOOSE(255) + 1;
s2 = RandomSequence("ACGT", p, 4, buflen);
/* strcat(s1,s2); */
if ((len = sre_strcat(&s1, len, s2, buflen)) < 0) exit(1);
free(s2);
}
free(s1);
}
exit(0);
}
#endif /*CUBS_WIN*/