apr_cstr.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /* ====================================================================
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. * ====================================================================
  19. */
  20. /**
  21. * @file apr_cstr.h
  22. * @brief C string goodies.
  23. */
  24. #ifndef APR_CSTR_H
  25. #define APR_CSTR_H
  26. #include <apr.h> /* for apr_size_t */
  27. #include <apr_pools.h> /* for apr_pool_t */
  28. #include <apr_tables.h> /* for apr_array_header_t */
  29. #ifdef __cplusplus
  30. extern "C" {
  31. #endif /* __cplusplus */
  32. /**
  33. * @defgroup apr_cstr C (POSIX) locale string functions
  34. * @ingroup apr_strings
  35. *
  36. * The apr_cstr_* functions provide traditional C char * string text handling,
  37. * and notabilty they treat all text in the C (a.k.a. POSIX) locale using the
  38. * minimal POSIX character set, represented in either ASCII or a corresponding
  39. * EBCDIC subset.
  40. *
  41. * Character values outside of that set are treated as opaque bytes, and all
  42. * multi-byte character sequences are handled as individual distinct octets.
  43. *
  44. * Multi-byte characters sequences whose octets fall in the ASCII range cause
  45. * unexpected results, such as in the ISO-2022-JP code page where ASCII octets
  46. * occur within both shift-state and multibyte sequences.
  47. *
  48. * In the case of the UTF-8 encoding, all multibyte characters all fall outside
  49. * of the C/POSIX range of characters, so these functions are generally safe
  50. * to use on UTF-8 strings. The programmer must be aware that each octet may
  51. * not represent a distinct printable character in such encodings.
  52. *
  53. * The standard C99/POSIX string functions, rather than apr_cstr, should be
  54. * used in all cases where the current locale and encoding of the text is
  55. * significant.
  56. * @{
  57. */
  58. /** Divide @a input into substrings, interpreting any char from @a sep
  59. * as a token separator.
  60. *
  61. * Return an array of copies of those substrings (plain const char*),
  62. * allocating both the array and the copies in @a pool.
  63. *
  64. * None of the elements added to the array contain any of the
  65. * characters in @a sep_chars, and none of the new elements are empty
  66. * (thus, it is possible that the returned array will have length
  67. * zero).
  68. *
  69. * If @a chop_whitespace is TRUE, then remove leading and trailing
  70. * whitespace from the returned strings.
  71. *
  72. * @since New in 1.6
  73. */
  74. APR_DECLARE(apr_array_header_t *) apr_cstr_split(const char *input,
  75. const char *sep_chars,
  76. int chop_whitespace,
  77. apr_pool_t *pool);
  78. /** Like apr_cstr_split(), but append to existing @a array instead of
  79. * creating a new one. Allocate the copied substrings in @a pool
  80. * (i.e., caller decides whether or not to pass @a array->pool as @a pool).
  81. *
  82. * @since New in 1.6
  83. */
  84. APR_DECLARE(void) apr_cstr_split_append(apr_array_header_t *array,
  85. const char *input,
  86. const char *sep_chars,
  87. int chop_whitespace,
  88. apr_pool_t *pool);
  89. /** Return @c TRUE iff @a str matches any of the elements of @a list, a list
  90. * of zero or more glob patterns.
  91. *
  92. * @since New in 1.6
  93. */
  94. APR_DECLARE(int) apr_cstr_match_glob_list(const char *str,
  95. const apr_array_header_t *list);
  96. /** Return @c TRUE iff @a str exactly matches any of the elements of @a list.
  97. *
  98. * @since New in 1.6
  99. */
  100. APR_DECLARE(int) apr_cstr_match_list(const char *str,
  101. const apr_array_header_t *list);
  102. /**
  103. * Get the next token from @a *str interpreting any char from @a sep as a
  104. * token separator. Separators at the beginning of @a str will be skipped.
  105. * Returns a pointer to the beginning of the first token in @a *str or NULL
  106. * if no token is left. Modifies @a str such that the next call will return
  107. * the next token.
  108. *
  109. * @note The content of @a *str may be modified by this function.
  110. *
  111. * @since New in 1.6.
  112. */
  113. APR_DECLARE(char *) apr_cstr_tokenize(const char *sep, char **str);
  114. /**
  115. * Return the number of line breaks in @a msg, allowing any kind of newline
  116. * termination (CR, LF, CRLF, or LFCR), even inconsistent.
  117. *
  118. * @since New in 1.6.
  119. */
  120. APR_DECLARE(int) apr_cstr_count_newlines(const char *msg);
  121. #if 0 /* XXX: stringbuf logic is not present in APR */
  122. /**
  123. * Return a cstring which is the concatenation of @a strings (an array
  124. * of char *) each followed by @a separator (that is, @a separator
  125. * will also end the resulting string). Allocate the result in @a pool.
  126. * If @a strings is empty, then return the empty string.
  127. *
  128. * @since New in 1.6.
  129. */
  130. APR_DECLARE(char *) apr_cstr_join(const apr_array_header_t *strings,
  131. const char *separator,
  132. apr_pool_t *pool);
  133. #endif
  134. /**
  135. * Perform a case-insensitive comparison of two strings @a atr1 and @a atr2,
  136. * treating upper and lower case values of the 26 standard C/POSIX alphabetic
  137. * characters as equivalent. Extended latin characters outside of this set
  138. * are treated as unique octets, irrespective of the current locale.
  139. *
  140. * Returns in integer greater than, equal to, or less than 0,
  141. * according to whether @a str1 is considered greater than, equal to,
  142. * or less than @a str2.
  143. *
  144. * @since New in 1.6.
  145. */
  146. APR_DECLARE(int) apr_cstr_casecmp(const char *str1, const char *str2);
  147. /**
  148. * Perform a case-insensitive comparison of two strings @a atr1 and @a atr2,
  149. * treating upper and lower case values of the 26 standard C/POSIX alphabetic
  150. * characters as equivalent. Extended latin characters outside of this set
  151. * are treated as unique octets, irrespective of the current locale.
  152. *
  153. * Returns in integer greater than, equal to, or less than 0,
  154. * according to whether @a str1 is considered greater than, equal to,
  155. * or less than @a str2.
  156. *
  157. * @since New in 1.6.
  158. */
  159. APR_DECLARE(int) apr_cstr_casecmpn(const char *str1,
  160. const char *str2,
  161. apr_size_t n);
  162. /**
  163. * Parse the C string @a str into a 64 bit number, and return it in @a *n.
  164. * Assume that the number is represented in base @a base.
  165. * Raise an error if conversion fails (e.g. due to overflow), or if the
  166. * converted number is smaller than @a minval or larger than @a maxval.
  167. *
  168. * Leading whitespace in @a str is skipped in a locale-dependent way.
  169. * After that, the string may contain an optional '+' (positive, default)
  170. * or '-' (negative) character, followed by an optional '0x' prefix if
  171. * @a base is 0 or 16, followed by numeric digits appropriate for the base.
  172. * If there are any more characters after the numeric digits, an error is
  173. * returned.
  174. *
  175. * If @a base is zero, then a leading '0x' or '0X' prefix means hexadecimal,
  176. * else a leading '0' means octal (implemented, though not documented, in
  177. * apr_strtoi64() in APR 0.9.0 through 1.5.0), else use base ten.
  178. *
  179. * @since New in 1.6.
  180. */
  181. APR_DECLARE(apr_status_t) apr_cstr_strtoi64(apr_int64_t *n, const char *str,
  182. apr_int64_t minval,
  183. apr_int64_t maxval,
  184. int base);
  185. /**
  186. * Parse the C string @a str into a 64 bit number, and return it in @a *n.
  187. * Assume that the number is represented in base 10.
  188. * Raise an error if conversion fails (e.g. due to overflow).
  189. *
  190. * The behaviour otherwise is as described for apr_cstr_strtoi64().
  191. *
  192. * @since New in 1.6.
  193. */
  194. APR_DECLARE(apr_status_t) apr_cstr_atoi64(apr_int64_t *n, const char *str);
  195. /**
  196. * Parse the C string @a str into a 32 bit number, and return it in @a *n.
  197. * Assume that the number is represented in base 10.
  198. * Raise an error if conversion fails (e.g. due to overflow).
  199. *
  200. * The behaviour otherwise is as described for apr_cstr_strtoi64().
  201. *
  202. * @since New in 1.6.
  203. */
  204. APR_DECLARE(apr_status_t) apr_cstr_atoi(int *n, const char *str);
  205. /**
  206. * Parse the C string @a str into an unsigned 64 bit number, and return
  207. * it in @a *n. Assume that the number is represented in base @a base.
  208. * Raise an error if conversion fails (e.g. due to overflow), or if the
  209. * converted number is smaller than @a minval or larger than @a maxval.
  210. *
  211. * Leading whitespace in @a str is skipped in a locale-dependent way.
  212. * After that, the string may contain an optional '+' (positive, default)
  213. * or '-' (negative) character, followed by an optional '0x' prefix if
  214. * @a base is 0 or 16, followed by numeric digits appropriate for the base.
  215. * If there are any more characters after the numeric digits, an error is
  216. * returned.
  217. *
  218. * If @a base is zero, then a leading '0x' or '0X' prefix means hexadecimal,
  219. * else a leading '0' means octal (as implemented, though not documented, in
  220. * apr_strtoi64(), else use base ten.
  221. *
  222. * @warning The implementation returns APR_ERANGE if the parsed number
  223. * is greater than APR_INT64_MAX, even if it is not greater than @a maxval.
  224. *
  225. * @since New in 1.6.
  226. */
  227. APR_DECLARE(apr_status_t) apr_cstr_strtoui64(apr_uint64_t *n, const char *str,
  228. apr_uint64_t minval,
  229. apr_uint64_t maxval,
  230. int base);
  231. /**
  232. * Parse the C string @a str into an unsigned 64 bit number, and return
  233. * it in @a *n. Assume that the number is represented in base 10.
  234. * Raise an error if conversion fails (e.g. due to overflow).
  235. *
  236. * The behaviour otherwise is as described for apr_cstr_strtoui64(),
  237. * including the upper limit of APR_INT64_MAX.
  238. *
  239. * @since New in 1.6.
  240. */
  241. APR_DECLARE(apr_status_t) apr_cstr_atoui64(apr_uint64_t *n, const char *str);
  242. /**
  243. * Parse the C string @a str into an unsigned 32 bit number, and return
  244. * it in @a *n. Assume that the number is represented in base 10.
  245. * Raise an error if conversion fails (e.g. due to overflow).
  246. *
  247. * The behaviour otherwise is as described for apr_cstr_strtoui64(),
  248. * including the upper limit of APR_INT64_MAX.
  249. *
  250. * @since New in 1.6.
  251. */
  252. APR_DECLARE(apr_status_t) apr_cstr_atoui(unsigned int *n, const char *str);
  253. /**
  254. * Skip the common prefix @a prefix from the C string @a str, and return
  255. * a pointer to the next character after the prefix.
  256. * Return @c NULL if @a str does not start with @a prefix.
  257. *
  258. * @since New in 1.6.
  259. */
  260. APR_DECLARE(const char *) apr_cstr_skip_prefix(const char *str,
  261. const char *prefix);
  262. /** @} */
  263. #ifdef __cplusplus
  264. }
  265. #endif /* __cplusplus */
  266. #endif /* SVN_STRING_H */