unistr.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  1. /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
  2. /* Elementary Unicode string functions.
  3. Copyright (C) 2001-2002, 2005-2020 Free Software Foundation, Inc.
  4. This program is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published
  6. by the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <https://www.gnu.org/licenses/>. */
  14. #ifndef _UNISTR_H
  15. #define _UNISTR_H
  16. #include "unitypes.h"
  17. /* Get common macros for C. */
  18. #include "unused-parameter.h"
  19. /* Get bool. */
  20. #include <stdbool.h>
  21. /* Get size_t, ptrdiff_t. */
  22. #include <stddef.h>
  23. #ifdef __cplusplus
  24. extern "C" {
  25. #endif
  26. /* Conventions:
  27. All functions prefixed with u8_ operate on UTF-8 encoded strings.
  28. Their unit is an uint8_t (1 byte).
  29. All functions prefixed with u16_ operate on UTF-16 encoded strings.
  30. Their unit is an uint16_t (a 2-byte word).
  31. All functions prefixed with u32_ operate on UCS-4 encoded strings.
  32. Their unit is an uint32_t (a 4-byte word).
  33. All argument pairs (s, n) denote a Unicode string s[0..n-1] with exactly
  34. n units.
  35. All arguments starting with "str" and the arguments of functions starting
  36. with u8_str/u16_str/u32_str denote a NUL terminated string, i.e. a string
  37. which terminates at the first NUL unit. This termination unit is
  38. considered part of the string for all memory allocation purposes, but
  39. is not considered part of the string for all other logical purposes.
  40. Functions returning a string result take a (resultbuf, lengthp) argument
  41. pair. If resultbuf is not NULL and the result fits into *lengthp units,
  42. it is put in resultbuf, and resultbuf is returned. Otherwise, a freshly
  43. allocated string is returned. In both cases, *lengthp is set to the
  44. length (number of units) of the returned string. In case of error,
  45. NULL is returned and errno is set. */
  46. /* Elementary string checks. */
  47. /* Check whether an UTF-8 string is well-formed.
  48. Return NULL if valid, or a pointer to the first invalid unit otherwise. */
  49. extern const uint8_t *
  50. u8_check (const uint8_t *s, size_t n)
  51. _UC_ATTRIBUTE_PURE;
  52. /* Check whether an UTF-16 string is well-formed.
  53. Return NULL if valid, or a pointer to the first invalid unit otherwise. */
  54. extern const uint16_t *
  55. u16_check (const uint16_t *s, size_t n)
  56. _UC_ATTRIBUTE_PURE;
  57. /* Check whether an UCS-4 string is well-formed.
  58. Return NULL if valid, or a pointer to the first invalid unit otherwise. */
  59. extern const uint32_t *
  60. u32_check (const uint32_t *s, size_t n)
  61. _UC_ATTRIBUTE_PURE;
  62. /* Elementary string conversions. */
  63. /* Convert an UTF-8 string to an UTF-16 string. */
  64. extern uint16_t *
  65. u8_to_u16 (const uint8_t *s, size_t n, uint16_t *resultbuf,
  66. size_t *lengthp);
  67. /* Convert an UTF-8 string to an UCS-4 string. */
  68. extern uint32_t *
  69. u8_to_u32 (const uint8_t *s, size_t n, uint32_t *resultbuf,
  70. size_t *lengthp);
  71. /* Convert an UTF-16 string to an UTF-8 string. */
  72. extern uint8_t *
  73. u16_to_u8 (const uint16_t *s, size_t n, uint8_t *resultbuf,
  74. size_t *lengthp);
  75. /* Convert an UTF-16 string to an UCS-4 string. */
  76. extern uint32_t *
  77. u16_to_u32 (const uint16_t *s, size_t n, uint32_t *resultbuf,
  78. size_t *lengthp);
  79. /* Convert an UCS-4 string to an UTF-8 string. */
  80. extern uint8_t *
  81. u32_to_u8 (const uint32_t *s, size_t n, uint8_t *resultbuf,
  82. size_t *lengthp);
  83. /* Convert an UCS-4 string to an UTF-16 string. */
  84. extern uint16_t *
  85. u32_to_u16 (const uint32_t *s, size_t n, uint16_t *resultbuf,
  86. size_t *lengthp);
  87. /* Elementary string functions. */
  88. /* Return the length (number of units) of the first character in S, which is
  89. no longer than N. Return 0 if it is the NUL character. Return -1 upon
  90. failure. */
  91. /* Similar to mblen(), except that s must not be NULL. */
  92. extern int
  93. u8_mblen (const uint8_t *s, size_t n)
  94. _UC_ATTRIBUTE_PURE;
  95. extern int
  96. u16_mblen (const uint16_t *s, size_t n)
  97. _UC_ATTRIBUTE_PURE;
  98. extern int
  99. u32_mblen (const uint32_t *s, size_t n)
  100. _UC_ATTRIBUTE_PURE;
  101. /* Return the length (number of units) of the first character in S, putting
  102. its 'ucs4_t' representation in *PUC. Upon failure, *PUC is set to 0xfffd,
  103. and an appropriate number of units is returned.
  104. The number of available units, N, must be > 0. */
  105. /* Similar to mbtowc(), except that puc and s must not be NULL, n must be > 0,
  106. and the NUL character is not treated specially. */
  107. /* The variants with _unsafe suffix are for backward compatibility with
  108. libunistring versions < 0.9.7. */
  109. #if GNULIB_UNISTR_U8_MBTOUC_UNSAFE || HAVE_LIBUNISTRING
  110. # if !HAVE_INLINE
  111. extern int
  112. u8_mbtouc_unsafe (ucs4_t *puc, const uint8_t *s, size_t n);
  113. # else
  114. extern int
  115. u8_mbtouc_unsafe_aux (ucs4_t *puc, const uint8_t *s, size_t n);
  116. static inline int
  117. u8_mbtouc_unsafe (ucs4_t *puc, const uint8_t *s, size_t n)
  118. {
  119. uint8_t c = *s;
  120. if (c < 0x80)
  121. {
  122. *puc = c;
  123. return 1;
  124. }
  125. else
  126. return u8_mbtouc_unsafe_aux (puc, s, n);
  127. }
  128. # endif
  129. #endif
  130. #if GNULIB_UNISTR_U16_MBTOUC_UNSAFE || HAVE_LIBUNISTRING
  131. # if !HAVE_INLINE
  132. extern int
  133. u16_mbtouc_unsafe (ucs4_t *puc, const uint16_t *s, size_t n);
  134. # else
  135. extern int
  136. u16_mbtouc_unsafe_aux (ucs4_t *puc, const uint16_t *s, size_t n);
  137. static inline int
  138. u16_mbtouc_unsafe (ucs4_t *puc, const uint16_t *s, size_t n)
  139. {
  140. uint16_t c = *s;
  141. if (c < 0xd800 || c >= 0xe000)
  142. {
  143. *puc = c;
  144. return 1;
  145. }
  146. else
  147. return u16_mbtouc_unsafe_aux (puc, s, n);
  148. }
  149. # endif
  150. #endif
  151. #if GNULIB_UNISTR_U32_MBTOUC_UNSAFE || HAVE_LIBUNISTRING
  152. # if !HAVE_INLINE
  153. extern int
  154. u32_mbtouc_unsafe (ucs4_t *puc, const uint32_t *s, size_t n);
  155. # else
  156. static inline int
  157. u32_mbtouc_unsafe (ucs4_t *puc,
  158. const uint32_t *s, size_t n _GL_UNUSED_PARAMETER)
  159. {
  160. uint32_t c = *s;
  161. if (c < 0xd800 || (c >= 0xe000 && c < 0x110000))
  162. *puc = c;
  163. else
  164. /* invalid multibyte character */
  165. *puc = 0xfffd;
  166. return 1;
  167. }
  168. # endif
  169. #endif
  170. #if GNULIB_UNISTR_U8_MBTOUC || HAVE_LIBUNISTRING
  171. # if !HAVE_INLINE
  172. extern int
  173. u8_mbtouc (ucs4_t *puc, const uint8_t *s, size_t n);
  174. # else
  175. extern int
  176. u8_mbtouc_aux (ucs4_t *puc, const uint8_t *s, size_t n);
  177. static inline int
  178. u8_mbtouc (ucs4_t *puc, const uint8_t *s, size_t n)
  179. {
  180. uint8_t c = *s;
  181. if (c < 0x80)
  182. {
  183. *puc = c;
  184. return 1;
  185. }
  186. else
  187. return u8_mbtouc_aux (puc, s, n);
  188. }
  189. # endif
  190. #endif
  191. #if GNULIB_UNISTR_U16_MBTOUC || HAVE_LIBUNISTRING
  192. # if !HAVE_INLINE
  193. extern int
  194. u16_mbtouc (ucs4_t *puc, const uint16_t *s, size_t n);
  195. # else
  196. extern int
  197. u16_mbtouc_aux (ucs4_t *puc, const uint16_t *s, size_t n);
  198. static inline int
  199. u16_mbtouc (ucs4_t *puc, const uint16_t *s, size_t n)
  200. {
  201. uint16_t c = *s;
  202. if (c < 0xd800 || c >= 0xe000)
  203. {
  204. *puc = c;
  205. return 1;
  206. }
  207. else
  208. return u16_mbtouc_aux (puc, s, n);
  209. }
  210. # endif
  211. #endif
  212. #if GNULIB_UNISTR_U32_MBTOUC || HAVE_LIBUNISTRING
  213. # if !HAVE_INLINE
  214. extern int
  215. u32_mbtouc (ucs4_t *puc, const uint32_t *s, size_t n);
  216. # else
  217. static inline int
  218. u32_mbtouc (ucs4_t *puc, const uint32_t *s, size_t n _GL_UNUSED_PARAMETER)
  219. {
  220. uint32_t c = *s;
  221. if (c < 0xd800 || (c >= 0xe000 && c < 0x110000))
  222. *puc = c;
  223. else
  224. /* invalid multibyte character */
  225. *puc = 0xfffd;
  226. return 1;
  227. }
  228. # endif
  229. #endif
  230. /* Return the length (number of units) of the first character in S, putting
  231. its 'ucs4_t' representation in *PUC. Upon failure, *PUC is set to 0xfffd,
  232. and -1 is returned for an invalid sequence of units, -2 is returned for an
  233. incomplete sequence of units.
  234. The number of available units, N, must be > 0. */
  235. /* Similar to u*_mbtouc(), except that the return value gives more details
  236. about the failure, similar to mbrtowc(). */
  237. #if GNULIB_UNISTR_U8_MBTOUCR || HAVE_LIBUNISTRING
  238. extern int
  239. u8_mbtoucr (ucs4_t *puc, const uint8_t *s, size_t n);
  240. #endif
  241. #if GNULIB_UNISTR_U16_MBTOUCR || HAVE_LIBUNISTRING
  242. extern int
  243. u16_mbtoucr (ucs4_t *puc, const uint16_t *s, size_t n);
  244. #endif
  245. #if GNULIB_UNISTR_U32_MBTOUCR || HAVE_LIBUNISTRING
  246. extern int
  247. u32_mbtoucr (ucs4_t *puc, const uint32_t *s, size_t n);
  248. #endif
  249. /* Put the multibyte character represented by UC in S, returning its
  250. length. Return -1 upon failure, -2 if the number of available units, N,
  251. is too small. The latter case cannot occur if N >= 6/2/1, respectively. */
  252. /* Similar to wctomb(), except that s must not be NULL, and the argument n
  253. must be specified. */
  254. #if GNULIB_UNISTR_U8_UCTOMB || HAVE_LIBUNISTRING
  255. /* Auxiliary function, also used by u8_chr, u8_strchr, u8_strrchr. */
  256. extern int
  257. u8_uctomb_aux (uint8_t *s, ucs4_t uc, ptrdiff_t n);
  258. # if !HAVE_INLINE
  259. extern int
  260. u8_uctomb (uint8_t *s, ucs4_t uc, ptrdiff_t n);
  261. # else
  262. static inline int
  263. u8_uctomb (uint8_t *s, ucs4_t uc, ptrdiff_t n)
  264. {
  265. if (uc < 0x80 && n > 0)
  266. {
  267. s[0] = uc;
  268. return 1;
  269. }
  270. else
  271. return u8_uctomb_aux (s, uc, n);
  272. }
  273. # endif
  274. #endif
  275. #if GNULIB_UNISTR_U16_UCTOMB || HAVE_LIBUNISTRING
  276. /* Auxiliary function, also used by u16_chr, u16_strchr, u16_strrchr. */
  277. extern int
  278. u16_uctomb_aux (uint16_t *s, ucs4_t uc, ptrdiff_t n);
  279. # if !HAVE_INLINE
  280. extern int
  281. u16_uctomb (uint16_t *s, ucs4_t uc, ptrdiff_t n);
  282. # else
  283. static inline int
  284. u16_uctomb (uint16_t *s, ucs4_t uc, ptrdiff_t n)
  285. {
  286. if (uc < 0xd800 && n > 0)
  287. {
  288. s[0] = uc;
  289. return 1;
  290. }
  291. else
  292. return u16_uctomb_aux (s, uc, n);
  293. }
  294. # endif
  295. #endif
  296. #if GNULIB_UNISTR_U32_UCTOMB || HAVE_LIBUNISTRING
  297. # if !HAVE_INLINE
  298. extern int
  299. u32_uctomb (uint32_t *s, ucs4_t uc, ptrdiff_t n);
  300. # else
  301. static inline int
  302. u32_uctomb (uint32_t *s, ucs4_t uc, ptrdiff_t n)
  303. {
  304. if (uc < 0xd800 || (uc >= 0xe000 && uc < 0x110000))
  305. {
  306. if (n > 0)
  307. {
  308. *s = uc;
  309. return 1;
  310. }
  311. else
  312. return -2;
  313. }
  314. else
  315. return -1;
  316. }
  317. # endif
  318. #endif
  319. /* Copy N units from SRC to DEST. */
  320. /* Similar to memcpy(). */
  321. extern uint8_t *
  322. u8_cpy (uint8_t *_UC_RESTRICT dest, const uint8_t *src, size_t n);
  323. extern uint16_t *
  324. u16_cpy (uint16_t *_UC_RESTRICT dest, const uint16_t *src, size_t n);
  325. extern uint32_t *
  326. u32_cpy (uint32_t *_UC_RESTRICT dest, const uint32_t *src, size_t n);
  327. /* Copy N units from SRC to DEST, guaranteeing correct behavior for
  328. overlapping memory areas. */
  329. /* Similar to memmove(). */
  330. extern uint8_t *
  331. u8_move (uint8_t *dest, const uint8_t *src, size_t n);
  332. extern uint16_t *
  333. u16_move (uint16_t *dest, const uint16_t *src, size_t n);
  334. extern uint32_t *
  335. u32_move (uint32_t *dest, const uint32_t *src, size_t n);
  336. /* Set the first N characters of S to UC. UC should be a character that
  337. occupies only 1 unit. */
  338. /* Similar to memset(). */
  339. extern uint8_t *
  340. u8_set (uint8_t *s, ucs4_t uc, size_t n);
  341. extern uint16_t *
  342. u16_set (uint16_t *s, ucs4_t uc, size_t n);
  343. extern uint32_t *
  344. u32_set (uint32_t *s, ucs4_t uc, size_t n);
  345. /* Compare S1 and S2, each of length N. */
  346. /* Similar to memcmp(). */
  347. extern int
  348. u8_cmp (const uint8_t *s1, const uint8_t *s2, size_t n)
  349. _UC_ATTRIBUTE_PURE;
  350. extern int
  351. u16_cmp (const uint16_t *s1, const uint16_t *s2, size_t n)
  352. _UC_ATTRIBUTE_PURE;
  353. extern int
  354. u32_cmp (const uint32_t *s1, const uint32_t *s2, size_t n)
  355. _UC_ATTRIBUTE_PURE;
  356. /* Compare S1 and S2. */
  357. /* Similar to the gnulib function memcmp2(). */
  358. extern int
  359. u8_cmp2 (const uint8_t *s1, size_t n1, const uint8_t *s2, size_t n2)
  360. _UC_ATTRIBUTE_PURE;
  361. extern int
  362. u16_cmp2 (const uint16_t *s1, size_t n1, const uint16_t *s2, size_t n2)
  363. _UC_ATTRIBUTE_PURE;
  364. extern int
  365. u32_cmp2 (const uint32_t *s1, size_t n1, const uint32_t *s2, size_t n2)
  366. _UC_ATTRIBUTE_PURE;
  367. /* Search the string at S for UC. */
  368. /* Similar to memchr(). */
  369. extern uint8_t *
  370. u8_chr (const uint8_t *s, size_t n, ucs4_t uc)
  371. _UC_ATTRIBUTE_PURE;
  372. extern uint16_t *
  373. u16_chr (const uint16_t *s, size_t n, ucs4_t uc)
  374. _UC_ATTRIBUTE_PURE;
  375. extern uint32_t *
  376. u32_chr (const uint32_t *s, size_t n, ucs4_t uc)
  377. _UC_ATTRIBUTE_PURE;
  378. /* Count the number of Unicode characters in the N units from S. */
  379. /* Similar to mbsnlen(). */
  380. extern size_t
  381. u8_mbsnlen (const uint8_t *s, size_t n)
  382. _UC_ATTRIBUTE_PURE;
  383. extern size_t
  384. u16_mbsnlen (const uint16_t *s, size_t n)
  385. _UC_ATTRIBUTE_PURE;
  386. extern size_t
  387. u32_mbsnlen (const uint32_t *s, size_t n)
  388. _UC_ATTRIBUTE_PURE;
  389. /* Elementary string functions with memory allocation. */
  390. /* Make a freshly allocated copy of S, of length N. */
  391. extern uint8_t *
  392. u8_cpy_alloc (const uint8_t *s, size_t n);
  393. extern uint16_t *
  394. u16_cpy_alloc (const uint16_t *s, size_t n);
  395. extern uint32_t *
  396. u32_cpy_alloc (const uint32_t *s, size_t n);
  397. /* Elementary string functions on NUL terminated strings. */
  398. /* Return the length (number of units) of the first character in S.
  399. Return 0 if it is the NUL character. Return -1 upon failure. */
  400. extern int
  401. u8_strmblen (const uint8_t *s)
  402. _UC_ATTRIBUTE_PURE;
  403. extern int
  404. u16_strmblen (const uint16_t *s)
  405. _UC_ATTRIBUTE_PURE;
  406. extern int
  407. u32_strmblen (const uint32_t *s)
  408. _UC_ATTRIBUTE_PURE;
  409. /* Return the length (number of units) of the first character in S, putting
  410. its 'ucs4_t' representation in *PUC. Return 0 if it is the NUL
  411. character. Return -1 upon failure. */
  412. extern int
  413. u8_strmbtouc (ucs4_t *puc, const uint8_t *s);
  414. extern int
  415. u16_strmbtouc (ucs4_t *puc, const uint16_t *s);
  416. extern int
  417. u32_strmbtouc (ucs4_t *puc, const uint32_t *s);
  418. /* Forward iteration step. Advances the pointer past the next character,
  419. or returns NULL if the end of the string has been reached. Puts the
  420. character's 'ucs4_t' representation in *PUC. */
  421. extern const uint8_t *
  422. u8_next (ucs4_t *puc, const uint8_t *s);
  423. extern const uint16_t *
  424. u16_next (ucs4_t *puc, const uint16_t *s);
  425. extern const uint32_t *
  426. u32_next (ucs4_t *puc, const uint32_t *s);
  427. /* Backward iteration step. Advances the pointer to point to the previous
  428. character, or returns NULL if the beginning of the string had been reached.
  429. Puts the character's 'ucs4_t' representation in *PUC. */
  430. extern const uint8_t *
  431. u8_prev (ucs4_t *puc, const uint8_t *s, const uint8_t *start);
  432. extern const uint16_t *
  433. u16_prev (ucs4_t *puc, const uint16_t *s, const uint16_t *start);
  434. extern const uint32_t *
  435. u32_prev (ucs4_t *puc, const uint32_t *s, const uint32_t *start);
  436. /* Return the number of units in S. */
  437. /* Similar to strlen(), wcslen(). */
  438. extern size_t
  439. u8_strlen (const uint8_t *s)
  440. _UC_ATTRIBUTE_PURE;
  441. extern size_t
  442. u16_strlen (const uint16_t *s)
  443. _UC_ATTRIBUTE_PURE;
  444. extern size_t
  445. u32_strlen (const uint32_t *s)
  446. _UC_ATTRIBUTE_PURE;
  447. /* Return the number of units in S, but at most MAXLEN. */
  448. /* Similar to strnlen(), wcsnlen(). */
  449. extern size_t
  450. u8_strnlen (const uint8_t *s, size_t maxlen)
  451. _UC_ATTRIBUTE_PURE;
  452. extern size_t
  453. u16_strnlen (const uint16_t *s, size_t maxlen)
  454. _UC_ATTRIBUTE_PURE;
  455. extern size_t
  456. u32_strnlen (const uint32_t *s, size_t maxlen)
  457. _UC_ATTRIBUTE_PURE;
  458. /* Copy SRC to DEST. */
  459. /* Similar to strcpy(), wcscpy(). */
  460. extern uint8_t *
  461. u8_strcpy (uint8_t *_UC_RESTRICT dest, const uint8_t *src);
  462. extern uint16_t *
  463. u16_strcpy (uint16_t *_UC_RESTRICT dest, const uint16_t *src);
  464. extern uint32_t *
  465. u32_strcpy (uint32_t *_UC_RESTRICT dest, const uint32_t *src);
  466. /* Copy SRC to DEST, returning the address of the terminating NUL in DEST. */
  467. /* Similar to stpcpy(). */
  468. extern uint8_t *
  469. u8_stpcpy (uint8_t *_UC_RESTRICT dest, const uint8_t *src);
  470. extern uint16_t *
  471. u16_stpcpy (uint16_t *_UC_RESTRICT dest, const uint16_t *src);
  472. extern uint32_t *
  473. u32_stpcpy (uint32_t *_UC_RESTRICT dest, const uint32_t *src);
  474. /* Copy no more than N units of SRC to DEST. */
  475. /* Similar to strncpy(), wcsncpy(). */
  476. extern uint8_t *
  477. u8_strncpy (uint8_t *_UC_RESTRICT dest, const uint8_t *src, size_t n);
  478. extern uint16_t *
  479. u16_strncpy (uint16_t *_UC_RESTRICT dest, const uint16_t *src, size_t n);
  480. extern uint32_t *
  481. u32_strncpy (uint32_t *_UC_RESTRICT dest, const uint32_t *src, size_t n);
  482. /* Copy no more than N units of SRC to DEST. Return a pointer past the last
  483. non-NUL unit written into DEST. */
  484. /* Similar to stpncpy(). */
  485. extern uint8_t *
  486. u8_stpncpy (uint8_t *_UC_RESTRICT dest, const uint8_t *src, size_t n);
  487. extern uint16_t *
  488. u16_stpncpy (uint16_t *_UC_RESTRICT dest, const uint16_t *src, size_t n);
  489. extern uint32_t *
  490. u32_stpncpy (uint32_t *_UC_RESTRICT dest, const uint32_t *src, size_t n);
  491. /* Append SRC onto DEST. */
  492. /* Similar to strcat(), wcscat(). */
  493. extern uint8_t *
  494. u8_strcat (uint8_t *_UC_RESTRICT dest, const uint8_t *src);
  495. extern uint16_t *
  496. u16_strcat (uint16_t *_UC_RESTRICT dest, const uint16_t *src);
  497. extern uint32_t *
  498. u32_strcat (uint32_t *_UC_RESTRICT dest, const uint32_t *src);
  499. /* Append no more than N units of SRC onto DEST. */
  500. /* Similar to strncat(), wcsncat(). */
  501. extern uint8_t *
  502. u8_strncat (uint8_t *_UC_RESTRICT dest, const uint8_t *src, size_t n);
  503. extern uint16_t *
  504. u16_strncat (uint16_t *_UC_RESTRICT dest, const uint16_t *src, size_t n);
  505. extern uint32_t *
  506. u32_strncat (uint32_t *_UC_RESTRICT dest, const uint32_t *src, size_t n);
  507. /* Compare S1 and S2. */
  508. /* Similar to strcmp(), wcscmp(). */
  509. #ifdef __sun
  510. /* Avoid a collision with the u8_strcmp() function in Solaris 11 libc. */
  511. extern int
  512. u8_strcmp_gnu (const uint8_t *s1, const uint8_t *s2)
  513. _UC_ATTRIBUTE_PURE;
  514. # define u8_strcmp u8_strcmp_gnu
  515. #else
  516. extern int
  517. u8_strcmp (const uint8_t *s1, const uint8_t *s2)
  518. _UC_ATTRIBUTE_PURE;
  519. #endif
  520. extern int
  521. u16_strcmp (const uint16_t *s1, const uint16_t *s2)
  522. _UC_ATTRIBUTE_PURE;
  523. extern int
  524. u32_strcmp (const uint32_t *s1, const uint32_t *s2)
  525. _UC_ATTRIBUTE_PURE;
  526. /* Compare S1 and S2 using the collation rules of the current locale.
  527. Return -1 if S1 < S2, 0 if S1 = S2, 1 if S1 > S2.
  528. Upon failure, set errno and return any value. */
  529. /* Similar to strcoll(), wcscoll(). */
  530. extern int
  531. u8_strcoll (const uint8_t *s1, const uint8_t *s2);
  532. extern int
  533. u16_strcoll (const uint16_t *s1, const uint16_t *s2);
  534. extern int
  535. u32_strcoll (const uint32_t *s1, const uint32_t *s2);
  536. /* Compare no more than N units of S1 and S2. */
  537. /* Similar to strncmp(), wcsncmp(). */
  538. extern int
  539. u8_strncmp (const uint8_t *s1, const uint8_t *s2, size_t n)
  540. _UC_ATTRIBUTE_PURE;
  541. extern int
  542. u16_strncmp (const uint16_t *s1, const uint16_t *s2, size_t n)
  543. _UC_ATTRIBUTE_PURE;
  544. extern int
  545. u32_strncmp (const uint32_t *s1, const uint32_t *s2, size_t n)
  546. _UC_ATTRIBUTE_PURE;
  547. /* Duplicate S, returning an identical malloc'd string. */
  548. /* Similar to strdup(), wcsdup(). */
  549. extern uint8_t *
  550. u8_strdup (const uint8_t *s);
  551. extern uint16_t *
  552. u16_strdup (const uint16_t *s);
  553. extern uint32_t *
  554. u32_strdup (const uint32_t *s);
  555. /* Find the first occurrence of UC in STR. */
  556. /* Similar to strchr(), wcschr(). */
  557. extern uint8_t *
  558. u8_strchr (const uint8_t *str, ucs4_t uc)
  559. _UC_ATTRIBUTE_PURE;
  560. extern uint16_t *
  561. u16_strchr (const uint16_t *str, ucs4_t uc)
  562. _UC_ATTRIBUTE_PURE;
  563. extern uint32_t *
  564. u32_strchr (const uint32_t *str, ucs4_t uc)
  565. _UC_ATTRIBUTE_PURE;
  566. /* Find the last occurrence of UC in STR. */
  567. /* Similar to strrchr(), wcsrchr(). */
  568. extern uint8_t *
  569. u8_strrchr (const uint8_t *str, ucs4_t uc)
  570. _UC_ATTRIBUTE_PURE;
  571. extern uint16_t *
  572. u16_strrchr (const uint16_t *str, ucs4_t uc)
  573. _UC_ATTRIBUTE_PURE;
  574. extern uint32_t *
  575. u32_strrchr (const uint32_t *str, ucs4_t uc)
  576. _UC_ATTRIBUTE_PURE;
  577. /* Return the length of the initial segment of STR which consists entirely
  578. of Unicode characters not in REJECT. */
  579. /* Similar to strcspn(), wcscspn(). */
  580. extern size_t
  581. u8_strcspn (const uint8_t *str, const uint8_t *reject)
  582. _UC_ATTRIBUTE_PURE;
  583. extern size_t
  584. u16_strcspn (const uint16_t *str, const uint16_t *reject)
  585. _UC_ATTRIBUTE_PURE;
  586. extern size_t
  587. u32_strcspn (const uint32_t *str, const uint32_t *reject)
  588. _UC_ATTRIBUTE_PURE;
  589. /* Return the length of the initial segment of STR which consists entirely
  590. of Unicode characters in ACCEPT. */
  591. /* Similar to strspn(), wcsspn(). */
  592. extern size_t
  593. u8_strspn (const uint8_t *str, const uint8_t *accept)
  594. _UC_ATTRIBUTE_PURE;
  595. extern size_t
  596. u16_strspn (const uint16_t *str, const uint16_t *accept)
  597. _UC_ATTRIBUTE_PURE;
  598. extern size_t
  599. u32_strspn (const uint32_t *str, const uint32_t *accept)
  600. _UC_ATTRIBUTE_PURE;
  601. /* Find the first occurrence in STR of any character in ACCEPT. */
  602. /* Similar to strpbrk(), wcspbrk(). */
  603. extern uint8_t *
  604. u8_strpbrk (const uint8_t *str, const uint8_t *accept)
  605. _UC_ATTRIBUTE_PURE;
  606. extern uint16_t *
  607. u16_strpbrk (const uint16_t *str, const uint16_t *accept)
  608. _UC_ATTRIBUTE_PURE;
  609. extern uint32_t *
  610. u32_strpbrk (const uint32_t *str, const uint32_t *accept)
  611. _UC_ATTRIBUTE_PURE;
  612. /* Find the first occurrence of NEEDLE in HAYSTACK. */
  613. /* Similar to strstr(), wcsstr(). */
  614. extern uint8_t *
  615. u8_strstr (const uint8_t *haystack, const uint8_t *needle)
  616. _UC_ATTRIBUTE_PURE;
  617. extern uint16_t *
  618. u16_strstr (const uint16_t *haystack, const uint16_t *needle)
  619. _UC_ATTRIBUTE_PURE;
  620. extern uint32_t *
  621. u32_strstr (const uint32_t *haystack, const uint32_t *needle)
  622. _UC_ATTRIBUTE_PURE;
  623. /* Test whether STR starts with PREFIX. */
  624. extern bool
  625. u8_startswith (const uint8_t *str, const uint8_t *prefix)
  626. _UC_ATTRIBUTE_PURE;
  627. extern bool
  628. u16_startswith (const uint16_t *str, const uint16_t *prefix)
  629. _UC_ATTRIBUTE_PURE;
  630. extern bool
  631. u32_startswith (const uint32_t *str, const uint32_t *prefix)
  632. _UC_ATTRIBUTE_PURE;
  633. /* Test whether STR ends with SUFFIX. */
  634. extern bool
  635. u8_endswith (const uint8_t *str, const uint8_t *suffix)
  636. _UC_ATTRIBUTE_PURE;
  637. extern bool
  638. u16_endswith (const uint16_t *str, const uint16_t *suffix)
  639. _UC_ATTRIBUTE_PURE;
  640. extern bool
  641. u32_endswith (const uint32_t *str, const uint32_t *suffix)
  642. _UC_ATTRIBUTE_PURE;
  643. /* Divide STR into tokens separated by characters in DELIM.
  644. This interface is actually more similar to wcstok than to strtok. */
  645. /* Similar to strtok_r(), wcstok(). */
  646. extern uint8_t *
  647. u8_strtok (uint8_t *_UC_RESTRICT str, const uint8_t *delim,
  648. uint8_t **ptr);
  649. extern uint16_t *
  650. u16_strtok (uint16_t *_UC_RESTRICT str, const uint16_t *delim,
  651. uint16_t **ptr);
  652. extern uint32_t *
  653. u32_strtok (uint32_t *_UC_RESTRICT str, const uint32_t *delim,
  654. uint32_t **ptr);
  655. #ifdef __cplusplus
  656. }
  657. #endif
  658. #endif /* _UNISTR_H */