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_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
;
238 svn_stringbuf_create_ensure(apr_size_t blocksize
, apr_pool_t
*pool
)
240 char *data
= apr_palloc(pool
, blocksize
);
244 /* wrap an svn_stringbuf_t around the new data buffer. */
245 return create_stringbuf(data
, 0, blocksize
, pool
);
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';
266 svn_stringbuf_create(const char *cstring
, apr_pool_t
*pool
)
268 return svn_stringbuf_ncreate(cstring
, strlen(cstring
), pool
);
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
);
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
);
291 svn_stringbuf_createf(apr_pool_t
*pool
, const char *fmt
, ...)
293 svn_stringbuf_t
*str
;
297 str
= svn_stringbuf_createv(pool
, fmt
, ap
);
305 svn_stringbuf_fillchar(svn_stringbuf_t
*str
, unsigned char c
)
307 memset(str
->data
, c
, str
->len
);
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);
322 svn_stringbuf_setempty(svn_stringbuf_t
*str
)
332 svn_stringbuf_chop(svn_stringbuf_t
*str
, apr_size_t nbytes
)
334 if (nbytes
> str
->len
)
339 str
->data
[str
->len
] = '\0';
344 svn_stringbuf_isempty(const svn_stringbuf_t
*str
)
346 return (str
->len
== 0);
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
;
359 while (str
->blocksize
< minimum_size
)
361 apr_size_t prev_size
= str
->blocksize
;
363 /* check for apr_size_t overflow */
364 if (prev_size
> str
->blocksize
)
366 str
->blocksize
= minimum_size
;
371 str
->data
= (char *) my__realloc(str
->data
,
373 /* We need to maintain (and thus copy)
382 svn_stringbuf_appendbytes(svn_stringbuf_t
*str
, const char *bytes
,
385 apr_size_t total_len
;
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. */
406 svn_stringbuf_appendstr(svn_stringbuf_t
*targetstr
,
407 const svn_stringbuf_t
*appendstr
)
409 svn_stringbuf_appendbytes(targetstr
, appendstr
->data
, appendstr
->len
);
414 svn_stringbuf_appendcstr(svn_stringbuf_t
*targetstr
, const char *cstr
)
416 svn_stringbuf_appendbytes(targetstr
, cstr
, strlen(cstr
));
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
));
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
);
441 svn_stringbuf_first_non_whitespace(const svn_stringbuf_t
*str
)
443 return string_first_non_whitespace(str
->data
, str
->len
);
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! :) */
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]))
461 str
->data
[str
->len
] = '\0';
466 svn_stringbuf_find_char_backward(const svn_stringbuf_t
*str
, char ch
)
468 return find_char_backward(str
->data
, str
->len
, ch
);
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. ***/
484 svn_cstring_split_append(apr_array_header_t
*array
,
486 const char *sep_chars
,
487 svn_boolean_t chop_whitespace
,
494 pats
= apr_pstrdup(pool
, input
); /* strtok wants non-const data */
495 p
= apr_strtok(pats
, sep_chars
, &last
);
501 while (apr_isspace(*p
))
505 char *e
= p
+ (strlen(p
) - 1);
506 while ((e
>= p
) && (apr_isspace(*e
)))
513 APR_ARRAY_PUSH(array
, const char *) = p
;
515 p
= apr_strtok(NULL
, sep_chars
, &last
);
523 svn_cstring_split(const char *input
,
524 const char *sep_chars
,
525 svn_boolean_t chop_whitespace
,
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
);
534 svn_boolean_t
svn_cstring_match_glob_list(const char *str
,
535 apr_array_header_t
*list
)
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
)
550 int svn_cstring_count_newlines(const char *msg
)
555 for (p
= msg
; *p
; p
++)
560 if (*(p
+ 1) == '\r')
566 if (*(p
+ 1) == '\n')
575 svn_cstring_join(apr_array_header_t
*strings
,
576 const char *separator
,
579 svn_stringbuf_t
*new_str
= svn_stringbuf_create("", pool
);
580 int sep_len
= strlen(separator
);
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
)
597 const int a
= *str1
++;
598 const int b
= *str2
++;
599 const int cmp
= svn_ctype_casecmp(a
, b
);