unistr.in.h 23 KB

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