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() */
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. */
35 my__realloc(char *data
, apr_size_t oldsize
, apr_size_t request
,
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
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. */
58 static APR_INLINE svn_boolean_t
59 string_compare(const char *str1
,
68 /* now the strings must have identical lenghths */
70 if ((memcmp(str1
, str2
, len1
)) == 0)
76 static APR_INLINE apr_size_t
77 string_first_non_whitespace(const char *str
, apr_size_t len
)
81 for (i
= 0; i
< len
; i
++)
83 if (! apr_isspace(str
[i
]))
87 /* if we get here, then the string must be entirely whitespace */
91 static APR_INLINE apr_size_t
92 find_char_backward(const char *str
, apr_size_t len
, char ch
)
102 /* char was not found, return len */
107 /* svn_string functions */
109 static svn_string_t
*
110 create_string(const char *data
, apr_size_t size
,
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
;
124 svn_string_ncreate(const char *bytes
, apr_size_t size
, apr_pool_t
*pool
)
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! */
136 /* wrap an svn_string_t around the new data */
137 return create_string(data
, size
, pool
);
142 svn_string_create(const char *cstring
, apr_pool_t
*pool
)
144 return svn_string_ncreate(cstring
, strlen(cstring
), pool
);
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
);
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
);
166 svn_string_createf(apr_pool_t
*pool
, const char *fmt
, ...)
172 str
= svn_string_createv(pool
, fmt
, ap
);
180 svn_string_isempty(const svn_string_t
*str
)
182 return (str
->len
== 0);
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
));
196 svn_string_compare(const svn_string_t
*str1
, const svn_string_t
*str2
)
199 string_compare(str1
->data
, str2
->data
, str1
->len
, str2
->len
);
205 svn_string_first_non_whitespace(const svn_string_t
*str
)
208 string_first_non_whitespace(str
->data
, str
->len
);
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_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
= size
+ 1; /* we know there is a null-term */
232 new_string
->pool
= pool
;
238 svn_stringbuf_ncreate(const char *bytes
, apr_size_t size
, apr_pool_t
*pool
)
242 data
= apr_palloc(pool
, size
+ 1);
243 memcpy(data
, bytes
, size
);
245 /* Null termination is the convention -- even if we suspect the data
246 to be binary, it's not up to us to decide, it's the caller's
247 call. Heck, that's why they call it the caller! */
250 /* wrap an svn_stringbuf_t around the new data */
251 return create_stringbuf(data
, size
, pool
);
256 svn_stringbuf_create(const char *cstring
, apr_pool_t
*pool
)
258 return svn_stringbuf_ncreate(cstring
, strlen(cstring
), pool
);
263 svn_stringbuf_create_from_string(const svn_string_t
*str
, apr_pool_t
*pool
)
265 return svn_stringbuf_ncreate(str
->data
, str
->len
, pool
);
270 svn_stringbuf_createv(apr_pool_t
*pool
, const char *fmt
, va_list ap
)
272 char *data
= apr_pvsprintf(pool
, fmt
, ap
);
274 /* wrap an svn_stringbuf_t around the new data */
275 return create_stringbuf(data
, strlen(data
), pool
);
280 svn_stringbuf_createf(apr_pool_t
*pool
, const char *fmt
, ...)
282 svn_stringbuf_t
*str
;
286 str
= svn_stringbuf_createv(pool
, fmt
, ap
);
294 svn_stringbuf_fillchar(svn_stringbuf_t
*str
, unsigned char c
)
296 memset(str
->data
, c
, str
->len
);
301 svn_stringbuf_set(svn_stringbuf_t
*str
, const char *value
)
303 apr_size_t amt
= strlen(value
);
305 svn_stringbuf_ensure(str
, amt
+ 1);
306 memcpy(str
->data
, value
, amt
+ 1);
311 svn_stringbuf_setempty(svn_stringbuf_t
*str
)
321 svn_stringbuf_chop(svn_stringbuf_t
*str
, apr_size_t nbytes
)
323 if (nbytes
> str
->len
)
328 str
->data
[str
->len
] = '\0';
333 svn_stringbuf_isempty(const svn_stringbuf_t
*str
)
335 return (str
->len
== 0);
340 svn_stringbuf_ensure(svn_stringbuf_t
*str
, apr_size_t minimum_size
)
342 /* Keep doubling capacity until have enough. */
343 if (str
->blocksize
< minimum_size
)
345 if (str
->blocksize
== 0)
346 str
->blocksize
= minimum_size
;
348 while (str
->blocksize
< minimum_size
)
350 apr_size_t prev_size
= str
->blocksize
;
352 /* check for apr_size_t overflow */
353 if (prev_size
> str
->blocksize
)
355 str
->blocksize
= minimum_size
;
360 str
->data
= (char *) my__realloc(str
->data
,
362 /* We need to maintain (and thus copy)
371 svn_stringbuf_appendbytes(svn_stringbuf_t
*str
, const char *bytes
,
374 apr_size_t total_len
;
377 total_len
= str
->len
+ count
; /* total size needed */
379 /* +1 for null terminator. */
380 svn_stringbuf_ensure(str
, (total_len
+ 1));
382 /* get address 1 byte beyond end of original bytestring */
383 start_address
= (str
->data
+ str
->len
);
385 memcpy(start_address
, bytes
, count
);
386 str
->len
= total_len
;
388 str
->data
[str
->len
] = '\0'; /* We don't know if this is binary
389 data or not, but convention is
390 to null-terminate. */
395 svn_stringbuf_appendstr(svn_stringbuf_t
*targetstr
,
396 const svn_stringbuf_t
*appendstr
)
398 svn_stringbuf_appendbytes(targetstr
, appendstr
->data
, appendstr
->len
);
403 svn_stringbuf_appendcstr(svn_stringbuf_t
*targetstr
, const char *cstr
)
405 svn_stringbuf_appendbytes(targetstr
, cstr
, strlen(cstr
));
412 svn_stringbuf_dup(const svn_stringbuf_t
*original_string
, apr_pool_t
*pool
)
414 return (svn_stringbuf_ncreate(original_string
->data
,
415 original_string
->len
, pool
));
421 svn_stringbuf_compare(const svn_stringbuf_t
*str1
,
422 const svn_stringbuf_t
*str2
)
424 return string_compare(str1
->data
, str2
->data
, str1
->len
, str2
->len
);
430 svn_stringbuf_first_non_whitespace(const svn_stringbuf_t
*str
)
432 return string_first_non_whitespace(str
->data
, str
->len
);
437 svn_stringbuf_strip_whitespace(svn_stringbuf_t
*str
)
439 /* Find first non-whitespace character */
440 apr_size_t offset
= svn_stringbuf_first_non_whitespace(str
);
442 /* Go ahead! Waste some RAM, we've got pools! :) */
445 str
->blocksize
-= offset
;
447 /* Now that we've trimmed the front, trim the end, wasting more RAM. */
448 while ((str
->len
> 0) && apr_isspace(str
->data
[str
->len
- 1]))
450 str
->data
[str
->len
] = '\0';
455 svn_stringbuf_find_char_backward(const svn_stringbuf_t
*str
, char ch
)
457 return find_char_backward(str
->data
, str
->len
, ch
);
462 svn_string_compare_stringbuf(const svn_string_t
*str1
,
463 const svn_stringbuf_t
*str2
)
465 return string_compare(str1
->data
, str2
->data
, str1
->len
, str2
->len
);
470 /*** C string stuff. ***/
473 svn_cstring_split_append(apr_array_header_t
*array
,
475 const char *sep_chars
,
476 svn_boolean_t chop_whitespace
,
483 pats
= apr_pstrdup(pool
, input
); /* strtok wants non-const data */
484 p
= apr_strtok(pats
, sep_chars
, &last
);
490 while (apr_isspace(*p
))
494 char *e
= p
+ (strlen(p
) - 1);
495 while ((e
>= p
) && (apr_isspace(*e
)))
502 APR_ARRAY_PUSH(array
, const char *) = p
;
504 p
= apr_strtok(NULL
, sep_chars
, &last
);
512 svn_cstring_split(const char *input
,
513 const char *sep_chars
,
514 svn_boolean_t chop_whitespace
,
517 apr_array_header_t
*a
= apr_array_make(pool
, 5, sizeof(input
));
518 svn_cstring_split_append(a
, input
, sep_chars
, chop_whitespace
, pool
);
523 svn_boolean_t
svn_cstring_match_glob_list(const char *str
,
524 apr_array_header_t
*list
)
528 for (i
= 0; i
< list
->nelts
; i
++)
530 const char *this_pattern
= APR_ARRAY_IDX(list
, i
, char *);
532 if (apr_fnmatch(this_pattern
, str
, 0) == APR_SUCCESS
)
539 int svn_cstring_count_newlines(const char *msg
)
544 for (p
= msg
; *p
; p
++)
549 if (*(p
+ 1) == '\r')
555 if (*(p
+ 1) == '\n')
564 svn_cstring_join(apr_array_header_t
*strings
,
565 const char *separator
,
568 svn_stringbuf_t
*new_str
= svn_stringbuf_create("", pool
);
569 int sep_len
= strlen(separator
);
572 for (i
= 0; i
< strings
->nelts
; i
++)
574 const char *string
= APR_ARRAY_IDX(strings
, i
, const char *);
575 svn_stringbuf_appendbytes(new_str
, string
, strlen(string
));
576 svn_stringbuf_appendbytes(new_str
, separator
, sep_len
);
578 return new_str
->data
;
582 svn_cstring_casecmp(const char *str1
, const char *str2
)
586 const int a
= *str1
++;
587 const int b
= *str2
++;
588 const int cmp
= svn_ctype_casecmp(a
, b
);