1
0

reference-libobs-util-dstr.rst 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. Dynamic Strings And String Helpers
  2. ==================================
  3. Provides string helper structures/functions (roughly equivalent to
  4. std::string).
  5. .. code:: cpp
  6. #include <util/dstr.h>
  7. Dynamic String Structure (struct dstr)
  8. --------------------------------------
  9. .. struct:: dstr
  10. .. member:: char *dstr.array
  11. .. member:: size_t dstr.len
  12. .. member:: size_t dstr.capacity
  13. General String Helper Functions
  14. -------------------------------
  15. .. function:: int astrcmpi(const char *str1, const char *str2)
  16. Case insensitive string comparison function.
  17. ----------------------
  18. .. function:: int wstrcmpi(const wchar_t *str1, const wchar_t *str2)
  19. Case insensitive wide string comparison function.
  20. ----------------------
  21. .. function:: int astrcmp_n(const char *str1, const char *str2, size_t n)
  22. String comparison function for a specific number of characters.
  23. ----------------------
  24. .. function:: int wstrcmp_n(const wchar_t *str1, const wchar_t *str2, size_t n)
  25. Wide string comparison function for a specific number of characters.
  26. ----------------------
  27. .. function:: int astrcmpi_n(const char *str1, const char *str2, size_t n)
  28. Case insensitive string comparison function for a specific number of
  29. characters.
  30. ----------------------
  31. .. function:: int wstrcmpi_n(const wchar_t *str1, const wchar_t *str2, size_t n)
  32. Case insensitive wide string comparison function for a specific
  33. number of characters.
  34. ----------------------
  35. .. function:: char *astrstri(const char *str, const char *find)
  36. Case insensitive version of strstr.
  37. ----------------------
  38. .. function:: wchar_t *wstrstri(const wchar_t *str, const wchar_t *find)
  39. Case insensitive version of wcsstr.
  40. ----------------------
  41. .. function:: char *strdepad(char *str)
  42. Removes padding characters (tab, space, CR, LF) from the front and
  43. end of a string.
  44. ----------------------
  45. .. function:: wchar_t *wcsdepad(wchar_t *str)
  46. Removes padding characters (tab, space, CR, LF) from the front and
  47. end of a wide string.
  48. ----------------------
  49. .. function:: char **strlist_split(const char *str, char split_ch, bool include_empty)
  50. Splits a string in to a list of multiple sub-strings, terminated by
  51. ``NULL``. If ``split_ch`` does not exist in the string, the first
  52. sub-string will be the same as ``str``. Free with :c:func:`strlist_free()`.
  53. :param str: The string to be split
  54. :param split_ch: The delimiter
  55. :param include_empty: If *true*, empty strings caused by having the
  56. ``split_ch`` right next to another will be
  57. included in the list. If *false*, they won't
  58. be included.
  59. Sample usage:
  60. .. code:: cpp
  61. char **words = strlist_split("OBS Studio", ' ', false);
  62. int count = 0;
  63. for (char **word = words; *word; ++word) {
  64. count++;
  65. blog(LOG_DEBUG, "%s", *word);
  66. }
  67. strlist_free(words);
  68. // count == 2
  69. ----------------------
  70. .. function:: void strlist_free(char **strlist)
  71. Frees a string list created with :c:func:`strlist_split()`.
  72. ---------------------
  73. Dynamic String Functions
  74. ------------------------
  75. .. function:: void dstr_init(struct dstr *dst)
  76. Initializes a dynamic string variable (just zeroes the variable).
  77. :param dst: Dynamic string to initialize
  78. ----------------------
  79. .. function:: void dstr_init_move(struct dstr *dst, struct dstr *src)
  80. Moves a *src* to *dst* without copying data and zeroes *src*.
  81. :param dst: Destination
  82. :param src: Source
  83. ----------------------
  84. .. function:: void dstr_init_move_array(struct dstr *dst, char *str)
  85. Sets a bmalloc-allocated string as the dynamic string without
  86. copying/reallocating.
  87. :param dst: Dynamic string to initialize
  88. :param str: bmalloc-allocated string
  89. ----------------------
  90. .. function:: void dstr_init_copy(struct dstr *dst, const char *src)
  91. Initializes a dynamic string with a copy of a string
  92. :param dst: Dynamic string to initialize
  93. :param src: String to copy
  94. ----------------------
  95. .. function:: void dstr_init_copy_dstr(struct dstr *dst, const struct dstr *src)
  96. Initializes a dynamic string with a copy of another dynamic string
  97. :param dst: Dynamic string to initialize
  98. :param src: Dynamic string to copy
  99. ----------------------
  100. .. function:: void dstr_free(struct dstr *dst)
  101. Frees a dynamic string.
  102. :param dst: Dynamic string
  103. ----------------------
  104. .. function:: void dstr_copy(struct dstr *dst, const char *array)
  105. Copies a string.
  106. :param dst: Dynamic string
  107. :param array: String to copy
  108. ----------------------
  109. .. function:: void dstr_copy_dstr(struct dstr *dst, const struct dstr *src)
  110. Copies another dynamic string.
  111. :param dst: Dynamic string
  112. :param src: Dynamic string to copy
  113. ----------------------
  114. .. function:: void dstr_ncopy(struct dstr *dst, const char *array, const size_t len)
  115. Copies a specific number of characters from a string.
  116. :param dst: Dynamic string
  117. :param array: String to copy
  118. :param len: Number of characters to copy
  119. ----------------------
  120. .. function:: void dstr_ncopy_dstr(struct dstr *dst, const struct dstr *src, const size_t len)
  121. Copies a specific number of characters from another dynamic string.
  122. :param dst: Dynamic string
  123. :param src: Dynamic string to copy
  124. :param len: Number of characters to copy
  125. ----------------------
  126. .. function:: void dstr_resize(struct dstr *dst, const size_t num)
  127. Sets the size of the dynamic string. If the new size is bigger than
  128. current size, zeroes the new characters.
  129. :param dst: Dynamic string
  130. :param num: New size
  131. ----------------------
  132. .. function:: void dstr_reserve(struct dstr *dst, const size_t num)
  133. Reserves a specific number of characters in the buffer (but does not
  134. change the size). Does not work if the value is smaller than the
  135. current reserve size.
  136. :param dst: Dynamic string
  137. :param num: New reserve size
  138. ----------------------
  139. .. function:: bool dstr_is_empty(const struct dstr *str)
  140. Returns whether the dynamic string is empty.
  141. :param str: Dynamic string
  142. :return: *true* if empty, *false* otherwise
  143. ----------------------
  144. .. function:: void dstr_cat(struct dstr *dst, const char *array)
  145. Concatenates a dynamic string.
  146. :param dst: Dynamic string
  147. :param array: String to concatenate with
  148. ----------------------
  149. .. function:: void dstr_cat_dstr(struct dstr *dst, const struct dstr *str)
  150. Concatenates a dynamic string with another dynamic string.
  151. :param dst: Dynamic string to concatenate to
  152. :param str: Dynamic string to concatenate with
  153. ----------------------
  154. .. function:: void dstr_cat_ch(struct dstr *dst, char ch)
  155. Concatenates a dynamic string with a single character.
  156. :param dst: Dynamic string to concatenate to
  157. :param ch: Character to concatenate
  158. ----------------------
  159. .. function:: void dstr_ncat(struct dstr *dst, const char *array, const size_t len)
  160. Concatenates a dynamic string with a specific number of characters
  161. from a string.
  162. :param dst: Dynamic string to concatenate to
  163. :param array: String to concatenate with
  164. :param len: Number of characters to concatenate with
  165. ----------------------
  166. .. function:: void dstr_ncat_dstr(struct dstr *dst, const struct dstr *str, const size_t len)
  167. Concatenates a dynamic string with a specific number of characters
  168. from another dynamic string.
  169. :param dst: Dynamic string to concatenate to
  170. :param str: Dynamic string to concatenate with
  171. :param len: Number of characters to concatenate with
  172. ----------------------
  173. .. function:: void dstr_insert(struct dstr *dst, const size_t idx, const char *array)
  174. Inserts a string in to a dynamic string at a specific index.
  175. :param dst: Dynamic string to insert in to
  176. :param idx: Character index to insert at
  177. :param array: String to insert
  178. ----------------------
  179. .. function:: void dstr_insert_dstr(struct dstr *dst, const size_t idx, const struct dstr *str)
  180. Inserts another dynamic string in to a dynamic string at a specific
  181. index.
  182. :param dst: Dynamic string to insert in to
  183. :param idx: Character index to insert at
  184. :param str: Dynamic string to insert
  185. ----------------------
  186. .. function:: void dstr_insert_ch(struct dstr *dst, const size_t idx, const char ch)
  187. Inserts a character in to a dynamic string at a specific index.
  188. :param dst: Dynamic string to insert in to
  189. :param idx: Character index to insert at
  190. :param ch: Character to insert
  191. ----------------------
  192. .. function:: void dstr_remove(struct dstr *dst, const size_t idx, const size_t count)
  193. Removes a specific number of characters starting from a specific
  194. index.
  195. :param dst: Dynamic string
  196. :param idx: Index to start removing characters at
  197. :param count: Number of characters to remove
  198. ----------------------
  199. .. function:: void dstr_printf(struct dstr *dst, const char *format, ...)
  200. void dstr_vprintf(struct dstr *dst, const char *format, va_list args)
  201. Sets a dynamic string to a formatted string.
  202. :param dst: Dynamic string
  203. :param format: Format string
  204. ----------------------
  205. .. function:: void dstr_catf(struct dstr *dst, const char *format, ...)
  206. void dstr_vcatf(struct dstr *dst, const char *format, va_list args)
  207. Concatenates a dynamic string with a formatted string.
  208. :param dst: Dynamic string
  209. :param format: Format string
  210. ----------------------
  211. .. function:: const char *dstr_find_i(const struct dstr *str, const char *find)
  212. Finds a string within a dynamic string, case insensitive.
  213. :param str: Dynamic string
  214. :param find: String to find
  215. :return: Pointer to the first occurrence, or *NULL* if not found
  216. ----------------------
  217. .. function:: const char *dstr_find(const struct dstr *str, const char *find)
  218. Finds a string within a dynamic string.
  219. :param str: Dynamic string
  220. :param find: String to find
  221. :return: Pointer to the first occurrence, or *NULL* if not found
  222. ----------------------
  223. .. function:: void dstr_replace(struct dstr *str, const char *find, const char *replace)
  224. Replaces all occurrences of *find* with *replace*.
  225. :param str: Dynamic string
  226. :param find: String to find
  227. :param replace: Replacement string
  228. ----------------------
  229. .. function:: int dstr_cmp(const struct dstr *str1, const char *str2)
  230. Compares with a string.
  231. :param str1: Dynamic string
  232. :param str2: String to compare
  233. :return: 0 if equal, nonzero otherwise
  234. ----------------------
  235. .. function:: int dstr_cmpi(const struct dstr *str1, const char *str2)
  236. Compares with a string, case-insensitive.
  237. :param str1: Dynamic string
  238. :param str2: String to compare
  239. :return: 0 if equal, nonzero otherwise
  240. ----------------------
  241. .. function:: int dstr_ncmp(const struct dstr *str1, const char *str2, const size_t n)
  242. Compares a specific number of characters.
  243. :param str1: Dynamic string
  244. :param str2: String to compare
  245. :param n: Number of characters to compare
  246. :return: 0 if equal, nonzero otherwise
  247. ----------------------
  248. .. function:: int dstr_ncmpi(const struct dstr *str1, const char *str2, const size_t n)
  249. Compares a specific number of characters, case-insensitive.
  250. :param str1: Dynamic string
  251. :param str2: String to compare
  252. :param n: Number of characters to compare
  253. :return: 0 if equal, nonzero otherwise
  254. ----------------------
  255. .. function:: void dstr_depad(struct dstr *dst)
  256. Removes all padding characters (tabs, spaces, CR, LF) from the front
  257. and ends of a dynamic string.
  258. :param dst: Dynamic string
  259. ----------------------
  260. .. function:: void dstr_left(struct dstr *dst, const struct dstr *str, const size_t pos)
  261. Copies a certain number of characters from the left side of one
  262. dynamic string in to another.
  263. :param dst: Destination
  264. :param str: Source
  265. :param pos: Number of characters
  266. ----------------------
  267. .. function:: void dstr_mid(struct dstr *dst, const struct dstr *str, const size_t start, const size_t count)
  268. Copies a certain number of characters from the middle of one dynamic
  269. string in to another.
  270. :param dst: Destination
  271. :param str: Source
  272. :param start: Starting index within *str*
  273. :param count: Number of characters to copy
  274. ----------------------
  275. .. function:: void dstr_right(struct dstr *dst, const struct dstr *str, const size_t pos)
  276. Copies a certain number of characters from the right of one dynamic
  277. string in to another.
  278. :param dst: Destination
  279. :param str: Source
  280. :param pos: Index of *str* to copy from
  281. ----------------------
  282. .. function:: char dstr_end(const struct dstr *str)
  283. :param str: Dynamic string
  284. :return: The last character of a dynamic string
  285. ----------------------
  286. .. function:: void dstr_from_wcs(struct dstr *dst, const wchar_t *wstr)
  287. Copies a wide string in to a dynamic string and converts it to UTF-8.
  288. :param dst: Dynamic string
  289. :param wstr: Wide string
  290. ----------------------
  291. .. function:: wchar_t *dstr_to_wcs(const struct dstr *str)
  292. Converts a dynamic array to a wide string. Free with
  293. :c:func:`bfree()`.
  294. :param str: Dynamic string
  295. :return: Wide string allocation. Free with :c:func:`bfree()`
  296. ----------------------
  297. .. function:: void dstr_to_upper(struct dstr *str)
  298. Converts all characters within a dynamic array to uppercase.
  299. :param str: Dynamic string
  300. ----------------------
  301. .. function:: void dstr_to_lower(struct dstr *str)
  302. Converts all characters within a dynamic array to lowercase.
  303. :param str: Dynamic string