1
0

reference-libobs-util-dstr.rst 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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. .. type:: 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. Free with
  51. :c:func:`strlist_free()`.
  52. ----------------------
  53. .. function:: void strlist_free(char **strlist)
  54. Frees a string list created with :c:func:`strlist_split()`.
  55. ---------------------
  56. Dynamic String Functions
  57. ------------------------
  58. .. function:: void dstr_init(struct dstr *dst)
  59. Initializes a dynamic string variable (just zeroes the variable).
  60. :param dst: Dynamic string to initialize
  61. ----------------------
  62. .. function:: void dstr_init_move(struct dstr *dst, struct dstr *src)
  63. Moves a *src* to *dst* without copying data and zeroes *src*.
  64. :param dst: Destination
  65. :param src: Source
  66. ----------------------
  67. .. function:: void dstr_init_move_array(struct dstr *dst, char *str)
  68. Sets a bmalloc-allocated string as the dynamic string without
  69. copying/reallocating.
  70. :param dst: Dynamic string to initialize
  71. :param str: bmalloc-allocated string
  72. ----------------------
  73. .. function:: void dstr_init_copy(struct dstr *dst, const char *src)
  74. Initializes a dynamic string with a copy of a string
  75. :param dst: Dynamic string to initialize
  76. :param src: String to copy
  77. ----------------------
  78. .. function:: void dstr_init_copy_dstr(struct dstr *dst, const struct dstr *src)
  79. Initializes a dynamic string with a copy of another dynamic string
  80. :param dst: Dynamic string to initialize
  81. :param src: Dynamic string to copy
  82. ----------------------
  83. .. function:: void dstr_free(struct dstr *dst)
  84. Frees a dynamic string.
  85. :param dst: Dynamic string
  86. ----------------------
  87. .. function:: void dstr_copy(struct dstr *dst, const char *array)
  88. Copies a string.
  89. :param dst: Dynamic string
  90. :param array: String to copy
  91. ----------------------
  92. .. function:: void dstr_copy_dstr(struct dstr *dst, const struct dstr *src)
  93. Copies another dynamic string.
  94. :param dst: Dynamic string
  95. :param src: Dynamic string to copy
  96. ----------------------
  97. .. function:: void dstr_ncopy(struct dstr *dst, const char *array, const size_t len)
  98. Copies a specific number of characters from a string.
  99. :param dst: Dynamic string
  100. :param array: String to copy
  101. :param len: Number of characters to copy
  102. ----------------------
  103. .. function:: void dstr_ncopy_dstr(struct dstr *dst, const struct dstr *src, const size_t len)
  104. Copies a specific number of characters from another dynamic string.
  105. :param dst: Dynamic string
  106. :param src: Dynamic string to copy
  107. :param len: Number of characters to copy
  108. ----------------------
  109. .. function:: void dstr_resize(struct dstr *dst, const size_t num)
  110. Sets the size of the dynamic string. If the new size is bigger than
  111. current size, zeroes the new characters.
  112. :param dst: Dynamic string
  113. :param num: New size
  114. ----------------------
  115. .. function:: void dstr_reserve(struct dstr *dst, const size_t num)
  116. Reserves a specific number of characters in the buffer (but does not
  117. change the size). Does not work if the value is smaller than the
  118. current reserve size.
  119. :param dst: Dynamic string
  120. :param num: New reserve size
  121. ----------------------
  122. .. function:: bool dstr_is_empty(const struct dstr *str)
  123. Returns whether the dynamic string is empty.
  124. :param str: Dynamic string
  125. :return: *true* if empty, *false* otherwise
  126. ----------------------
  127. .. function:: void dstr_cat(struct dstr *dst, const char *array)
  128. Concatenates a dynamic string.
  129. :param dst: Dynamic string
  130. :param array: String to concatenate with
  131. ----------------------
  132. .. function:: void dstr_cat_dstr(struct dstr *dst, const struct dstr *str)
  133. Concatenates a dynamic string with another dynamic string.
  134. :param dst: Dynamic string to concatenate to
  135. :param str: Dynamic string to concatenate with
  136. ----------------------
  137. .. function:: void dstr_cat_ch(struct dstr *dst, char ch)
  138. Concatenates a dynamic string with a single character.
  139. :param dst: Dynamic string to concatenate to
  140. :param ch: Character to concatenate
  141. ----------------------
  142. .. function:: void dstr_ncat(struct dstr *dst, const char *array, const size_t len)
  143. Concatenates a dynamic string with a specific number of characters
  144. from a string.
  145. :param dst: Dynamic string to concatenate to
  146. :param array: String to concatenate with
  147. :param len: Number of characters to concatenate with
  148. ----------------------
  149. .. function:: void dstr_ncat_dstr(struct dstr *dst, const struct dstr *str, const size_t len)
  150. Concatenates a dynamic string with a specific number of characters
  151. from another dynamic string.
  152. :param dst: Dynamic string to concatenate to
  153. :param str: Dynamic string to concatenate with
  154. :param len: Number of characters to concatenate with
  155. ----------------------
  156. .. function:: void dstr_insert(struct dstr *dst, const size_t idx, const char *array)
  157. Inserts a string in to a dynamic string at a specific index.
  158. :param dst: Dynamic string to insert in to
  159. :param idx: Character index to insert at
  160. :param array: String to insert
  161. ----------------------
  162. .. function:: void dstr_insert_dstr(struct dstr *dst, const size_t idx, const struct dstr *str)
  163. Inserts another dynamic string in to a dynamic string at a specific
  164. index.
  165. :param dst: Dynamic string to insert in to
  166. :param idx: Character index to insert at
  167. :param str: Dynamic string to insert
  168. ----------------------
  169. .. function:: void dstr_insert_ch(struct dstr *dst, const size_t idx, const char ch)
  170. Inserts a character in to a dynamic string at a specific index.
  171. :param dst: Dynamic string to insert in to
  172. :param idx: Character index to insert at
  173. :param ch: Character to insert
  174. ----------------------
  175. .. function:: void dstr_remove(struct dstr *dst, const size_t idx, const size_t count)
  176. Removes a specific number of characters starting from a specific
  177. index.
  178. :param dst: Dynamic string
  179. :param idx: Index to start removing characters at
  180. :param count: Number of characters to remove
  181. ----------------------
  182. .. function:: void dstr_printf(struct dstr *dst, const char *format, ...)
  183. void dstr_vprintf(struct dstr *dst, const char *format, va_list args)
  184. Sets a dynamic string to a formatted string.
  185. :param dst: Dynamic string
  186. :param format: Format string
  187. ----------------------
  188. .. function:: void dstr_catf(struct dstr *dst, const char *format, ...)
  189. void dstr_vcatf(struct dstr *dst, const char *format, va_list args)
  190. Concatenates a dynamic string with a formatted string.
  191. :param dst: Dynamic string
  192. :param format: Format string
  193. ----------------------
  194. .. function:: const char *dstr_find_i(const struct dstr *str, const char *find)
  195. Finds a string within a dynamic string, case insensitive.
  196. :param str: Dynamic string
  197. :param find: String to find
  198. :return: Pointer to the first occurrence, or *NULL* if not found
  199. ----------------------
  200. .. function:: const char *dstr_find(const struct dstr *str, const char *find)
  201. Finds a string within a dynamic string.
  202. :param str: Dynamic string
  203. :param find: String to find
  204. :return: Pointer to the first occurrence, or *NULL* if not found
  205. ----------------------
  206. .. function:: void dstr_replace(struct dstr *str, const char *find, const char *replace)
  207. Replaces all occurrences of *find* with *replace*.
  208. :param str: Dynamic string
  209. :param find: String to find
  210. :param replace: Replacement string
  211. ----------------------
  212. .. function:: int dstr_cmp(const struct dstr *str1, const char *str2)
  213. Compares with a string.
  214. :param str1: Dynamic string
  215. :param str2: String to compare
  216. :return: 0 if equal, nonzero otherwise
  217. ----------------------
  218. .. function:: int dstr_cmpi(const struct dstr *str1, const char *str2)
  219. Compares with a string, case-insensitive.
  220. :param str1: Dynamic string
  221. :param str2: String to compare
  222. :return: 0 if equal, nonzero otherwise
  223. ----------------------
  224. .. function:: int dstr_ncmp(const struct dstr *str1, const char *str2, const size_t n)
  225. Compares a specific number of characters.
  226. :param str1: Dynamic string
  227. :param str2: String to compare
  228. :param n: Number of characters to compare
  229. :return: 0 if equal, nonzero otherwise
  230. ----------------------
  231. .. function:: int dstr_ncmpi(const struct dstr *str1, const char *str2, const size_t n)
  232. Compares a specific number of characters, case-insensitive.
  233. :param str1: Dynamic string
  234. :param str2: String to compare
  235. :param n: Number of characters to compare
  236. :return: 0 if equal, nonzero otherwise
  237. ----------------------
  238. .. function:: void dstr_depad(struct dstr *dst)
  239. Removes all padding characters (tabs, spaces, CR, LF) from the front
  240. and ends of a dynamic string.
  241. :param dst: Dynamic string
  242. ----------------------
  243. .. function:: void dstr_left(struct dstr *dst, const struct dstr *str, const size_t pos)
  244. Copies a certain number of characters from the left side of one
  245. dynamic string in to another.
  246. :param dst: Destination
  247. :param str: Source
  248. :param pos: Number of characters
  249. ----------------------
  250. .. function:: void dstr_mid(struct dstr *dst, const struct dstr *str, const size_t start, const size_t count)
  251. Copies a certain number of characters from the middle of one dynamic
  252. string in to another.
  253. :param dst: Destination
  254. :param str: Source
  255. :param start: Starting index within *str*
  256. :param count: Number of characters to copy
  257. ----------------------
  258. .. function:: void dstr_right(struct dstr *dst, const struct dstr *str, const size_t pos)
  259. Copies a certain number of characters from the right of one dynamic
  260. string in to another.
  261. :param dst: Destination
  262. :param str: Source
  263. :param pos: Index of *str* to copy from
  264. ----------------------
  265. .. function:: char dstr_end(const struct dstr *str)
  266. :param str: Dynamic string
  267. :return: The last character of a dynamic string
  268. ----------------------
  269. .. function:: void dstr_from_wcs(struct dstr *dst, const wchar_t *wstr)
  270. Copies a wide string in to a dynamic string and converts it to UTF-8.
  271. :param dst: Dynamic string
  272. :param wstr: Wide string
  273. ----------------------
  274. .. function:: wchar_t *dstr_to_wcs(const struct dstr *str)
  275. Converts a dynamic array to a wide string. Free with
  276. :c:func:`bfree()`.
  277. :param str: Dynamic string
  278. :return: Wide string allocation. Free with :c:func:`bfree()`
  279. ----------------------
  280. .. function:: void dstr_to_upper(struct dstr *str)
  281. Converts all characters within a dynamic array to uppercase.
  282. :param str: Dynamic string
  283. ----------------------
  284. .. function:: void dstr_to_lower(struct dstr *str)
  285. Converts all characters within a dynamic array to lowercase.
  286. :param str: Dynamic string