1
0

str.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. * string.h
  3. */
  4. /**
  5. \ingroup lub
  6. \defgroup lub_string string
  7. @{
  8. \brief This utility provides some simple string manipulation functions which
  9. augment those found in the standard ANSI-C library.
  10. As a rule of thumb if a function returns "char *" then the calling client becomes responsible for invoking
  11. lub_string_free() to release the dynamically allocated memory.
  12. If a "const char *" is returned then the client has no responsiblity for releasing memory.
  13. */
  14. #ifndef _faux_str_h
  15. #define _faux_str_h
  16. #include <stddef.h>
  17. #include "faux/types.h"
  18. #define UTF8_MASK 0xC0
  19. #define UTF8_7BIT_MASK 0x80 /* One byte or multibyte */
  20. #define UTF8_11 0xC0 /* First UTF8 byte */
  21. #define UTF8_10 0x80 /* Next UTF8 bytes */
  22. C_DECL_BEGIN
  23. /**
  24. * This operation duplicates the specified string.
  25. *
  26. * \pre
  27. * - none
  28. *
  29. * \return
  30. * A dynamically allocated string containing the same content as that specified.
  31. *
  32. * \post
  33. * - The client is responsible for calling lub_string_free() with the
  34. * returned string when they are finished using it.
  35. */
  36. char *lub_string_dup(
  37. /**
  38. * The string to duplicate
  39. */
  40. const char *string);
  41. /**
  42. * This operation concatinates the specified text onto an existing string.
  43. *
  44. * \pre
  45. * - 'string_ptr' must contain reference to NULL or to a dynamically
  46. * allocated string.
  47. *
  48. * \post
  49. * - The old string referenced by 'string_ptr' will be automatically released
  50. * - 'string_ptr' will be updated to point to a dynamically allocated string
  51. * containing the concatinated text.
  52. * - If there is insufficient resource to extend the string then it will not
  53. * be extended.
  54. * - The client maintains responsibility for releasing the string reference
  55. * by string_ptr when they are finished using it.
  56. */
  57. void lub_string_cat(
  58. /**
  59. * A pointer to the string to concatinate
  60. */
  61. char **string_ptr,
  62. /**
  63. * The text to be appended
  64. */
  65. const char *text);
  66. /**
  67. * This operation concatinates a specified length of some text onto an
  68. * existing string.
  69. *
  70. * \pre
  71. * - 'string_ptr' must contain reference to NULL or to a dynamically allocated
  72. * string.
  73. *
  74. * \post
  75. * - The old string referenced by 'string_ptr' will be automatically
  76. * released.
  77. * - 'string_ptr' will be updated to point to a dynamically allocated
  78. * string containing the concatinated text.
  79. * - If there is insufficient resource to extend the string then it will not
  80. * be extended.
  81. * - If there length passed in is greater than that of the specified 'text'
  82. * then the length of the 'text' will be assumed.
  83. * - The client maintains responsibility for releasing the string reference
  84. * by string_ptr when they are finished using it.
  85. */
  86. void lub_string_catn(
  87. /**
  88. * A pointer to the string to concatinate
  89. */
  90. char **string_ptr,
  91. /**
  92. * The text to be appended
  93. */
  94. const char *text,
  95. /**
  96. * The length of text to be appended
  97. */
  98. size_t length);
  99. /**
  100. * This operation dupicates a specified length of some text into a
  101. * new string.
  102. *
  103. * \pre
  104. * - none
  105. *
  106. * \return
  107. * A dynamically allocated string containing the same content as that specified.
  108. *
  109. * \post
  110. * - The client is responsible for calling lub_string_free() with the
  111. * returned string when they are finished using it.
  112. */
  113. char *lub_string_dupn(
  114. /**
  115. * The string containing the text to duplicate
  116. */
  117. const char *string,
  118. /**
  119. * The length of text to be duplicated
  120. */
  121. unsigned length);
  122. /**
  123. * This operation returns a pointer to the last (space separated) word in the
  124. * specified string.
  125. *
  126. * \pre
  127. * - none
  128. *
  129. * \return
  130. * A pointer to the last word in the string.
  131. *
  132. * \post
  133. * - none
  134. */
  135. const char *lub_string_suffix(
  136. /**
  137. * The string from which to extract a suffix
  138. */
  139. const char *string);
  140. /**
  141. * This operation compares string cs to string ct in a case insensitive manner.
  142. *
  143. * \pre
  144. * - none
  145. *
  146. * \return
  147. * - < 0 if cs < ct
  148. * - 0 if cs == ct
  149. * - > 0 if cs > ct
  150. *
  151. * \post
  152. * - none
  153. */
  154. int lub_string_nocasecmp(
  155. /**
  156. * The first string for the comparison
  157. */
  158. const char *cs,
  159. /**
  160. * The second string for the comparison
  161. */
  162. const char *ct);
  163. /**
  164. * This operation performs a case insensitive search for a substring within
  165. * another string.
  166. *
  167. * \pre
  168. * - none
  169. *
  170. * \return
  171. * pointer to first occurance of a case insensitive version of the string ct,
  172. * or NULL if not present.
  173. *
  174. * \post
  175. * - none
  176. */
  177. const char *lub_string_nocasestr(
  178. /**
  179. * The string within which to find a substring
  180. */
  181. const char *cs,
  182. /**
  183. * The substring for which to search
  184. */
  185. const char *ct);
  186. /**
  187. * This operation releases the resources associated with a dynamically allocated
  188. * string.
  189. *
  190. * \pre
  191. * - The calling client must have responsibility for the passed string.
  192. *
  193. * \return
  194. * none
  195. *
  196. * \post
  197. * - The string is no longer usable, any references to it must be discarded.
  198. */
  199. void lub_string_free(
  200. /**
  201. * The string to be released
  202. */
  203. char *string);
  204. /*
  205. * These are the escape characters which are used by default when
  206. * expanding variables. These characters will be backslash escaped
  207. * to prevent them from being interpreted in a script.
  208. *
  209. * This is a security feature to prevent users from arbitarily setting
  210. * parameters to contain special sequences.
  211. */
  212. extern const char *lub_string_esc_default;
  213. extern const char *lub_string_esc_regex;
  214. extern const char *lub_string_esc_quoted;
  215. /**
  216. * This operation decode the escaped string.
  217. *
  218. * \pre
  219. * - none
  220. *
  221. * \return
  222. * - The allocated string without escapes.
  223. *
  224. * \post
  225. * - The result string must be freed after using.
  226. */
  227. char *lub_string_decode(const char *string);
  228. char *lub_string_ndecode(const char *string, unsigned int len);
  229. /**
  230. * This operation encode the string using escape.
  231. *
  232. * \pre
  233. * - none
  234. *
  235. * \return
  236. * - The allocated string with escapes.
  237. *
  238. * \post
  239. * - The result string must be freed after using.
  240. */
  241. char *lub_string_encode(const char *string, const char *escape_chars);
  242. char *lub_string_tolower(const char *str);
  243. unsigned int lub_string_equal_part(const char *str1, const char *str2,
  244. bool_t utf8);
  245. const char *lub_string_nextword(const char *string,
  246. size_t *len, size_t *offset, size_t *quoted);
  247. unsigned int lub_string_wordcount(const char *line);
  248. C_DECL_END
  249. #endif /* _faux_str_h */
  250. /** @} */