gstring.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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_STRING_H__
  26. #define __G_STRING_H__
  27. #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
  28. #error "Only <glib.h> can be included directly."
  29. #endif
  30. #include <glib/gtypes.h>
  31. #include <glib/gunicode.h>
  32. #include <glib/gbytes.h>
  33. #include <glib/gstrfuncs.h>
  34. #include <glib/gutils.h> /* for G_CAN_INLINE */
  35. #include <string.h>
  36. G_BEGIN_DECLS
  37. typedef struct _GString GString;
  38. struct _GString
  39. {
  40. gchar *str;
  41. gsize len;
  42. gsize allocated_len;
  43. };
  44. GLIB_AVAILABLE_IN_ALL
  45. GString* g_string_new (const gchar *init);
  46. GLIB_AVAILABLE_IN_2_78
  47. GString* g_string_new_take (gchar *init);
  48. GLIB_AVAILABLE_IN_ALL
  49. GString* g_string_new_len (const gchar *init,
  50. gssize len);
  51. GLIB_AVAILABLE_IN_ALL
  52. GString* g_string_sized_new (gsize dfl_size);
  53. GLIB_AVAILABLE_IN_ALL
  54. gchar* (g_string_free) (GString *string,
  55. gboolean free_segment);
  56. GLIB_AVAILABLE_IN_2_76
  57. gchar* g_string_free_and_steal (GString *string) G_GNUC_WARN_UNUSED_RESULT;
  58. #if G_GNUC_CHECK_VERSION (2, 0) && (GLIB_VERSION_MIN_REQUIRED >= GLIB_VERSION_2_76)
  59. #if !defined(__cplusplus) || !G_GNUC_CHECK_VERSION (6, 1) || G_GNUC_CHECK_VERSION (7, 3)
  60. #define g_string_free(str, free_segment) \
  61. (__builtin_constant_p (free_segment) ? \
  62. ((free_segment) ? \
  63. (g_string_free) ((str), (free_segment)) : \
  64. g_string_free_and_steal (str)) \
  65. : \
  66. (g_string_free) ((str), (free_segment)))
  67. #endif /* !defined(__cplusplus) || !G_GNUC_CHECK_VERSION (6, 1) || G_GNUC_CHECK_VERSION (7, 3) */
  68. #endif /* G_GNUC_CHECK_VERSION (2, 0) && (GLIB_VERSION_MIN_REQUIRED >= GLIB_VERSION_2_76) */
  69. GLIB_AVAILABLE_IN_2_34
  70. GBytes* g_string_free_to_bytes (GString *string);
  71. GLIB_AVAILABLE_IN_ALL
  72. gboolean g_string_equal (const GString *v,
  73. const GString *v2);
  74. GLIB_AVAILABLE_IN_ALL
  75. guint g_string_hash (const GString *str);
  76. GLIB_AVAILABLE_IN_ALL
  77. GString* g_string_assign (GString *string,
  78. const gchar *rval);
  79. GLIB_AVAILABLE_IN_ALL
  80. GString* g_string_truncate (GString *string,
  81. gsize len);
  82. GLIB_AVAILABLE_IN_ALL
  83. GString* g_string_set_size (GString *string,
  84. gsize len);
  85. GLIB_AVAILABLE_IN_ALL
  86. GString* g_string_insert_len (GString *string,
  87. gssize pos,
  88. const gchar *val,
  89. gssize len);
  90. GLIB_AVAILABLE_IN_ALL
  91. GString* g_string_append (GString *string,
  92. const gchar *val);
  93. GLIB_AVAILABLE_IN_ALL
  94. GString* g_string_append_len (GString *string,
  95. const gchar *val,
  96. gssize len);
  97. GLIB_AVAILABLE_IN_ALL
  98. GString* g_string_append_c (GString *string,
  99. gchar c);
  100. GLIB_AVAILABLE_IN_ALL
  101. GString* g_string_append_unichar (GString *string,
  102. gunichar wc);
  103. GLIB_AVAILABLE_IN_ALL
  104. GString* g_string_prepend (GString *string,
  105. const gchar *val);
  106. GLIB_AVAILABLE_IN_ALL
  107. GString* g_string_prepend_c (GString *string,
  108. gchar c);
  109. GLIB_AVAILABLE_IN_ALL
  110. GString* g_string_prepend_unichar (GString *string,
  111. gunichar wc);
  112. GLIB_AVAILABLE_IN_ALL
  113. GString* g_string_prepend_len (GString *string,
  114. const gchar *val,
  115. gssize len);
  116. GLIB_AVAILABLE_IN_ALL
  117. GString* g_string_insert (GString *string,
  118. gssize pos,
  119. const gchar *val);
  120. GLIB_AVAILABLE_IN_ALL
  121. GString* g_string_insert_c (GString *string,
  122. gssize pos,
  123. gchar c);
  124. GLIB_AVAILABLE_IN_ALL
  125. GString* g_string_insert_unichar (GString *string,
  126. gssize pos,
  127. gunichar wc);
  128. GLIB_AVAILABLE_IN_ALL
  129. GString* g_string_overwrite (GString *string,
  130. gsize pos,
  131. const gchar *val);
  132. GLIB_AVAILABLE_IN_ALL
  133. GString* g_string_overwrite_len (GString *string,
  134. gsize pos,
  135. const gchar *val,
  136. gssize len);
  137. GLIB_AVAILABLE_IN_ALL
  138. GString* g_string_erase (GString *string,
  139. gssize pos,
  140. gssize len);
  141. GLIB_AVAILABLE_IN_2_68
  142. guint g_string_replace (GString *string,
  143. const gchar *find,
  144. const gchar *replace,
  145. guint limit);
  146. GLIB_AVAILABLE_IN_ALL
  147. GString* g_string_ascii_down (GString *string);
  148. GLIB_AVAILABLE_IN_ALL
  149. GString* g_string_ascii_up (GString *string);
  150. GLIB_AVAILABLE_IN_ALL
  151. void g_string_vprintf (GString *string,
  152. const gchar *format,
  153. va_list args)
  154. G_GNUC_PRINTF(2, 0);
  155. GLIB_AVAILABLE_IN_ALL
  156. void g_string_printf (GString *string,
  157. const gchar *format,
  158. ...) G_GNUC_PRINTF (2, 3);
  159. GLIB_AVAILABLE_IN_ALL
  160. void g_string_append_vprintf (GString *string,
  161. const gchar *format,
  162. va_list args)
  163. G_GNUC_PRINTF(2, 0);
  164. GLIB_AVAILABLE_IN_ALL
  165. void g_string_append_printf (GString *string,
  166. const gchar *format,
  167. ...) G_GNUC_PRINTF (2, 3);
  168. GLIB_AVAILABLE_IN_ALL
  169. GString* g_string_append_uri_escaped (GString *string,
  170. const gchar *unescaped,
  171. const gchar *reserved_chars_allowed,
  172. gboolean allow_utf8);
  173. #ifdef G_CAN_INLINE
  174. #if defined (_MSC_VER) && !defined (__clang__)
  175. #pragma warning (push)
  176. #pragma warning (disable : 4141) /* silence "warning C4141: 'inline' used more than once" */
  177. #endif
  178. #ifndef __GTK_DOC_IGNORE__
  179. G_ALWAYS_INLINE
  180. static inline GString*
  181. g_string_append_c_inline (GString *gstring,
  182. gchar c)
  183. {
  184. if (G_LIKELY (gstring != NULL &&
  185. gstring->len + 1 < gstring->allocated_len))
  186. {
  187. gstring->str[gstring->len++] = c;
  188. gstring->str[gstring->len] = 0;
  189. }
  190. else
  191. g_string_insert_c (gstring, -1, c);
  192. return gstring;
  193. }
  194. #define g_string_append_c(gstr,c) \
  195. g_string_append_c_inline (gstr, c)
  196. G_ALWAYS_INLINE
  197. static inline GString *
  198. g_string_append_len_inline (GString *gstring,
  199. const char *val,
  200. gssize len)
  201. {
  202. gsize len_unsigned;
  203. if G_UNLIKELY (gstring == NULL)
  204. return g_string_append_len (gstring, val, len);
  205. if G_UNLIKELY (val == NULL)
  206. return (len != 0) ? g_string_append_len (gstring, val, len) : gstring;
  207. if (len < 0)
  208. len_unsigned = strlen (val);
  209. else
  210. len_unsigned = (gsize) len;
  211. if (G_LIKELY (gstring->len + len_unsigned < gstring->allocated_len))
  212. {
  213. char *end = gstring->str + gstring->len;
  214. if (G_LIKELY (val + len_unsigned <= end || val > end + len_unsigned))
  215. memcpy (end, val, len_unsigned);
  216. else
  217. memmove (end, val, len_unsigned);
  218. gstring->len += len_unsigned;
  219. gstring->str[gstring->len] = 0;
  220. return gstring;
  221. }
  222. else
  223. return g_string_insert_len (gstring, -1, val, len);
  224. }
  225. #define g_string_append_len(gstr, val, len) \
  226. g_string_append_len_inline (gstr, val, len)
  227. G_ALWAYS_INLINE
  228. static inline GString *
  229. g_string_truncate_inline (GString *gstring,
  230. gsize len)
  231. {
  232. gstring->len = MIN (len, gstring->len);
  233. gstring->str[gstring->len] = '\0';
  234. return gstring;
  235. }
  236. #define g_string_truncate(gstr, len) \
  237. g_string_truncate_inline (gstr, len)
  238. #if G_GNUC_CHECK_VERSION (2, 0)
  239. #define g_string_append(gstr, val) \
  240. (__builtin_constant_p (val) ? \
  241. G_GNUC_EXTENSION ({ \
  242. const char * const __val = (val); \
  243. g_string_append_len (gstr, __val, \
  244. G_LIKELY (__val != NULL) ? \
  245. (gssize) strlen (_G_STR_NONNULL (__val)) \
  246. : (gssize) -1); \
  247. }) \
  248. : \
  249. g_string_append_len (gstr, val, (gssize) -1))
  250. #endif /* G_GNUC_CHECK_VERSION (2, 0) */
  251. #endif /* __GTK_DOC_IGNORE__ */
  252. #if defined (_MSC_VER) && !defined (__clang__)
  253. #pragma warning (pop) /* #pragma warning (disable : 4141) */
  254. #endif
  255. #endif /* G_CAN_INLINE */
  256. GLIB_DEPRECATED
  257. GString *g_string_down (GString *string);
  258. GLIB_DEPRECATED
  259. GString *g_string_up (GString *string);
  260. #define g_string_sprintf g_string_printf GLIB_DEPRECATED_MACRO_IN_2_26_FOR(g_string_printf)
  261. #define g_string_sprintfa g_string_append_printf GLIB_DEPRECATED_MACRO_IN_2_26_FOR(g_string_append_printf)
  262. G_END_DECLS
  263. #endif /* __G_STRING_H__ */