1 #include <libnu/defines.h>
2 #include <libnu/strings.h>
3
4 #if defined (NU_WITH_Z_STRINGS) || defined(NU_WITH_N_STRINGS)
5
_nu_strlen(const char * encoded,const char * limit,nu_read_iterator_t it)6 static ssize_t _nu_strlen(const char *encoded, const char *limit, nu_read_iterator_t it) {
7 ssize_t len = 0;
8
9 const char *p = encoded;
10 while (p < limit) {
11 uint32_t u = 0;
12 p = it(p, &u);
13
14 if (u == 0) {
15 break;
16 }
17
18 ++len;
19 }
20
21 return len;
22 }
23
_nu_bytelen(const uint32_t * unicode,const uint32_t * limit,nu_write_iterator_t it)24 static ssize_t _nu_bytelen(const uint32_t *unicode, const uint32_t *limit, nu_write_iterator_t it) {
25 ssize_t len = 0;
26
27 const uint32_t *p = unicode;
28 while (p < limit) {
29 if (*p == 0) {
30 break;
31 }
32
33 /* nu_write_iterator_t will return offset relative to 0
34 * which is effectively bytes length of codepoint */
35 size_t byte_len = (size_t)it(*p, 0);
36 len += byte_len;
37
38 ++p;
39 }
40
41 return len;
42 }
43
_nu_strbytelen(const char * encoded,const char * limit,nu_read_iterator_t it)44 static ssize_t _nu_strbytelen(const char *encoded, const char *limit, nu_read_iterator_t it) {
45 uint32_t u = 0;
46 const char *p = encoded;
47
48 while (p < limit) {
49 const char *np = it(p, &u);
50
51 if (u == 0) {
52 return (p - encoded);
53 }
54
55 p = np;
56 }
57
58 return 0;
59 }
60
61 #endif /* NU_WITH_N_STRINGS || NU_WITH_Z_STRINGS */
62
63 #ifdef NU_WITH_Z_STRINGS
64
nu_strlen(const char * encoded,nu_read_iterator_t it)65 ssize_t nu_strlen(const char *encoded, nu_read_iterator_t it) {
66 return _nu_strlen(encoded, NU_UNLIMITED, it);
67 }
68
nu_bytelen(const uint32_t * unicode,nu_write_iterator_t it)69 ssize_t nu_bytelen(const uint32_t *unicode, nu_write_iterator_t it) {
70 return _nu_bytelen(unicode, NU_UNLIMITED, it);
71 }
72
nu_strbytelen(const char * encoded,nu_read_iterator_t it)73 ssize_t nu_strbytelen(const char *encoded, nu_read_iterator_t it) {
74 return _nu_strbytelen(encoded, NU_UNLIMITED, it);
75 }
76
77 #endif /* NU_WITH_Z_STRINGS */
78
79 #ifdef NU_WITH_N_STRINGS
80
nu_strnlen(const char * encoded,size_t max_len,nu_read_iterator_t it)81 ssize_t nu_strnlen(const char *encoded, size_t max_len, nu_read_iterator_t it) {
82 return _nu_strlen(encoded, encoded + max_len, it);
83 }
84
nu_bytenlen(const uint32_t * unicode,size_t max_len,nu_write_iterator_t it)85 ssize_t nu_bytenlen(const uint32_t *unicode, size_t max_len, nu_write_iterator_t it) {
86 return _nu_bytelen(unicode, unicode + max_len, it);
87 }
88
89 #endif /* NU_WITH_N_STRINGS */
90