Fix compiler warning due to missing function prototype.
[svn.git] / subversion / libsvn_subr / svn_string.c
blob9dd7b6422693201149182b8ab232571543bd96a2
1 /*
2 * svn_string.h: routines to manipulate counted-length strings
3 * (svn_stringbuf_t and svn_string_t) and C strings.
6 * ====================================================================
7 * Copyright (c) 2000-2007 CollabNet. All rights reserved.
9 * This software is licensed as described in the file COPYING, which
10 * you should have received as part of this distribution. The terms
11 * are also available at http://subversion.tigris.org/license-1.html.
12 * If newer versions of this license are posted there, you may use a
13 * newer version instead, at your option.
15 * This software consists of voluntary contributions made by many
16 * individuals. For exact contribution history, see the revision
17 * history and logs, available at http://subversion.tigris.org/.
18 * ====================================================================
23 #include <string.h> /* for memcpy(), memcmp(), strlen() */
24 #include <apr_lib.h> /* for apr_isspace() */
25 #include <apr_md5.h>
26 #include <apr_fnmatch.h>
27 #include "svn_string.h" /* loads "svn_types.h" and <apr_pools.h> */
28 #include "svn_ctype.h"
32 /* Our own realloc, since APR doesn't have one. Note: this is a
33 generic realloc for memory pools, *not* for strings. */
34 static void *
35 my__realloc(char *data, apr_size_t oldsize, apr_size_t request,
36 apr_pool_t *pool)
38 void *new_area;
40 /* kff todo: it's a pity APR doesn't give us this -- sometimes it
41 could realloc the block merely by extending in place, sparing us
42 a memcpy(), but only the pool would know enough to be able to do
43 this. We should add a realloc() to APR if someone hasn't
44 already. */
46 /* malloc new area */
47 new_area = apr_palloc(pool, request);
49 /* copy data to new area */
50 memcpy(new_area, data, oldsize);
52 /* I'm NOT freeing old area here -- cuz we're using pools, ugh. */
54 /* return new area */
55 return new_area;
58 static APR_INLINE svn_boolean_t
59 string_compare(const char *str1,
60 const char *str2,
61 apr_size_t len1,
62 apr_size_t len2)
64 /* easy way out :) */
65 if (len1 != len2)
66 return FALSE;
68 /* now the strings must have identical lenghths */
70 if ((memcmp(str1, str2, len1)) == 0)
71 return TRUE;
72 else
73 return FALSE;
76 static APR_INLINE apr_size_t
77 string_first_non_whitespace(const char *str, apr_size_t len)
79 apr_size_t i;
81 for (i = 0; i < len; i++)
83 if (! apr_isspace(str[i]))
84 return i;
87 /* if we get here, then the string must be entirely whitespace */
88 return len;
91 static APR_INLINE apr_size_t
92 find_char_backward(const char *str, apr_size_t len, char ch)
94 apr_size_t i = len;
96 while (i != 0)
98 if (str[--i] == ch)
99 return i;
102 /* char was not found, return len */
103 return len;
107 /* svn_string functions */
109 static svn_string_t *
110 create_string(const char *data, apr_size_t size,
111 apr_pool_t *pool)
113 svn_string_t *new_string;
115 new_string = apr_palloc(pool, sizeof(*new_string));
117 new_string->data = data;
118 new_string->len = size;
120 return new_string;
123 svn_string_t *
124 svn_string_ncreate(const char *bytes, apr_size_t size, apr_pool_t *pool)
126 char *data;
128 data = apr_palloc(pool, size + 1);
129 memcpy(data, bytes, size);
131 /* Null termination is the convention -- even if we suspect the data
132 to be binary, it's not up to us to decide, it's the caller's
133 call. Heck, that's why they call it the caller! */
134 data[size] = '\0';
136 /* wrap an svn_string_t around the new data */
137 return create_string(data, size, pool);
141 svn_string_t *
142 svn_string_create(const char *cstring, apr_pool_t *pool)
144 return svn_string_ncreate(cstring, strlen(cstring), pool);
148 svn_string_t *
149 svn_string_create_from_buf(const svn_stringbuf_t *strbuf, apr_pool_t *pool)
151 return svn_string_ncreate(strbuf->data, strbuf->len, pool);
155 svn_string_t *
156 svn_string_createv(apr_pool_t *pool, const char *fmt, va_list ap)
158 char *data = apr_pvsprintf(pool, fmt, ap);
160 /* wrap an svn_string_t around the new data */
161 return create_string(data, strlen(data), pool);
165 svn_string_t *
166 svn_string_createf(apr_pool_t *pool, const char *fmt, ...)
168 svn_string_t *str;
170 va_list ap;
171 va_start(ap, fmt);
172 str = svn_string_createv(pool, fmt, ap);
173 va_end(ap);
175 return str;
179 svn_boolean_t
180 svn_string_isempty(const svn_string_t *str)
182 return (str->len == 0);
186 svn_string_t *
187 svn_string_dup(const svn_string_t *original_string, apr_pool_t *pool)
189 return (svn_string_ncreate(original_string->data,
190 original_string->len, pool));
195 svn_boolean_t
196 svn_string_compare(const svn_string_t *str1, const svn_string_t *str2)
198 return
199 string_compare(str1->data, str2->data, str1->len, str2->len);
204 apr_size_t
205 svn_string_first_non_whitespace(const svn_string_t *str)
207 return
208 string_first_non_whitespace(str->data, str->len);
212 apr_size_t
213 svn_string_find_char_backward(const svn_string_t *str, char ch)
215 return find_char_backward(str->data, str->len, ch);
220 /* svn_stringbuf functions */
222 static svn_stringbuf_t *
223 create_stringbuf(char *data, apr_size_t size, apr_size_t blocksize, apr_pool_t *pool)
225 svn_stringbuf_t *new_string;
227 new_string = apr_palloc(pool, sizeof(*new_string));
229 new_string->data = data;
230 new_string->len = size;
231 new_string->blocksize = blocksize;
232 new_string->pool = pool;
234 return new_string;
237 svn_stringbuf_t *
238 svn_stringbuf_create_ensure(apr_size_t blocksize, apr_pool_t *pool)
240 char *data = apr_palloc(pool, blocksize);
242 data[0] = '\0';
244 /* wrap an svn_stringbuf_t around the new data buffer. */
245 return create_stringbuf(data, 0, blocksize, pool);
248 svn_stringbuf_t *
249 svn_stringbuf_ncreate(const char *bytes, apr_size_t size, apr_pool_t *pool)
251 svn_stringbuf_t *strbuf = svn_stringbuf_create_ensure(size + 1, pool);
253 memcpy(strbuf->data, bytes, size);
255 /* Null termination is the convention -- even if we suspect the data
256 to be binary, it's not up to us to decide, it's the caller's
257 call. Heck, that's why they call it the caller! */
258 strbuf->data[size] = '\0';
259 strbuf->len = size;
261 return strbuf;
265 svn_stringbuf_t *
266 svn_stringbuf_create(const char *cstring, apr_pool_t *pool)
268 return svn_stringbuf_ncreate(cstring, strlen(cstring), pool);
272 svn_stringbuf_t *
273 svn_stringbuf_create_from_string(const svn_string_t *str, apr_pool_t *pool)
275 return svn_stringbuf_ncreate(str->data, str->len, pool);
279 svn_stringbuf_t *
280 svn_stringbuf_createv(apr_pool_t *pool, const char *fmt, va_list ap)
282 char *data = apr_pvsprintf(pool, fmt, ap);
283 apr_size_t size = strlen(data);
285 /* wrap an svn_stringbuf_t around the new data */
286 return create_stringbuf(data, size, size + 1, pool);
290 svn_stringbuf_t *
291 svn_stringbuf_createf(apr_pool_t *pool, const char *fmt, ...)
293 svn_stringbuf_t *str;
295 va_list ap;
296 va_start(ap, fmt);
297 str = svn_stringbuf_createv(pool, fmt, ap);
298 va_end(ap);
300 return str;
304 void
305 svn_stringbuf_fillchar(svn_stringbuf_t *str, unsigned char c)
307 memset(str->data, c, str->len);
311 void
312 svn_stringbuf_set(svn_stringbuf_t *str, const char *value)
314 apr_size_t amt = strlen(value);
316 svn_stringbuf_ensure(str, amt + 1);
317 memcpy(str->data, value, amt + 1);
318 str->len = amt;
321 void
322 svn_stringbuf_setempty(svn_stringbuf_t *str)
324 if (str->len > 0)
325 str->data[0] = '\0';
327 str->len = 0;
331 void
332 svn_stringbuf_chop(svn_stringbuf_t *str, apr_size_t nbytes)
334 if (nbytes > str->len)
335 str->len = 0;
336 else
337 str->len -= nbytes;
339 str->data[str->len] = '\0';
343 svn_boolean_t
344 svn_stringbuf_isempty(const svn_stringbuf_t *str)
346 return (str->len == 0);
350 void
351 svn_stringbuf_ensure(svn_stringbuf_t *str, apr_size_t minimum_size)
353 /* Keep doubling capacity until have enough. */
354 if (str->blocksize < minimum_size)
356 if (str->blocksize == 0)
357 str->blocksize = minimum_size;
358 else
359 while (str->blocksize < minimum_size)
361 apr_size_t prev_size = str->blocksize;
362 str->blocksize *= 2;
363 /* check for apr_size_t overflow */
364 if (prev_size > str->blocksize)
366 str->blocksize = minimum_size;
367 break;
371 str->data = (char *) my__realloc(str->data,
372 str->len + 1,
373 /* We need to maintain (and thus copy)
374 the trailing nul */
375 str->blocksize,
376 str->pool);
381 void
382 svn_stringbuf_appendbytes(svn_stringbuf_t *str, const char *bytes,
383 apr_size_t count)
385 apr_size_t total_len;
386 void *start_address;
388 total_len = str->len + count; /* total size needed */
390 /* +1 for null terminator. */
391 svn_stringbuf_ensure(str, (total_len + 1));
393 /* get address 1 byte beyond end of original bytestring */
394 start_address = (str->data + str->len);
396 memcpy(start_address, bytes, count);
397 str->len = total_len;
399 str->data[str->len] = '\0'; /* We don't know if this is binary
400 data or not, but convention is
401 to null-terminate. */
405 void
406 svn_stringbuf_appendstr(svn_stringbuf_t *targetstr,
407 const svn_stringbuf_t *appendstr)
409 svn_stringbuf_appendbytes(targetstr, appendstr->data, appendstr->len);
413 void
414 svn_stringbuf_appendcstr(svn_stringbuf_t *targetstr, const char *cstr)
416 svn_stringbuf_appendbytes(targetstr, cstr, strlen(cstr));
422 svn_stringbuf_t *
423 svn_stringbuf_dup(const svn_stringbuf_t *original_string, apr_pool_t *pool)
425 return (svn_stringbuf_ncreate(original_string->data,
426 original_string->len, pool));
431 svn_boolean_t
432 svn_stringbuf_compare(const svn_stringbuf_t *str1,
433 const svn_stringbuf_t *str2)
435 return string_compare(str1->data, str2->data, str1->len, str2->len);
440 apr_size_t
441 svn_stringbuf_first_non_whitespace(const svn_stringbuf_t *str)
443 return string_first_non_whitespace(str->data, str->len);
447 void
448 svn_stringbuf_strip_whitespace(svn_stringbuf_t *str)
450 /* Find first non-whitespace character */
451 apr_size_t offset = svn_stringbuf_first_non_whitespace(str);
453 /* Go ahead! Waste some RAM, we've got pools! :) */
454 str->data += offset;
455 str->len -= offset;
456 str->blocksize -= offset;
458 /* Now that we've trimmed the front, trim the end, wasting more RAM. */
459 while ((str->len > 0) && apr_isspace(str->data[str->len - 1]))
460 str->len--;
461 str->data[str->len] = '\0';
465 apr_size_t
466 svn_stringbuf_find_char_backward(const svn_stringbuf_t *str, char ch)
468 return find_char_backward(str->data, str->len, ch);
472 svn_boolean_t
473 svn_string_compare_stringbuf(const svn_string_t *str1,
474 const svn_stringbuf_t *str2)
476 return string_compare(str1->data, str2->data, str1->len, str2->len);
481 /*** C string stuff. ***/
483 void
484 svn_cstring_split_append(apr_array_header_t *array,
485 const char *input,
486 const char *sep_chars,
487 svn_boolean_t chop_whitespace,
488 apr_pool_t *pool)
490 char *last;
491 char *pats;
492 char *p;
494 pats = apr_pstrdup(pool, input); /* strtok wants non-const data */
495 p = apr_strtok(pats, sep_chars, &last);
497 while (p)
499 if (chop_whitespace)
501 while (apr_isspace(*p))
502 p++;
505 char *e = p + (strlen(p) - 1);
506 while ((e >= p) && (apr_isspace(*e)))
507 e--;
508 *(++e) = '\0';
512 if (p[0] != '\0')
513 APR_ARRAY_PUSH(array, const char *) = p;
515 p = apr_strtok(NULL, sep_chars, &last);
518 return;
522 apr_array_header_t *
523 svn_cstring_split(const char *input,
524 const char *sep_chars,
525 svn_boolean_t chop_whitespace,
526 apr_pool_t *pool)
528 apr_array_header_t *a = apr_array_make(pool, 5, sizeof(input));
529 svn_cstring_split_append(a, input, sep_chars, chop_whitespace, pool);
530 return a;
534 svn_boolean_t svn_cstring_match_glob_list(const char *str,
535 apr_array_header_t *list)
537 int i;
539 for (i = 0; i < list->nelts; i++)
541 const char *this_pattern = APR_ARRAY_IDX(list, i, char *);
543 if (apr_fnmatch(this_pattern, str, 0) == APR_SUCCESS)
544 return TRUE;
547 return FALSE;
550 int svn_cstring_count_newlines(const char *msg)
552 int count = 0;
553 const char *p;
555 for (p = msg; *p; p++)
557 if (*p == '\n')
559 count++;
560 if (*(p + 1) == '\r')
561 p++;
563 else if (*p == '\r')
565 count++;
566 if (*(p + 1) == '\n')
567 p++;
571 return count;
574 char *
575 svn_cstring_join(apr_array_header_t *strings,
576 const char *separator,
577 apr_pool_t *pool)
579 svn_stringbuf_t *new_str = svn_stringbuf_create("", pool);
580 int sep_len = strlen(separator);
581 int i;
583 for (i = 0; i < strings->nelts; i++)
585 const char *string = APR_ARRAY_IDX(strings, i, const char *);
586 svn_stringbuf_appendbytes(new_str, string, strlen(string));
587 svn_stringbuf_appendbytes(new_str, separator, sep_len);
589 return new_str->data;
593 svn_cstring_casecmp(const char *str1, const char *str2)
595 for (;;)
597 const int a = *str1++;
598 const int b = *str2++;
599 const int cmp = svn_ctype_casecmp(a, b);
600 if (cmp || !a || !b)
601 return cmp;