svn_string.h

Go to the documentation of this file.
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 svn_string_t *
00145 svn_string_ncreate(const char *bytes, apr_size_t size, apr_pool_t *pool);
00146 
00147 /** Create a new string copied from the stringbuf @a strbuf.
00148  */
00149 svn_string_t *
00150 svn_string_create_from_buf(const svn_stringbuf_t *strbuf, apr_pool_t *pool);
00151 
00152 /** Create a new string by printf-style formatting using @a fmt and the
00153  * variable arguments, which are as appropriate for apr_psprintf().
00154  */
00155 svn_string_t *
00156 svn_string_createf(apr_pool_t *pool, const char *fmt, ...)
00157   __attribute__((format(printf, 2, 3)));
00158 
00159 /** Create a new string by printf-style formatting using @c fmt and @a ap.
00160  * This is the same as svn_string_createf() except for the different
00161  * way of passing the variable arguments.
00162  */
00163 svn_string_t *
00164 svn_string_createv(apr_pool_t *pool, const char *fmt, va_list ap)
00165   __attribute__((format(printf, 2, 0)));
00166 
00167 /** Return TRUE if @a str is empty (has length zero). */
00168 svn_boolean_t
00169 svn_string_isempty(const svn_string_t *str);
00170 
00171 /** Return a duplicate of @a original_string. */
00172 svn_string_t *
00173 svn_string_dup(const svn_string_t *original_string, apr_pool_t *pool);
00174 
00175 /** Return @c TRUE iff @a str1 and @a str2 have identical length and data. */
00176 svn_boolean_t
00177 svn_string_compare(const svn_string_t *str1, const svn_string_t *str2);
00178 
00179 /** Return offset of first non-whitespace character in @a str, or return
00180  * @a str->len if none.
00181  */
00182 apr_size_t
00183 svn_string_first_non_whitespace(const svn_string_t *str);
00184 
00185 /** Return position of last occurrence of @a ch in @a str, or return
00186  * @a str->len if no occurrence.
00187  */
00188 apr_size_t
00189 svn_string_find_char_backward(const svn_string_t *str, char ch);
00190 
00191 /** @} */
00192 
00193 
00194 /**
00195  * @defgroup svn_string_svn_stringbuf_t svn_stringbuf_t functions
00196  * @{
00197  */
00198 
00199 /** Create a new stringbuf copied from the null-terminated C string
00200  * @a cstring.
00201  */
00202 svn_stringbuf_t *
00203 svn_stringbuf_create(const char *cstring, apr_pool_t *pool);
00204 
00205 /** Create a new stringbuf copied from the generic string of bytes, @a bytes,
00206  * of length @a size bytes.  @a bytes is NOT assumed to be null-terminated,
00207  * but the new stringbuf will be.
00208  */
00209 svn_stringbuf_t *
00210 svn_stringbuf_ncreate(const char *bytes, apr_size_t size, apr_pool_t *pool);
00211 
00212 /** Create a new, empty stringbuf.
00213  *
00214  * @since New in 1.8.
00215  */
00216 svn_stringbuf_t *
00217 svn_stringbuf_create_empty(apr_pool_t *pool);
00218 
00219 /** Create a new, empty stringbuf with at least @a minimum_size bytes of
00220  * space available in the memory block.
00221  *
00222  * The allocated string buffer will be at least one byte larger than
00223  * @a minimum_size to account for a final '\\0'.
00224  *
00225  * @since New in 1.6.
00226  */
00227 svn_stringbuf_t *
00228 svn_stringbuf_create_ensure(apr_size_t minimum_size, apr_pool_t *pool);
00229 
00230 /** Create a new stringbuf copied from the string @a str.
00231  */
00232 svn_stringbuf_t *
00233 svn_stringbuf_create_from_string(const svn_string_t *str, apr_pool_t *pool);
00234 
00235 /** Create a new stringbuf using the given @a str as initial buffer.
00236  * Allocate the result in @a pool.  In contrast to #svn_stringbuf_create,
00237  * the contents of @a str may change when the stringbuf gets modified.
00238  *
00239  * @since New in 1.9
00240  */
00241 svn_stringbuf_t *
00242 svn_stringbuf_create_wrap(char *str, apr_pool_t *pool);
00243 
00244 /** Create a new stringbuf by printf-style formatting using @a fmt and the
00245  * variable arguments, which are as appropriate for apr_psprintf().
00246  */
00247 svn_stringbuf_t *
00248 svn_stringbuf_createf(apr_pool_t *pool, const char *fmt, ...)
00249   __attribute__((format(printf, 2, 3)));
00250 
00251 /** Create a new stringbuf by printf-style formatting using @c fmt and @a ap.
00252  * This is the same as svn_stringbuf_createf() except for the different
00253  * way of passing the variable arguments.
00254  */
00255 svn_stringbuf_t *
00256 svn_stringbuf_createv(apr_pool_t *pool, const char *fmt, va_list ap)
00257   __attribute__((format(printf, 2, 0)));
00258 
00259 /** Make sure that @a str has at least @a minimum_size
00260  * bytes of space available in the memory block.
00261  *
00262  * The allocated string buffer will be at least one byte larger than
00263  * @a minimum_size to account for a final '\\0'.
00264  *
00265  * @note: Before Subversion 1.8 this function did not ensure space for
00266  * one byte more than @a minimum_size.  If compatibility with pre-1.8
00267  * behaviour is required callers must assume space for only
00268  * @a minimum_size-1 data bytes plus a final '\\0'.
00269  */
00270 void
00271 svn_stringbuf_ensure(svn_stringbuf_t *str, apr_size_t minimum_size);
00272 
00273 /** Set @a str to a copy of the null-terminated C string @a value. */
00274 void
00275 svn_stringbuf_set(svn_stringbuf_t *str, const char *value);
00276 
00277 /** Set @a str to empty (zero length). */
00278 void
00279 svn_stringbuf_setempty(svn_stringbuf_t *str);
00280 
00281 /** Return @c TRUE if @a str is empty (has length zero). */
00282 svn_boolean_t
00283 svn_stringbuf_isempty(const svn_stringbuf_t *str);
00284 
00285 /** Chop @a nbytes bytes off end of @a str, but not more than @a str->len. */
00286 void
00287 svn_stringbuf_chop(svn_stringbuf_t *str, apr_size_t nbytes);
00288 
00289 /** Fill @a str with character @a c. */
00290 void
00291 svn_stringbuf_fillchar(svn_stringbuf_t *str, unsigned char c);
00292 
00293 /** Append the single character @a byte onto @a targetstr.
00294  *
00295  * This is an optimized version of svn_stringbuf_appendbytes()
00296  * that is much faster to call and execute. Gains vary with the ABI.
00297  * The advantages extend beyond the actual call because the reduced
00298  * register pressure allows for more optimization within the caller.
00299  *
00300  * reallocs if necessary. @a targetstr is affected, nothing else is.
00301  * @since New in 1.7.
00302  */
00303 void
00304 svn_stringbuf_appendbyte(svn_stringbuf_t *targetstr,
00305                          char byte);
00306 
00307 /** Append an array of bytes onto @a targetstr.
00308  *
00309  * reallocs if necessary. @a targetstr is affected, nothing else is.
00310  */
00311 void
00312 svn_stringbuf_appendbytes(svn_stringbuf_t *targetstr,
00313                           const char *bytes,
00314                           apr_size_t count);
00315 
00316 /** Append @a byte @a count times onto @a targetstr.
00317  *
00318  * reallocs if necessary. @a targetstr is affected, nothing else is.
00319  * @since New in 1.9.
00320  */
00321 void
00322 svn_stringbuf_appendfill(svn_stringbuf_t *targetstr,
00323                          char byte,
00324                          apr_size_t count);
00325 
00326 /** Append the stringbuf @c appendstr onto @a targetstr.
00327  *
00328  * reallocs if necessary. @a targetstr is affected, nothing else is.
00329  */
00330 void
00331 svn_stringbuf_appendstr(svn_stringbuf_t *targetstr,
00332                         const svn_stringbuf_t *appendstr);
00333 
00334 /** Append the C string @a cstr onto @a targetstr.
00335  *
00336  * reallocs if necessary. @a targetstr is affected, nothing else is.
00337  */
00338 void
00339 svn_stringbuf_appendcstr(svn_stringbuf_t *targetstr,
00340                          const char *cstr);
00341 
00342 /** Read @a count bytes from @a bytes and insert them into @a str at
00343  * position @a pos and following.  The resulting string will be
00344  * @c count+str->len bytes long.  If @c pos is larger or equal to the
00345  * number of bytes currently used in @a str,  simply append @a bytes.
00346  *
00347  * Reallocs if necessary. @a str is affected, nothing else is.
00348  *
00349  * @note The inserted string may be a sub-range if @a str.
00350  *
00351  * @since New in 1.8.
00352  */
00353 void
00354 svn_stringbuf_insert(svn_stringbuf_t *str,
00355                      apr_size_t pos,
00356                      const char *bytes,
00357                      apr_size_t count);
00358 
00359 /** Removes @a count bytes from @a str, starting at position @a pos.
00360  * If that range exceeds the current string data,  @a str gets truncated
00361  * at @a pos.  If the latter is larger or equal to @c str->pos, this will
00362  * be a no-op.  Otherwise, the resulting string will be @c str->len-count
00363  * bytes long.
00364  *
00365  * @since New in 1.8.
00366  */
00367 void
00368 svn_stringbuf_remove(svn_stringbuf_t *str,
00369                      apr_size_t pos,
00370                      apr_size_t count);
00371 
00372 /** Replace in @a str the substring which starts at @a pos and is @a
00373  * old_count bytes long with a new substring @a bytes (which is @a
00374  * new_count bytes long).
00375  *
00376  * This is faster but functionally equivalent to the following sequence:
00377  * @code
00378      svn_stringbuf_remove(str, pos, old_count);
00379      svn_stringbuf_insert(str, pos, bytes, new_count);
00380  * @endcode
00381  *
00382  * @since New in 1.8.
00383  */
00384 void
00385 svn_stringbuf_replace(svn_stringbuf_t *str,
00386                       apr_size_t pos,
00387                       apr_size_t old_count,
00388                       const char *bytes,
00389                       apr_size_t new_count);
00390 
00391 /** Return a duplicate of @a original_string. */
00392 svn_stringbuf_t *
00393 svn_stringbuf_dup(const svn_stringbuf_t *original_string, apr_pool_t *pool);
00394 
00395 /** Return @c TRUE iff @a str1 and @a str2 have identical length and data. */
00396 svn_boolean_t
00397 svn_stringbuf_compare(const svn_stringbuf_t *str1,
00398                       const svn_stringbuf_t *str2);
00399 
00400 /** Return offset of first non-whitespace character in @a str, or return
00401  * @a str->len if none.
00402  */
00403 apr_size_t
00404 svn_stringbuf_first_non_whitespace(const svn_stringbuf_t *str);
00405 
00406 /** Strip whitespace from both sides of @a str (modified in place). */
00407 void
00408 svn_stringbuf_strip_whitespace(svn_stringbuf_t *str);
00409 
00410 /** Return position of last occurrence of @a ch in @a str, or return
00411  * @a str->len if no occurrence.
00412  */
00413 apr_size_t
00414 svn_stringbuf_find_char_backward(const svn_stringbuf_t *str, char ch);
00415 
00416 /** Return @c TRUE iff @a str1 and @a str2 have identical length and data. */
00417 svn_boolean_t
00418 svn_string_compare_stringbuf(const svn_string_t *str1,
00419                              const svn_stringbuf_t *str2);
00420 
00421 /** @} */
00422 
00423 
00424 /**
00425  * @defgroup svn_string_cstrings C string functions
00426  * @{
00427  */
00428 
00429 /** Divide @a input into substrings, interpreting any char from @a sep
00430  * as a token separator.  
00431  *
00432  * Return an array of copies of those substrings (plain const char*),
00433  * allocating both the array and the copies in @a pool.
00434  *
00435  * None of the elements added to the array contain any of the
00436  * characters in @a sep_chars, and none of the new elements are empty
00437  * (thus, it is possible that the returned array will have length
00438  * zero).
00439  *
00440  * If @a chop_whitespace is TRUE, then remove leading and trailing
00441  * whitespace from the returned strings.
00442  */
00443 apr_array_header_t *
00444 svn_cstring_split(const char *input,
00445                   const char *sep_chars,
00446                   svn_boolean_t chop_whitespace,
00447                   apr_pool_t *pool);
00448 
00449 /** Like svn_cstring_split(), but append to existing @a array instead of
00450  * creating a new one.  Allocate the copied substrings in @a pool
00451  * (i.e., caller decides whether or not to pass @a array->pool as @a pool).
00452  */
00453 void
00454 svn_cstring_split_append(apr_array_header_t *array,
00455                          const char *input,
00456                          const char *sep_chars,
00457                          svn_boolean_t chop_whitespace,
00458                          apr_pool_t *pool);
00459 
00460 
00461 /** Return @c TRUE iff @a str matches any of the elements of @a list, a list
00462  * of zero or more glob patterns.
00463  */
00464 svn_boolean_t
00465 svn_cstring_match_glob_list(const char *str, const apr_array_header_t *list);
00466 
00467 /** Return @c TRUE iff @a str exactly matches any of the elements of @a list.
00468  *
00469  * @since new in 1.7
00470  */
00471 svn_boolean_t
00472 svn_cstring_match_list(const char *str, const apr_array_header_t *list);
00473 
00474 /**
00475  * Get the next token from @a *str interpreting any char from @a sep as a
00476  * token separator.  Separators at the beginning of @a str will be skipped.
00477  * Returns a pointer to the beginning of the first token in @a *str or NULL
00478  * if no token is left.  Modifies @a str such that the next call will return
00479  * the next token.
00480  *
00481  * @note The content of @a *str may be modified by this function.
00482  *
00483  * @since New in 1.8.
00484  */
00485 char *
00486 svn_cstring_tokenize(const char *sep, char **str);
00487 
00488 /**
00489  * Return the number of line breaks in @a msg, allowing any kind of newline
00490  * termination (CR, LF, CRLF, or LFCR), even inconsistent.
00491  *
00492  * @since New in 1.2.
00493  */
00494 int
00495 svn_cstring_count_newlines(const char *msg);
00496 
00497 /**
00498  * Return a cstring which is the concatenation of @a strings (an array
00499  * of char *) each followed by @a separator (that is, @a separator
00500  * will also end the resulting string).  Allocate the result in @a pool.
00501  * If @a strings is empty, then return the empty string.
00502  *
00503  * @since New in 1.2.
00504  */
00505 char *
00506 svn_cstring_join(const apr_array_header_t *strings,
00507                  const char *separator,
00508                  apr_pool_t *pool);
00509 
00510 /**
00511  * Compare two strings @a atr1 and @a atr2, treating case-equivalent
00512  * unaccented Latin (ASCII subset) letters as equal.
00513  *
00514  * Returns in integer greater than, equal to, or less than 0,
00515  * according to whether @a str1 is considered greater than, equal to,
00516  * or less than @a str2.
00517  *
00518  * @since New in 1.5.
00519  */
00520 int
00521 svn_cstring_casecmp(const char *str1, const char *str2);
00522 
00523 /**
00524  * Parse the C string @a str into a 64 bit number, and return it in @a *n.
00525  * Assume that the number is represented in base @a base.
00526  * Raise an error if conversion fails (e.g. due to overflow), or if the
00527  * converted number is smaller than @a minval or larger than @a maxval.
00528  *
00529  * @since New in 1.7.
00530  */
00531 svn_error_t *
00532 svn_cstring_strtoi64(apr_int64_t *n, const char *str,
00533                      apr_int64_t minval, apr_int64_t maxval,
00534                      int base);
00535 
00536 /**
00537  * Parse the C string @a str into a 64 bit number, and return it in @a *n.
00538  * Assume that the number is represented in base 10.
00539  * Raise an error if conversion fails (e.g. due to overflow).
00540  *
00541  * @since New in 1.7.
00542  */
00543 svn_error_t *
00544 svn_cstring_atoi64(apr_int64_t *n, const char *str);
00545 
00546 /**
00547  * Parse the C string @a str into a 32 bit number, and return it in @a *n.
00548  * Assume that the number is represented in base 10.
00549  * Raise an error if conversion fails (e.g. due to overflow).
00550  *
00551  * @since New in 1.7.
00552  */
00553 svn_error_t *
00554 svn_cstring_atoi(int *n, const char *str);
00555 
00556 /**
00557  * Parse the C string @a str into an unsigned 64 bit number, and return
00558  * it in @a *n. Assume that the number is represented in base @a base.
00559  * Raise an error if conversion fails (e.g. due to overflow), or if the
00560  * converted number is smaller than @a minval or larger than @a maxval.
00561  *
00562  * @since New in 1.7.
00563  */
00564 svn_error_t *
00565 svn_cstring_strtoui64(apr_uint64_t *n, const char *str,
00566                       apr_uint64_t minval, apr_uint64_t maxval,
00567                       int base);
00568 
00569 /**
00570  * Parse the C string @a str into an unsigned 64 bit number, and return
00571  * it in @a *n. Assume that the number is represented in base 10.
00572  * Raise an error if conversion fails (e.g. due to overflow).
00573  *
00574  * @since New in 1.7.
00575  */
00576 svn_error_t *
00577 svn_cstring_atoui64(apr_uint64_t *n, const char *str);
00578 
00579 /**
00580  * Parse the C string @a str into an unsigned 32 bit number, and return
00581  * it in @a *n. Assume that the number is represented in base 10.
00582  * Raise an error if conversion fails (e.g. due to overflow).
00583  *
00584  * @since New in 1.7.
00585  */
00586 svn_error_t *
00587 svn_cstring_atoui(unsigned int *n, const char *str);
00588 
00589 /** @} */
00590 
00591 /** @} */
00592 
00593 
00594 #ifdef __cplusplus
00595 }
00596 #endif /* __cplusplus */
00597 
00598 #endif  /* SVN_STRING_H */

Generated on Mon Mar 17 15:45:52 2014 for Subversion by  doxygen 1.4.7