gstrfuncs.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. /* GLIB - Library of useful routines for C programming
  2. * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
  3. *
  4. * SPDX-License-Identifier: LGPL-2.1-or-later
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /*
  20. * Modified by the GLib Team and others 1997-2000. See the AUTHORS
  21. * file for a list of people on the GLib Team. See the ChangeLog
  22. * files for a list of changes. These files are distributed with
  23. * GLib at ftp://ftp.gtk.org/pub/gtk/.
  24. */
  25. #ifndef __G_STRFUNCS_H__
  26. #define __G_STRFUNCS_H__
  27. #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
  28. #error "Only <glib.h> can be included directly."
  29. #endif
  30. #include <stdarg.h>
  31. #include <string.h>
  32. #include <glib/gmacros.h>
  33. #include <glib/gtypes.h>
  34. #include <glib/gerror.h>
  35. #include <glib/gmem.h>
  36. G_BEGIN_DECLS
  37. /* Functions like the ones in <ctype.h> that are not affected by locale. */
  38. typedef enum {
  39. G_ASCII_ALNUM = 1 << 0,
  40. G_ASCII_ALPHA = 1 << 1,
  41. G_ASCII_CNTRL = 1 << 2,
  42. G_ASCII_DIGIT = 1 << 3,
  43. G_ASCII_GRAPH = 1 << 4,
  44. G_ASCII_LOWER = 1 << 5,
  45. G_ASCII_PRINT = 1 << 6,
  46. G_ASCII_PUNCT = 1 << 7,
  47. G_ASCII_SPACE = 1 << 8,
  48. G_ASCII_UPPER = 1 << 9,
  49. G_ASCII_XDIGIT = 1 << 10
  50. } GAsciiType;
  51. GLIB_VAR const guint16 * const g_ascii_table;
  52. #define g_ascii_isalnum(c) \
  53. ((g_ascii_table[(guchar) (c)] & G_ASCII_ALNUM) != 0)
  54. #define g_ascii_isalpha(c) \
  55. ((g_ascii_table[(guchar) (c)] & G_ASCII_ALPHA) != 0)
  56. #define g_ascii_iscntrl(c) \
  57. ((g_ascii_table[(guchar) (c)] & G_ASCII_CNTRL) != 0)
  58. #define g_ascii_isdigit(c) \
  59. ((g_ascii_table[(guchar) (c)] & G_ASCII_DIGIT) != 0)
  60. #define g_ascii_isgraph(c) \
  61. ((g_ascii_table[(guchar) (c)] & G_ASCII_GRAPH) != 0)
  62. #define g_ascii_islower(c) \
  63. ((g_ascii_table[(guchar) (c)] & G_ASCII_LOWER) != 0)
  64. #define g_ascii_isprint(c) \
  65. ((g_ascii_table[(guchar) (c)] & G_ASCII_PRINT) != 0)
  66. #define g_ascii_ispunct(c) \
  67. ((g_ascii_table[(guchar) (c)] & G_ASCII_PUNCT) != 0)
  68. #define g_ascii_isspace(c) \
  69. ((g_ascii_table[(guchar) (c)] & G_ASCII_SPACE) != 0)
  70. #define g_ascii_isupper(c) \
  71. ((g_ascii_table[(guchar) (c)] & G_ASCII_UPPER) != 0)
  72. #define g_ascii_isxdigit(c) \
  73. ((g_ascii_table[(guchar) (c)] & G_ASCII_XDIGIT) != 0)
  74. GLIB_AVAILABLE_IN_ALL
  75. gchar g_ascii_tolower (gchar c) G_GNUC_CONST;
  76. GLIB_AVAILABLE_IN_ALL
  77. gchar g_ascii_toupper (gchar c) G_GNUC_CONST;
  78. GLIB_AVAILABLE_IN_ALL
  79. gint g_ascii_digit_value (gchar c) G_GNUC_CONST;
  80. GLIB_AVAILABLE_IN_ALL
  81. gint g_ascii_xdigit_value (gchar c) G_GNUC_CONST;
  82. /* String utility functions that modify a string argument or
  83. * return a constant string that must not be freed.
  84. */
  85. #define G_STR_DELIMITERS "_-|> <."
  86. GLIB_AVAILABLE_IN_ALL
  87. gchar* g_strdelimit (gchar *string,
  88. const gchar *delimiters,
  89. gchar new_delimiter);
  90. GLIB_AVAILABLE_IN_ALL
  91. gchar* g_strcanon (gchar *string,
  92. const gchar *valid_chars,
  93. gchar substitutor);
  94. GLIB_AVAILABLE_IN_ALL
  95. const gchar * g_strerror (gint errnum) G_GNUC_CONST;
  96. GLIB_AVAILABLE_IN_ALL
  97. const gchar * g_strsignal (gint signum) G_GNUC_CONST;
  98. GLIB_AVAILABLE_IN_ALL
  99. gchar * g_strreverse (gchar *string);
  100. GLIB_AVAILABLE_IN_ALL
  101. gsize g_strlcpy (gchar *dest,
  102. const gchar *src,
  103. gsize dest_size);
  104. GLIB_AVAILABLE_IN_ALL
  105. gsize g_strlcat (gchar *dest,
  106. const gchar *src,
  107. gsize dest_size);
  108. GLIB_AVAILABLE_IN_ALL
  109. gchar * g_strstr_len (const gchar *haystack,
  110. gssize haystack_len,
  111. const gchar *needle);
  112. GLIB_AVAILABLE_IN_ALL
  113. gchar * g_strrstr (const gchar *haystack,
  114. const gchar *needle);
  115. GLIB_AVAILABLE_IN_ALL
  116. gchar * g_strrstr_len (const gchar *haystack,
  117. gssize haystack_len,
  118. const gchar *needle);
  119. GLIB_AVAILABLE_IN_ALL
  120. gboolean (g_str_has_suffix) (const gchar *str,
  121. const gchar *suffix);
  122. GLIB_AVAILABLE_IN_ALL
  123. gboolean (g_str_has_prefix) (const gchar *str,
  124. const gchar *prefix);
  125. #if G_GNUC_CHECK_VERSION (2, 0)
  126. #ifndef __GTK_DOC_IGNORE__
  127. #ifndef __GI_SCANNER__
  128. /* This macro is defeat a false -Wnonnull warning in GCC.
  129. * Without it, it thinks strlen and memcmp may be getting passed NULL
  130. * despite the explicit check for NULL right above the calls.
  131. */
  132. #define _G_STR_NONNULL(x) ((x) + !(x))
  133. #define g_str_has_prefix(STR, PREFIX) \
  134. (__builtin_constant_p (PREFIX)? \
  135. G_GNUC_EXTENSION ({ \
  136. const char * const __str = (STR); \
  137. const char * const __prefix = (PREFIX); \
  138. gboolean __result = FALSE; \
  139. \
  140. if G_UNLIKELY (__str == NULL || __prefix == NULL) \
  141. __result = (g_str_has_prefix) (__str, __prefix); \
  142. else \
  143. { \
  144. const size_t __str_len = strlen (_G_STR_NONNULL (__str)); \
  145. const size_t __prefix_len = strlen (_G_STR_NONNULL (__prefix)); \
  146. if (__str_len >= __prefix_len) \
  147. __result = memcmp (_G_STR_NONNULL (__str), \
  148. _G_STR_NONNULL (__prefix), \
  149. __prefix_len) == 0; \
  150. } \
  151. __result; \
  152. }) \
  153. : \
  154. (g_str_has_prefix) (STR, PREFIX) \
  155. )
  156. #define g_str_has_suffix(STR, SUFFIX) \
  157. (__builtin_constant_p (SUFFIX)? \
  158. G_GNUC_EXTENSION ({ \
  159. const char * const __str = (STR); \
  160. const char * const __suffix = (SUFFIX); \
  161. gboolean __result = FALSE; \
  162. \
  163. if G_UNLIKELY (__str == NULL || __suffix == NULL) \
  164. __result = (g_str_has_suffix) (__str, __suffix); \
  165. else \
  166. { \
  167. const size_t __str_len = strlen (_G_STR_NONNULL (__str)); \
  168. const size_t __suffix_len = strlen (_G_STR_NONNULL (__suffix)); \
  169. if (__str_len >= __suffix_len) \
  170. __result = memcmp (__str + __str_len - __suffix_len, \
  171. _G_STR_NONNULL (__suffix), \
  172. __suffix_len) == 0; \
  173. } \
  174. __result; \
  175. }) \
  176. : \
  177. (g_str_has_suffix) (STR, SUFFIX) \
  178. )
  179. #endif /* !defined (__GI_SCANNER__) */
  180. #endif /* !defined (__GTK_DOC_IGNORE__) */
  181. #endif /* G_GNUC_CHECK_VERSION (2, 0) */
  182. /* String to/from double conversion functions */
  183. GLIB_AVAILABLE_IN_ALL
  184. gdouble g_strtod (const gchar *nptr,
  185. gchar **endptr);
  186. GLIB_AVAILABLE_IN_ALL
  187. gdouble g_ascii_strtod (const gchar *nptr,
  188. gchar **endptr);
  189. GLIB_AVAILABLE_IN_ALL
  190. guint64 g_ascii_strtoull (const gchar *nptr,
  191. gchar **endptr,
  192. guint base);
  193. GLIB_AVAILABLE_IN_ALL
  194. gint64 g_ascii_strtoll (const gchar *nptr,
  195. gchar **endptr,
  196. guint base);
  197. /* 29 bytes should enough for all possible values that
  198. * g_ascii_dtostr can produce.
  199. * Then add 10 for good measure */
  200. #define G_ASCII_DTOSTR_BUF_SIZE (29 + 10)
  201. GLIB_AVAILABLE_IN_ALL
  202. gchar * g_ascii_dtostr (gchar *buffer,
  203. gint buf_len,
  204. gdouble d);
  205. GLIB_AVAILABLE_IN_ALL
  206. gchar * g_ascii_formatd (gchar *buffer,
  207. gint buf_len,
  208. const gchar *format,
  209. gdouble d);
  210. /* removes leading spaces */
  211. GLIB_AVAILABLE_IN_ALL
  212. gchar* g_strchug (gchar *string);
  213. /* removes trailing spaces */
  214. GLIB_AVAILABLE_IN_ALL
  215. gchar* g_strchomp (gchar *string);
  216. /* removes leading & trailing spaces */
  217. #define g_strstrip( string ) g_strchomp (g_strchug (string))
  218. GLIB_AVAILABLE_IN_ALL
  219. gint g_ascii_strcasecmp (const gchar *s1,
  220. const gchar *s2);
  221. GLIB_AVAILABLE_IN_ALL
  222. gint g_ascii_strncasecmp (const gchar *s1,
  223. const gchar *s2,
  224. gsize n);
  225. GLIB_AVAILABLE_IN_ALL
  226. gchar* g_ascii_strdown (const gchar *str,
  227. gssize len) G_GNUC_MALLOC;
  228. GLIB_AVAILABLE_IN_ALL
  229. gchar* g_ascii_strup (const gchar *str,
  230. gssize len) G_GNUC_MALLOC;
  231. GLIB_AVAILABLE_IN_2_40
  232. gboolean g_str_is_ascii (const gchar *str);
  233. GLIB_DEPRECATED
  234. gint g_strcasecmp (const gchar *s1,
  235. const gchar *s2);
  236. GLIB_DEPRECATED
  237. gint g_strncasecmp (const gchar *s1,
  238. const gchar *s2,
  239. guint n);
  240. GLIB_DEPRECATED
  241. gchar* g_strdown (gchar *string);
  242. GLIB_DEPRECATED
  243. gchar* g_strup (gchar *string);
  244. /* String utility functions that return a newly allocated string which
  245. * ought to be freed with g_free from the caller at some point.
  246. */
  247. GLIB_AVAILABLE_IN_ALL
  248. gchar* (g_strdup) (const gchar *str) G_GNUC_MALLOC;
  249. GLIB_AVAILABLE_IN_ALL
  250. gchar* g_strdup_printf (const gchar *format,
  251. ...) G_GNUC_PRINTF (1, 2) G_GNUC_MALLOC;
  252. GLIB_AVAILABLE_IN_ALL
  253. gchar* g_strdup_vprintf (const gchar *format,
  254. va_list args) G_GNUC_PRINTF(1, 0) G_GNUC_MALLOC;
  255. GLIB_AVAILABLE_IN_ALL
  256. gchar* g_strndup (const gchar *str,
  257. gsize n) G_GNUC_MALLOC;
  258. GLIB_AVAILABLE_IN_ALL
  259. gchar* g_strnfill (gsize length,
  260. gchar fill_char) G_GNUC_MALLOC;
  261. GLIB_AVAILABLE_IN_ALL
  262. gchar* g_strconcat (const gchar *string1,
  263. ...) G_GNUC_MALLOC G_GNUC_NULL_TERMINATED;
  264. GLIB_AVAILABLE_IN_ALL
  265. gchar* g_strjoin (const gchar *separator,
  266. ...) G_GNUC_MALLOC G_GNUC_NULL_TERMINATED;
  267. #if G_GNUC_CHECK_VERSION(2, 0)
  268. #ifndef __GTK_DOC_IGNORE__
  269. #ifndef __GI_SCANNER__
  270. G_ALWAYS_INLINE static inline char *
  271. g_strdup_inline (const char *str)
  272. {
  273. if (__builtin_constant_p (!str) && !str)
  274. return NULL;
  275. if (__builtin_constant_p (!!str) && !!str && __builtin_constant_p (strlen (str)))
  276. {
  277. const size_t len = strlen (str) + 1;
  278. char *dup_str = (char *) g_malloc (len);
  279. return (char *) memcpy (dup_str, str, len);
  280. }
  281. return g_strdup (str);
  282. }
  283. #define g_strdup(x) g_strdup_inline (x)
  284. #endif /* !defined (__GI_SCANNER__) */
  285. #endif /* !defined (__GTK_DOC_IGNORE__) */
  286. #endif /* G_GNUC_CHECK_VERSION (2, 0) */
  287. GLIB_AVAILABLE_IN_ALL
  288. gchar* g_strcompress (const gchar *source) G_GNUC_MALLOC;
  289. GLIB_AVAILABLE_IN_ALL
  290. gchar* g_strescape (const gchar *source,
  291. const gchar *exceptions) G_GNUC_MALLOC;
  292. GLIB_DEPRECATED_IN_2_68_FOR (g_memdup2)
  293. gpointer g_memdup (gconstpointer mem,
  294. guint byte_size) G_GNUC_ALLOC_SIZE(2);
  295. GLIB_AVAILABLE_IN_2_68
  296. gpointer g_memdup2 (gconstpointer mem,
  297. gsize byte_size) G_GNUC_ALLOC_SIZE(2);
  298. /* NULL terminated string arrays.
  299. * g_strsplit(), g_strsplit_set() split up string into max_tokens tokens
  300. * at delim and return a newly allocated string array.
  301. * g_strjoinv() concatenates all of str_array's strings, sliding in an
  302. * optional separator, the returned string is newly allocated.
  303. * g_strfreev() frees the array itself and all of its strings.
  304. * g_strdupv() copies a NULL-terminated array of strings
  305. * g_strv_length() returns the length of a NULL-terminated array of strings
  306. */
  307. typedef gchar** GStrv;
  308. GLIB_AVAILABLE_IN_ALL
  309. gchar** g_strsplit (const gchar *string,
  310. const gchar *delimiter,
  311. gint max_tokens);
  312. GLIB_AVAILABLE_IN_ALL
  313. gchar ** g_strsplit_set (const gchar *string,
  314. const gchar *delimiters,
  315. gint max_tokens);
  316. GLIB_AVAILABLE_IN_ALL
  317. gchar* g_strjoinv (const gchar *separator,
  318. gchar **str_array) G_GNUC_MALLOC;
  319. GLIB_AVAILABLE_IN_ALL
  320. void g_strfreev (gchar **str_array);
  321. GLIB_AVAILABLE_IN_ALL
  322. gchar** g_strdupv (gchar **str_array);
  323. GLIB_AVAILABLE_IN_ALL
  324. guint g_strv_length (gchar **str_array);
  325. GLIB_AVAILABLE_IN_ALL
  326. gchar* g_stpcpy (gchar *dest,
  327. const char *src);
  328. GLIB_AVAILABLE_IN_2_40
  329. gchar * g_str_to_ascii (const gchar *str,
  330. const gchar *from_locale);
  331. GLIB_AVAILABLE_IN_2_40
  332. gchar ** g_str_tokenize_and_fold (const gchar *string,
  333. const gchar *translit_locale,
  334. gchar ***ascii_alternates);
  335. GLIB_AVAILABLE_IN_2_40
  336. gboolean g_str_match_string (const gchar *search_term,
  337. const gchar *potential_hit,
  338. gboolean accept_alternates);
  339. GLIB_AVAILABLE_IN_2_44
  340. gboolean g_strv_contains (const gchar * const *strv,
  341. const gchar *str);
  342. GLIB_AVAILABLE_IN_2_60
  343. gboolean g_strv_equal (const gchar * const *strv1,
  344. const gchar * const *strv2);
  345. /* Convenience ASCII string to number API */
  346. /**
  347. * GNumberParserError:
  348. * @G_NUMBER_PARSER_ERROR_INVALID: string was not a valid number
  349. * @G_NUMBER_PARSER_ERROR_OUT_OF_BOUNDS: string was a number, but out of bounds
  350. *
  351. * Error codes returned by functions converting a string to a number.
  352. *
  353. * Since: 2.54
  354. */
  355. typedef enum
  356. {
  357. G_NUMBER_PARSER_ERROR_INVALID,
  358. G_NUMBER_PARSER_ERROR_OUT_OF_BOUNDS,
  359. } GNumberParserError;
  360. /**
  361. * G_NUMBER_PARSER_ERROR:
  362. *
  363. * Domain for errors returned by functions converting a string to a
  364. * number.
  365. *
  366. * Since: 2.54
  367. */
  368. #define G_NUMBER_PARSER_ERROR (g_number_parser_error_quark ())
  369. GLIB_AVAILABLE_IN_2_54
  370. GQuark g_number_parser_error_quark (void);
  371. GLIB_AVAILABLE_IN_2_54
  372. gboolean g_ascii_string_to_signed (const gchar *str,
  373. guint base,
  374. gint64 min,
  375. gint64 max,
  376. gint64 *out_num,
  377. GError **error);
  378. GLIB_AVAILABLE_IN_2_54
  379. gboolean g_ascii_string_to_unsigned (const gchar *str,
  380. guint base,
  381. guint64 min,
  382. guint64 max,
  383. guint64 *out_num,
  384. GError **error);
  385. /**
  386. * g_set_str: (skip)
  387. * @str_pointer: (inout) (not optional) (nullable): a pointer to either
  388. * a string or `NULL`
  389. * @new_str: (nullable): a string to assign to @str_pointer
  390. *
  391. * Updates a pointer to a string to a copy of @new_str and returns whether the
  392. * string was changed.
  393. *
  394. * If @new_str matches the previous string, this function is a no-op. If
  395. * @new_str is different, a copy of it will be assigned to @str_pointer and
  396. * the previous string pointed to by @str_pointer will be freed with
  397. * [func@GLib.free].
  398. *
  399. * @str_pointer must not be `NULL`, but can point to a `NULL` value.
  400. *
  401. * One convenient usage of this function is in implementing property settings:
  402. * ```C
  403. * void
  404. * foo_set_bar (Foo *foo,
  405. * const char *new_bar)
  406. * {
  407. * g_return_if_fail (IS_FOO (foo));
  408. *
  409. * if (g_set_str (&foo->bar, new_bar))
  410. * g_object_notify (foo, "bar");
  411. * }
  412. * ```
  413. *
  414. * Returns: true if the value of @str_pointer changed, false otherwise
  415. *
  416. * Since: 2.76
  417. */
  418. GLIB_AVAILABLE_STATIC_INLINE_IN_2_76
  419. static inline gboolean
  420. g_set_str (char **str_pointer,
  421. const char *new_str)
  422. {
  423. char *copy;
  424. if (*str_pointer == new_str ||
  425. (*str_pointer && new_str && strcmp (*str_pointer, new_str) == 0))
  426. return FALSE;
  427. copy = g_strdup (new_str);
  428. g_free (*str_pointer);
  429. *str_pointer = copy;
  430. return TRUE;
  431. }
  432. G_END_DECLS
  433. #endif /* __G_STRFUNCS_H__ */