gstrfuncs.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /* GLIB - Library of useful routines for C programming
  2. * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  16. */
  17. /*
  18. * Modified by the GLib Team and others 1997-2000. See the AUTHORS
  19. * file for a list of people on the GLib Team. See the ChangeLog
  20. * files for a list of changes. These files are distributed with
  21. * GLib at ftp://ftp.gtk.org/pub/gtk/.
  22. */
  23. #ifndef __G_STRFUNCS_H__
  24. #define __G_STRFUNCS_H__
  25. #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
  26. #error "Only <glib.h> can be included directly."
  27. #endif
  28. #include <stdarg.h>
  29. #include <glib/gmacros.h>
  30. #include <glib/gtypes.h>
  31. #include <glib/gerror.h>
  32. G_BEGIN_DECLS
  33. /* Functions like the ones in <ctype.h> that are not affected by locale. */
  34. typedef enum {
  35. G_ASCII_ALNUM = 1 << 0,
  36. G_ASCII_ALPHA = 1 << 1,
  37. G_ASCII_CNTRL = 1 << 2,
  38. G_ASCII_DIGIT = 1 << 3,
  39. G_ASCII_GRAPH = 1 << 4,
  40. G_ASCII_LOWER = 1 << 5,
  41. G_ASCII_PRINT = 1 << 6,
  42. G_ASCII_PUNCT = 1 << 7,
  43. G_ASCII_SPACE = 1 << 8,
  44. G_ASCII_UPPER = 1 << 9,
  45. G_ASCII_XDIGIT = 1 << 10
  46. } GAsciiType;
  47. GLIB_VAR const guint16 * const g_ascii_table;
  48. #define g_ascii_isalnum(c) \
  49. ((g_ascii_table[(guchar) (c)] & G_ASCII_ALNUM) != 0)
  50. #define g_ascii_isalpha(c) \
  51. ((g_ascii_table[(guchar) (c)] & G_ASCII_ALPHA) != 0)
  52. #define g_ascii_iscntrl(c) \
  53. ((g_ascii_table[(guchar) (c)] & G_ASCII_CNTRL) != 0)
  54. #define g_ascii_isdigit(c) \
  55. ((g_ascii_table[(guchar) (c)] & G_ASCII_DIGIT) != 0)
  56. #define g_ascii_isgraph(c) \
  57. ((g_ascii_table[(guchar) (c)] & G_ASCII_GRAPH) != 0)
  58. #define g_ascii_islower(c) \
  59. ((g_ascii_table[(guchar) (c)] & G_ASCII_LOWER) != 0)
  60. #define g_ascii_isprint(c) \
  61. ((g_ascii_table[(guchar) (c)] & G_ASCII_PRINT) != 0)
  62. #define g_ascii_ispunct(c) \
  63. ((g_ascii_table[(guchar) (c)] & G_ASCII_PUNCT) != 0)
  64. #define g_ascii_isspace(c) \
  65. ((g_ascii_table[(guchar) (c)] & G_ASCII_SPACE) != 0)
  66. #define g_ascii_isupper(c) \
  67. ((g_ascii_table[(guchar) (c)] & G_ASCII_UPPER) != 0)
  68. #define g_ascii_isxdigit(c) \
  69. ((g_ascii_table[(guchar) (c)] & G_ASCII_XDIGIT) != 0)
  70. GLIB_AVAILABLE_IN_ALL
  71. gchar g_ascii_tolower (gchar c) G_GNUC_CONST;
  72. GLIB_AVAILABLE_IN_ALL
  73. gchar g_ascii_toupper (gchar c) G_GNUC_CONST;
  74. GLIB_AVAILABLE_IN_ALL
  75. gint g_ascii_digit_value (gchar c) G_GNUC_CONST;
  76. GLIB_AVAILABLE_IN_ALL
  77. gint g_ascii_xdigit_value (gchar c) G_GNUC_CONST;
  78. /* String utility functions that modify a string argument or
  79. * return a constant string that must not be freed.
  80. */
  81. #define G_STR_DELIMITERS "_-|> <."
  82. GLIB_AVAILABLE_IN_ALL
  83. gchar* g_strdelimit (gchar *string,
  84. const gchar *delimiters,
  85. gchar new_delimiter);
  86. GLIB_AVAILABLE_IN_ALL
  87. gchar* g_strcanon (gchar *string,
  88. const gchar *valid_chars,
  89. gchar substitutor);
  90. GLIB_AVAILABLE_IN_ALL
  91. const gchar * g_strerror (gint errnum) G_GNUC_CONST;
  92. GLIB_AVAILABLE_IN_ALL
  93. const gchar * g_strsignal (gint signum) G_GNUC_CONST;
  94. GLIB_AVAILABLE_IN_ALL
  95. gchar * g_strreverse (gchar *string);
  96. GLIB_AVAILABLE_IN_ALL
  97. gsize g_strlcpy (gchar *dest,
  98. const gchar *src,
  99. gsize dest_size);
  100. GLIB_AVAILABLE_IN_ALL
  101. gsize g_strlcat (gchar *dest,
  102. const gchar *src,
  103. gsize dest_size);
  104. GLIB_AVAILABLE_IN_ALL
  105. gchar * g_strstr_len (const gchar *haystack,
  106. gssize haystack_len,
  107. const gchar *needle);
  108. GLIB_AVAILABLE_IN_ALL
  109. gchar * g_strrstr (const gchar *haystack,
  110. const gchar *needle);
  111. GLIB_AVAILABLE_IN_ALL
  112. gchar * g_strrstr_len (const gchar *haystack,
  113. gssize haystack_len,
  114. const gchar *needle);
  115. GLIB_AVAILABLE_IN_ALL
  116. gboolean g_str_has_suffix (const gchar *str,
  117. const gchar *suffix);
  118. GLIB_AVAILABLE_IN_ALL
  119. gboolean g_str_has_prefix (const gchar *str,
  120. const gchar *prefix);
  121. /* String to/from double conversion functions */
  122. GLIB_AVAILABLE_IN_ALL
  123. gdouble g_strtod (const gchar *nptr,
  124. gchar **endptr);
  125. GLIB_AVAILABLE_IN_ALL
  126. gdouble g_ascii_strtod (const gchar *nptr,
  127. gchar **endptr);
  128. GLIB_AVAILABLE_IN_ALL
  129. guint64 g_ascii_strtoull (const gchar *nptr,
  130. gchar **endptr,
  131. guint base);
  132. GLIB_AVAILABLE_IN_ALL
  133. gint64 g_ascii_strtoll (const gchar *nptr,
  134. gchar **endptr,
  135. guint base);
  136. /* 29 bytes should enough for all possible values that
  137. * g_ascii_dtostr can produce.
  138. * Then add 10 for good measure */
  139. #define G_ASCII_DTOSTR_BUF_SIZE (29 + 10)
  140. GLIB_AVAILABLE_IN_ALL
  141. gchar * g_ascii_dtostr (gchar *buffer,
  142. gint buf_len,
  143. gdouble d);
  144. GLIB_AVAILABLE_IN_ALL
  145. gchar * g_ascii_formatd (gchar *buffer,
  146. gint buf_len,
  147. const gchar *format,
  148. gdouble d);
  149. /* removes leading spaces */
  150. GLIB_AVAILABLE_IN_ALL
  151. gchar* g_strchug (gchar *string);
  152. /* removes trailing spaces */
  153. GLIB_AVAILABLE_IN_ALL
  154. gchar* g_strchomp (gchar *string);
  155. /* removes leading & trailing spaces */
  156. #define g_strstrip( string ) g_strchomp (g_strchug (string))
  157. GLIB_AVAILABLE_IN_ALL
  158. gint g_ascii_strcasecmp (const gchar *s1,
  159. const gchar *s2);
  160. GLIB_AVAILABLE_IN_ALL
  161. gint g_ascii_strncasecmp (const gchar *s1,
  162. const gchar *s2,
  163. gsize n);
  164. GLIB_AVAILABLE_IN_ALL
  165. gchar* g_ascii_strdown (const gchar *str,
  166. gssize len) G_GNUC_MALLOC;
  167. GLIB_AVAILABLE_IN_ALL
  168. gchar* g_ascii_strup (const gchar *str,
  169. gssize len) G_GNUC_MALLOC;
  170. GLIB_AVAILABLE_IN_2_40
  171. gboolean g_str_is_ascii (const gchar *str);
  172. GLIB_DEPRECATED
  173. gint g_strcasecmp (const gchar *s1,
  174. const gchar *s2);
  175. GLIB_DEPRECATED
  176. gint g_strncasecmp (const gchar *s1,
  177. const gchar *s2,
  178. guint n);
  179. GLIB_DEPRECATED
  180. gchar* g_strdown (gchar *string);
  181. GLIB_DEPRECATED
  182. gchar* g_strup (gchar *string);
  183. /* String utility functions that return a newly allocated string which
  184. * ought to be freed with g_free from the caller at some point.
  185. */
  186. GLIB_AVAILABLE_IN_ALL
  187. gchar* g_strdup (const gchar *str) G_GNUC_MALLOC;
  188. GLIB_AVAILABLE_IN_ALL
  189. gchar* g_strdup_printf (const gchar *format,
  190. ...) G_GNUC_PRINTF (1, 2) G_GNUC_MALLOC;
  191. GLIB_AVAILABLE_IN_ALL
  192. gchar* g_strdup_vprintf (const gchar *format,
  193. va_list args) G_GNUC_PRINTF(1, 0) G_GNUC_MALLOC;
  194. GLIB_AVAILABLE_IN_ALL
  195. gchar* g_strndup (const gchar *str,
  196. gsize n) G_GNUC_MALLOC;
  197. GLIB_AVAILABLE_IN_ALL
  198. gchar* g_strnfill (gsize length,
  199. gchar fill_char) G_GNUC_MALLOC;
  200. GLIB_AVAILABLE_IN_ALL
  201. gchar* g_strconcat (const gchar *string1,
  202. ...) G_GNUC_MALLOC G_GNUC_NULL_TERMINATED;
  203. GLIB_AVAILABLE_IN_ALL
  204. gchar* g_strjoin (const gchar *separator,
  205. ...) G_GNUC_MALLOC G_GNUC_NULL_TERMINATED;
  206. /* Make a copy of a string interpreting C string -style escape
  207. * sequences. Inverse of g_strescape. The recognized sequences are \b
  208. * \f \n \r \t \\ \" and the octal format.
  209. */
  210. GLIB_AVAILABLE_IN_ALL
  211. gchar* g_strcompress (const gchar *source) G_GNUC_MALLOC;
  212. /* Copy a string escaping nonprintable characters like in C strings.
  213. * Inverse of g_strcompress. The exceptions parameter, if non-NULL, points
  214. * to a string containing characters that are not to be escaped.
  215. *
  216. * Deprecated API: gchar* g_strescape (const gchar *source);
  217. * Luckily this function wasn't used much, using NULL as second parameter
  218. * provides mostly identical semantics.
  219. */
  220. GLIB_AVAILABLE_IN_ALL
  221. gchar* g_strescape (const gchar *source,
  222. const gchar *exceptions) G_GNUC_MALLOC;
  223. GLIB_AVAILABLE_IN_ALL
  224. gpointer g_memdup (gconstpointer mem,
  225. guint byte_size) G_GNUC_ALLOC_SIZE(2);
  226. /* NULL terminated string arrays.
  227. * g_strsplit(), g_strsplit_set() split up string into max_tokens tokens
  228. * at delim and return a newly allocated string array.
  229. * g_strjoinv() concatenates all of str_array's strings, sliding in an
  230. * optional separator, the returned string is newly allocated.
  231. * g_strfreev() frees the array itself and all of its strings.
  232. * g_strdupv() copies a NULL-terminated array of strings
  233. * g_strv_length() returns the length of a NULL-terminated array of strings
  234. */
  235. typedef gchar** GStrv;
  236. GLIB_AVAILABLE_IN_ALL
  237. gchar** g_strsplit (const gchar *string,
  238. const gchar *delimiter,
  239. gint max_tokens);
  240. GLIB_AVAILABLE_IN_ALL
  241. gchar ** g_strsplit_set (const gchar *string,
  242. const gchar *delimiters,
  243. gint max_tokens);
  244. GLIB_AVAILABLE_IN_ALL
  245. gchar* g_strjoinv (const gchar *separator,
  246. gchar **str_array) G_GNUC_MALLOC;
  247. GLIB_AVAILABLE_IN_ALL
  248. void g_strfreev (gchar **str_array);
  249. GLIB_AVAILABLE_IN_ALL
  250. gchar** g_strdupv (gchar **str_array);
  251. GLIB_AVAILABLE_IN_ALL
  252. guint g_strv_length (gchar **str_array);
  253. GLIB_AVAILABLE_IN_ALL
  254. gchar* g_stpcpy (gchar *dest,
  255. const char *src);
  256. GLIB_AVAILABLE_IN_2_40
  257. gchar * g_str_to_ascii (const gchar *str,
  258. const gchar *from_locale);
  259. GLIB_AVAILABLE_IN_2_40
  260. gchar ** g_str_tokenize_and_fold (const gchar *string,
  261. const gchar *translit_locale,
  262. gchar ***ascii_alternates);
  263. GLIB_AVAILABLE_IN_2_40
  264. gboolean g_str_match_string (const gchar *search_term,
  265. const gchar *potential_hit,
  266. gboolean accept_alternates);
  267. GLIB_AVAILABLE_IN_2_44
  268. gboolean g_strv_contains (const gchar * const *strv,
  269. const gchar *str);
  270. GLIB_AVAILABLE_IN_2_60
  271. gboolean g_strv_equal (const gchar * const *strv1,
  272. const gchar * const *strv2);
  273. /* Convenience ASCII string to number API */
  274. /**
  275. * GNumberParserError:
  276. * @G_NUMBER_PARSER_ERROR_INVALID: String was not a valid number.
  277. * @G_NUMBER_PARSER_ERROR_OUT_OF_BOUNDS: String was a number, but out of bounds.
  278. *
  279. * Error codes returned by functions converting a string to a number.
  280. *
  281. * Since: 2.54
  282. */
  283. typedef enum
  284. {
  285. G_NUMBER_PARSER_ERROR_INVALID,
  286. G_NUMBER_PARSER_ERROR_OUT_OF_BOUNDS,
  287. } GNumberParserError;
  288. /**
  289. * G_NUMBER_PARSER_ERROR:
  290. *
  291. * Domain for errors returned by functions converting a string to a
  292. * number.
  293. *
  294. * Since: 2.54
  295. */
  296. #define G_NUMBER_PARSER_ERROR (g_number_parser_error_quark ())
  297. GLIB_AVAILABLE_IN_2_54
  298. GQuark g_number_parser_error_quark (void);
  299. GLIB_AVAILABLE_IN_2_54
  300. gboolean g_ascii_string_to_signed (const gchar *str,
  301. guint base,
  302. gint64 min,
  303. gint64 max,
  304. gint64 *out_num,
  305. GError **error);
  306. GLIB_AVAILABLE_IN_2_54
  307. gboolean g_ascii_string_to_unsigned (const gchar *str,
  308. guint base,
  309. guint64 min,
  310. guint64 max,
  311. guint64 *out_num,
  312. GError **error);
  313. G_END_DECLS
  314. #endif /* __G_STRFUNCS_H__ */