00001 /** 00002 * @copyright 00003 * ==================================================================== 00004 * Licensed to the Apache Software Foundation (ASF) under one 00005 * or more contributor license agreements. See the NOTICE file 00006 * distributed with this work for additional information 00007 * regarding copyright ownership. The ASF licenses this file 00008 * to you under the Apache License, Version 2.0 (the 00009 * "License"); you may not use this file except in compliance 00010 * with the License. You may obtain a copy of the License at 00011 * 00012 * http://www.apache.org/licenses/LICENSE-2.0 00013 * 00014 * Unless required by applicable law or agreed to in writing, 00015 * software distributed under the License is distributed on an 00016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 00017 * KIND, either express or implied. See the License for the 00018 * specific language governing permissions and limitations 00019 * under the License. 00020 * ==================================================================== 00021 * @endcopyright 00022 * 00023 * @file svn_string.h 00024 * @brief Counted-length strings for Subversion, plus some C string goodies. 00025 * 00026 * There are two string datatypes: @c svn_string_t and @c svn_stringbuf_t. 00027 * The former is a simple pointer/length pair useful for passing around 00028 * strings (or arbitrary bytes) with a counted length. @c svn_stringbuf_t is 00029 * buffered to enable efficient appending of strings without an allocation 00030 * and copy for each append operation. 00031 * 00032 * @c svn_string_t contains a <tt>const char *</tt> for its data, so it is 00033 * most appropriate for constant data and for functions which expect constant, 00034 * counted data. Functions should generally use <tt>const @c svn_string_t 00035 * *</tt> as their parameter to indicate they are expecting a constant, 00036 * counted string. 00037 * 00038 * @c svn_stringbuf_t uses a plain <tt>char *</tt> for its data, so it is 00039 * most appropriate for modifiable data. 00040 * 00041 * <h3>Invariants</h3> 00042 * 00043 * 1. Null termination: 00044 * 00045 * Both structures maintain a significant invariant: 00046 * 00047 * <tt>s->data[s->len] == '\\0'</tt> 00048 * 00049 * The functions defined within this header file will maintain 00050 * the invariant (which does imply that memory is 00051 * allocated/defined as @c len+1 bytes). If code outside of the 00052 * @c svn_string.h functions manually builds these structures, 00053 * then they must enforce this invariant. 00054 * 00055 * Note that an @c svn_string(buf)_t may contain binary data, 00056 * which means that strlen(s->data) does not have to equal @c 00057 * s->len. The null terminator is provided to make it easier to 00058 * pass @c s->data to C string interfaces. 00059 * 00060 * 00061 * 2. Non-NULL input: 00062 * 00063 * All the functions assume their input data pointer is non-NULL, 00064 * unless otherwise documented, and may seg fault if passed 00065 * NULL. The input data may *contain* null bytes, of course, just 00066 * the data pointer itself must not be NULL. 00067 * 00068 * <h3>Memory allocation</h3> 00069 * 00070 * All the functions make a deep copy of all input data, and never store 00071 * a pointer to the original input data. 00072 */ 00073 00074 00075 #ifndef SVN_STRING_H 00076 #define SVN_STRING_H 00077 00078 #include <apr.h> /* for apr_size_t */ 00079 #include <apr_pools.h> /* for apr_pool_t */ 00080 #include <apr_tables.h> /* for apr_array_header_t */ 00081 00082 #include "svn_types.h" /* for svn_boolean_t, svn_error_t */ 00083 00084 #ifdef __cplusplus 00085 extern "C" { 00086 #endif /* __cplusplus */ 00087 00088 /** 00089 * @defgroup svn_string String handling 00090 * @{ 00091 */ 00092 00093 00094 00095 /** A simple counted string. */ 00096 typedef struct svn_string_t 00097 { 00098 const char *data; /**< pointer to the bytestring */ 00099 apr_size_t len; /**< length of bytestring */ 00100 } svn_string_t; 00101 00102 /** A buffered string, capable of appending without an allocation and copy 00103 * for each append. */ 00104 typedef struct svn_stringbuf_t 00105 { 00106 /** a pool from which this string was originally allocated, and is not 00107 * necessarily specific to this string. This is used only for allocating 00108 * more memory from when the string needs to grow. 00109 */ 00110 apr_pool_t *pool; 00111 00112 /** pointer to the bytestring */ 00113 char *data; 00114 00115 /** length of bytestring */ 00116 apr_size_t len; 00117 00118 /** total size of buffer allocated */ 00119 apr_size_t blocksize; 00120 } svn_stringbuf_t; 00121 00122 00123 /** 00124 * @defgroup svn_string_svn_string_t svn_string_t functions 00125 * @{ 00126 */ 00127 00128 /** Create a new string copied from the null-terminated C string @a cstring. 00129 */ 00130 svn_string_t * 00131 svn_string_create(const char *cstring, apr_pool_t *pool); 00132 00133 /** Create a new, empty string. 00134 * 00135 * @since New in 1.8. 00136 */ 00137 svn_string_t * 00138 svn_string_create_empty(apr_pool_t *pool); 00139 00140 /** Create a new string copied from a generic string of bytes, @a bytes, of 00141 * length @a size bytes. @a bytes is NOT assumed to be null-terminated, but 00142 * the new string will be. 00143 * 00144 * @since Since 1.9, @a bytes can be NULL if @a size is zero. 00145 */ 00146 svn_string_t * 00147 svn_string_ncreate(const char *bytes, apr_size_t size, apr_pool_t *pool); 00148 00149 /** Create a new string copied from the stringbuf @a strbuf. 00150 */ 00151 svn_string_t * 00152 svn_string_create_from_buf(const svn_stringbuf_t *strbuf, apr_pool_t *pool); 00153 00154 /** Create a new string by printf-style formatting using @a fmt and the 00155 * variable arguments, which are as appropriate for apr_psprintf(). 00156 */ 00157 svn_string_t * 00158 svn_string_createf(apr_pool_t *pool, const char *fmt, ...) 00159 __attribute__((format(printf, 2, 3))); 00160 00161 /** Create a new string by printf-style formatting using @c fmt and @a ap. 00162 * This is the same as svn_string_createf() except for the different 00163 * way of passing the variable arguments. 00164 */ 00165 svn_string_t * 00166 svn_string_createv(apr_pool_t *pool, const char *fmt, va_list ap) 00167 __attribute__((format(printf, 2, 0))); 00168 00169 /** Return TRUE if @a str is empty (has length zero). */ 00170 svn_boolean_t 00171 svn_string_isempty(const svn_string_t *str); 00172 00173 /** Return a duplicate of @a original_string. 00174 * 00175 * @since Since 1.9, @a original_string can be NULL in which case NULL will 00176 * be returned. 00177 */ 00178 svn_string_t * 00179 svn_string_dup(const svn_string_t *original_string, apr_pool_t *pool); 00180 00181 /** Return @c TRUE iff @a str1 and @a str2 have identical length and data. */ 00182 svn_boolean_t 00183 svn_string_compare(const svn_string_t *str1, const svn_string_t *str2); 00184 00185 /** Return offset of first non-whitespace character in @a str, or return 00186 * @a str->len if none. 00187 */ 00188 apr_size_t 00189 svn_string_first_non_whitespace(const svn_string_t *str); 00190 00191 /** Return position of last occurrence of @a ch in @a str, or return 00192 * @a str->len if no occurrence. 00193 */ 00194 apr_size_t 00195 svn_string_find_char_backward(const svn_string_t *str, char ch); 00196 00197 /** @} */ 00198 00199 00200 /** 00201 * @defgroup svn_string_svn_stringbuf_t svn_stringbuf_t functions 00202 * @{ 00203 */ 00204 00205 /** Create a new stringbuf copied from the null-terminated C string 00206 * @a cstring. 00207 */ 00208 svn_stringbuf_t * 00209 svn_stringbuf_create(const char *cstring, apr_pool_t *pool); 00210 00211 /** Create a new stringbuf copied from the generic string of bytes, @a bytes, 00212 * of length @a size bytes. @a bytes is NOT assumed to be null-terminated, 00213 * but the new stringbuf will be. 00214 * 00215 * @since Since 1.9, @a bytes can be NULL if @a size is zero. 00216 */ 00217 svn_stringbuf_t * 00218 svn_stringbuf_ncreate(const char *bytes, apr_size_t size, apr_pool_t *pool); 00219 00220 /** Create a new, empty stringbuf. 00221 * 00222 * @since New in 1.8. 00223 */ 00224 svn_stringbuf_t * 00225 svn_stringbuf_create_empty(apr_pool_t *pool); 00226 00227 /** Create a new, empty stringbuf with at least @a minimum_size bytes of 00228 * space available in the memory block. 00229 * 00230 * The allocated string buffer will be at least one byte larger than 00231 * @a minimum_size to account for a final '\\0'. 00232 * 00233 * @since New in 1.6. 00234 */ 00235 svn_stringbuf_t * 00236 svn_stringbuf_create_ensure(apr_size_t minimum_size, apr_pool_t *pool); 00237 00238 /** Create a new stringbuf copied from the string @a str. 00239 */ 00240 svn_stringbuf_t * 00241 svn_stringbuf_create_from_string(const svn_string_t *str, apr_pool_t *pool); 00242 00243 /** Create a new stringbuf using the given @a str as initial buffer. 00244 * Allocate the result in @a pool. In contrast to #svn_stringbuf_create, 00245 * the contents of @a str may change when the stringbuf gets modified. 00246 * 00247 * @since New in 1.9 00248 */ 00249 svn_stringbuf_t * 00250 svn_stringbuf_create_wrap(char *str, apr_pool_t *pool); 00251 00252 /** Create a new stringbuf by printf-style formatting using @a fmt and the 00253 * variable arguments, which are as appropriate for apr_psprintf(). 00254 */ 00255 svn_stringbuf_t * 00256 svn_stringbuf_createf(apr_pool_t *pool, const char *fmt, ...) 00257 __attribute__((format(printf, 2, 3))); 00258 00259 /** Create a new stringbuf by printf-style formatting using @c fmt and @a ap. 00260 * This is the same as svn_stringbuf_createf() except for the different 00261 * way of passing the variable arguments. 00262 */ 00263 svn_stringbuf_t * 00264 svn_stringbuf_createv(apr_pool_t *pool, const char *fmt, va_list ap) 00265 __attribute__((format(printf, 2, 0))); 00266 00267 /** Make sure that @a str has at least @a minimum_size 00268 * bytes of space available in the memory block. 00269 * 00270 * The allocated string buffer will be at least one byte larger than 00271 * @a minimum_size to account for a final '\\0'. 00272 * 00273 * @note: Before Subversion 1.8 this function did not ensure space for 00274 * one byte more than @a minimum_size. If compatibility with pre-1.8 00275 * behaviour is required callers must assume space for only 00276 * @a minimum_size-1 data bytes plus a final '\\0'. 00277 */ 00278 void 00279 svn_stringbuf_ensure(svn_stringbuf_t *str, apr_size_t minimum_size); 00280 00281 /** Set @a str to a copy of the null-terminated C string @a value. */ 00282 void 00283 svn_stringbuf_set(svn_stringbuf_t *str, const char *value); 00284 00285 /** Set @a str to empty (zero length). */ 00286 void 00287 svn_stringbuf_setempty(svn_stringbuf_t *str); 00288 00289 /** Return @c TRUE if @a str is empty (has length zero). */ 00290 svn_boolean_t 00291 svn_stringbuf_isempty(const svn_stringbuf_t *str); 00292 00293 /** Chop @a nbytes bytes off end of @a str, but not more than @a str->len. */ 00294 void 00295 svn_stringbuf_chop(svn_stringbuf_t *str, apr_size_t nbytes); 00296 00297 /** Fill @a str with character @a c. */ 00298 void 00299 svn_stringbuf_fillchar(svn_stringbuf_t *str, unsigned char c); 00300 00301 /** Append the single character @a byte onto @a targetstr. 00302 * 00303 * This is an optimized version of svn_stringbuf_appendbytes() 00304 * that is much faster to call and execute. Gains vary with the ABI. 00305 * The advantages extend beyond the actual call because the reduced 00306 * register pressure allows for more optimization within the caller. 00307 * 00308 * Reallocs if necessary. @a targetstr is affected, nothing else is. 00309 * @since New in 1.7. 00310 */ 00311 void 00312 svn_stringbuf_appendbyte(svn_stringbuf_t *targetstr, 00313 char byte); 00314 00315 /** Append the array of bytes @a bytes of length @a count onto @a targetstr. 00316 * 00317 * Reallocs if necessary. @a targetstr is affected, nothing else is. 00318 * 00319 * @since 1.9 @a bytes can be NULL if @a count is zero. 00320 */ 00321 void 00322 svn_stringbuf_appendbytes(svn_stringbuf_t *targetstr, 00323 const char *bytes, 00324 apr_size_t count); 00325 00326 /** Append @a byte @a count times onto @a targetstr. 00327 * 00328 * Reallocs if necessary. @a targetstr is affected, nothing else is. 00329 * @since New in 1.9. 00330 */ 00331 void 00332 svn_stringbuf_appendfill(svn_stringbuf_t *targetstr, 00333 char byte, 00334 apr_size_t count); 00335 00336 /** Append the stringbuf @c appendstr onto @a targetstr. 00337 * 00338 * Reallocs if necessary. @a targetstr is affected, nothing else is. 00339 */ 00340 void 00341 svn_stringbuf_appendstr(svn_stringbuf_t *targetstr, 00342 const svn_stringbuf_t *appendstr); 00343 00344 /** Append the C string @a cstr onto @a targetstr. 00345 * 00346 * Reallocs if necessary. @a targetstr is affected, nothing else is. 00347 */ 00348 void 00349 svn_stringbuf_appendcstr(svn_stringbuf_t *targetstr, 00350 const char *cstr); 00351 00352 /** Insert into @a str at position @a pos an array of bytes @a bytes 00353 * which is @a count bytes long. 00354 * 00355 * The resulting string will be @c count+str->len bytes long. If 00356 * @a pos is larger than or equal to @c str->len, simply append @a bytes. 00357 * 00358 * Reallocs if necessary. @a str is affected, nothing else is. 00359 * 00360 * @note The inserted string may be a sub-range of @a str. 00361 * 00362 * @since New in 1.8. 00363 * 00364 * @since Since 1.9, @a bytes can be NULL if @a count is zero. 00365 */ 00366 void 00367 svn_stringbuf_insert(svn_stringbuf_t *str, 00368 apr_size_t pos, 00369 const char *bytes, 00370 apr_size_t count); 00371 00372 /** Remove @a count bytes from @a str, starting at position @a pos. 00373 * 00374 * If that range exceeds the current string data, truncate @a str at 00375 * @a pos. If @a pos is larger than or equal to @c str->len, this will 00376 * be a no-op. Otherwise, the resulting string will be @c str->len-count 00377 * bytes long. 00378 * 00379 * @since New in 1.8. 00380 */ 00381 void 00382 svn_stringbuf_remove(svn_stringbuf_t *str, 00383 apr_size_t pos, 00384 apr_size_t count); 00385 00386 /** Replace in @a str the substring which starts at @a pos and is @a 00387 * old_count bytes long with a new substring @a bytes which is @a 00388 * new_count bytes long. 00389 * 00390 * This is faster but functionally equivalent to the following sequence: 00391 * @code 00392 svn_stringbuf_remove(str, pos, old_count); 00393 svn_stringbuf_insert(str, pos, bytes, new_count); 00394 * @endcode 00395 * 00396 * @since New in 1.8. 00397 * 00398 * @since Since 1.9, @a bytes can be NULL if @a new_count is zero. 00399 */ 00400 void 00401 svn_stringbuf_replace(svn_stringbuf_t *str, 00402 apr_size_t pos, 00403 apr_size_t old_count, 00404 const char *bytes, 00405 apr_size_t new_count); 00406 00407 /** Return a duplicate of @a original_string. */ 00408 svn_stringbuf_t * 00409 svn_stringbuf_dup(const svn_stringbuf_t *original_string, apr_pool_t *pool); 00410 00411 /** Return @c TRUE iff @a str1 and @a str2 have identical length and data. */ 00412 svn_boolean_t 00413 svn_stringbuf_compare(const svn_stringbuf_t *str1, 00414 const svn_stringbuf_t *str2); 00415 00416 /** Return offset of first non-whitespace character in @a str, or return 00417 * @a str->len if none. 00418 */ 00419 apr_size_t 00420 svn_stringbuf_first_non_whitespace(const svn_stringbuf_t *str); 00421 00422 /** Strip whitespace from both sides of @a str (modified in place). */ 00423 void 00424 svn_stringbuf_strip_whitespace(svn_stringbuf_t *str); 00425 00426 /** Return position of last occurrence of @a ch in @a str, or return 00427 * @a str->len if no occurrence. 00428 */ 00429 apr_size_t 00430 svn_stringbuf_find_char_backward(const svn_stringbuf_t *str, char ch); 00431 00432 /** Return @c TRUE iff @a str1 and @a str2 have identical length and data. */ 00433 svn_boolean_t 00434 svn_string_compare_stringbuf(const svn_string_t *str1, 00435 const svn_stringbuf_t *str2); 00436 00437 /** @} */ 00438 00439 00440 /** 00441 * @defgroup svn_string_cstrings C string functions 00442 * @{ 00443 */ 00444 00445 /** Divide @a input into substrings, interpreting any char from @a sep 00446 * as a token separator. 00447 * 00448 * Return an array of copies of those substrings (plain const char*), 00449 * allocating both the array and the copies in @a pool. 00450 * 00451 * None of the elements added to the array contain any of the 00452 * characters in @a sep_chars, and none of the new elements are empty 00453 * (thus, it is possible that the returned array will have length 00454 * zero). 00455 * 00456 * If @a chop_whitespace is TRUE, then remove leading and trailing 00457 * whitespace from the returned strings. 00458 */ 00459 apr_array_header_t * 00460 svn_cstring_split(const char *input, 00461 const char *sep_chars, 00462 svn_boolean_t chop_whitespace, 00463 apr_pool_t *pool); 00464 00465 /** Like svn_cstring_split(), but append to existing @a array instead of 00466 * creating a new one. Allocate the copied substrings in @a pool 00467 * (i.e., caller decides whether or not to pass @a array->pool as @a pool). 00468 */ 00469 void 00470 svn_cstring_split_append(apr_array_header_t *array, 00471 const char *input, 00472 const char *sep_chars, 00473 svn_boolean_t chop_whitespace, 00474 apr_pool_t *pool); 00475 00476 00477 /** Return @c TRUE iff @a str matches any of the elements of @a list, a list 00478 * of zero or more glob patterns. 00479 */ 00480 svn_boolean_t 00481 svn_cstring_match_glob_list(const char *str, const apr_array_header_t *list); 00482 00483 /** Return @c TRUE iff @a str exactly matches any of the elements of @a list. 00484 * 00485 * @since new in 1.7 00486 */ 00487 svn_boolean_t 00488 svn_cstring_match_list(const char *str, const apr_array_header_t *list); 00489 00490 /** 00491 * Get the next token from @a *str interpreting any char from @a sep as a 00492 * token separator. Separators at the beginning of @a str will be skipped. 00493 * Returns a pointer to the beginning of the first token in @a *str or NULL 00494 * if no token is left. Modifies @a str such that the next call will return 00495 * the next token. 00496 * 00497 * @note The content of @a *str may be modified by this function. 00498 * 00499 * @since New in 1.8. 00500 */ 00501 char * 00502 svn_cstring_tokenize(const char *sep, char **str); 00503 00504 /** 00505 * Return the number of line breaks in @a msg, allowing any kind of newline 00506 * termination (CR, LF, CRLF, or LFCR), even inconsistent. 00507 * 00508 * @since New in 1.2. 00509 */ 00510 int 00511 svn_cstring_count_newlines(const char *msg); 00512 00513 /** 00514 * Return a cstring which is the concatenation of @a strings (an array 00515 * of char *) each followed by @a separator (that is, @a separator 00516 * will also end the resulting string). Allocate the result in @a pool. 00517 * If @a strings is empty, then return the empty string. 00518 * 00519 * @since New in 1.2. 00520 */ 00521 char * 00522 svn_cstring_join(const apr_array_header_t *strings, 00523 const char *separator, 00524 apr_pool_t *pool); 00525 00526 /** 00527 * Compare two strings @a atr1 and @a atr2, treating case-equivalent 00528 * unaccented Latin (ASCII subset) letters as equal. 00529 * 00530 * Returns in integer greater than, equal to, or less than 0, 00531 * according to whether @a str1 is considered greater than, equal to, 00532 * or less than @a str2. 00533 * 00534 * @since New in 1.5. 00535 */ 00536 int 00537 svn_cstring_casecmp(const char *str1, const char *str2); 00538 00539 /** 00540 * Parse the C string @a str into a 64 bit number, and return it in @a *n. 00541 * Assume that the number is represented in base @a base. 00542 * Raise an error if conversion fails (e.g. due to overflow), or if the 00543 * converted number is smaller than @a minval or larger than @a maxval. 00544 * Leading whitespace in @a str is skipped in a locale-dependent way. 00545 * 00546 * @since New in 1.7. 00547 */ 00548 svn_error_t * 00549 svn_cstring_strtoi64(apr_int64_t *n, const char *str, 00550 apr_int64_t minval, apr_int64_t maxval, 00551 int base); 00552 00553 /** 00554 * Parse the C string @a str into a 64 bit number, and return it in @a *n. 00555 * Assume that the number is represented in base 10. 00556 * Raise an error if conversion fails (e.g. due to overflow). 00557 * Leading whitespace in @a str is skipped in a locale-dependent way. 00558 * 00559 * @since New in 1.7. 00560 */ 00561 svn_error_t * 00562 svn_cstring_atoi64(apr_int64_t *n, const char *str); 00563 00564 /** 00565 * Parse the C string @a str into a 32 bit number, and return it in @a *n. 00566 * Assume that the number is represented in base 10. 00567 * Raise an error if conversion fails (e.g. due to overflow). 00568 * Leading whitespace in @a str is skipped in a locale-dependent way. 00569 * 00570 * @since New in 1.7. 00571 */ 00572 svn_error_t * 00573 svn_cstring_atoi(int *n, const char *str); 00574 00575 /** 00576 * Parse the C string @a str into an unsigned 64 bit number, and return 00577 * it in @a *n. Assume that the number is represented in base @a base. 00578 * Raise an error if conversion fails (e.g. due to overflow), or if the 00579 * converted number is smaller than @a minval or larger than @a maxval. 00580 * Leading whitespace in @a str is skipped in a locale-dependent way. 00581 * 00582 * @since New in 1.7. 00583 */ 00584 svn_error_t * 00585 svn_cstring_strtoui64(apr_uint64_t *n, const char *str, 00586 apr_uint64_t minval, apr_uint64_t maxval, 00587 int base); 00588 00589 /** 00590 * Parse the C string @a str into an unsigned 64 bit number, and return 00591 * it in @a *n. Assume that the number is represented in base 10. 00592 * Raise an error if conversion fails (e.g. due to overflow). 00593 * Leading whitespace in @a str is skipped in a locale-dependent way. 00594 * 00595 * @since New in 1.7. 00596 */ 00597 svn_error_t * 00598 svn_cstring_atoui64(apr_uint64_t *n, const char *str); 00599 00600 /** 00601 * Parse the C string @a str into an unsigned 32 bit number, and return 00602 * it in @a *n. Assume that the number is represented in base 10. 00603 * Raise an error if conversion fails (e.g. due to overflow). 00604 * Leading whitespace in @a str is skipped in a locale-dependent way. 00605 * 00606 * @since New in 1.7. 00607 */ 00608 svn_error_t * 00609 svn_cstring_atoui(unsigned int *n, const char *str); 00610 00611 /** 00612 * Skip the common prefix @a prefix from the C string @a str, and return 00613 * a pointer to the next character after the prefix. 00614 * Return @c NULL if @a str does not start with @a prefix. 00615 * 00616 * @since New in 1.9. 00617 */ 00618 const char * 00619 svn_cstring_skip_prefix(const char *str, const char *prefix); 00620 00621 /** @} */ 00622 00623 /** @} */ 00624 00625 00626 #ifdef __cplusplus 00627 } 00628 #endif /* __cplusplus */ 00629 00630 #endif /* SVN_STRING_H */
1.6.1