3 unsigned int a_utf_encode(void *_str
, a_u32 val
)
6 unsigned int offset
= 0;
7 a_u32 x
= val
& A_U32_C(0x7FFFFFFF);
8 if (x
< A_U32_C(0x0010000))
10 if (x
< A_U32_C(0x0000800))
12 if (x
< A_U32_C(0x0000080) && x
)
14 offset
= 1; /* U+0000001 ~ U+0000007F */
18 offset
= 2; /* U+0000080 ~ U+000007FF */
24 offset
= 3; /* U+0000800 ~ U+0000FFFF */
30 if (x
< A_U32_C(0x0200000))
32 offset
= 4; /* U+0010000 ~ U+001FFFFF */
37 if (x
< A_U32_C(0x4000000))
39 offset
= 5; /* U+0200000 ~ U+03FFFFFF */
44 offset
= 6; /* U+4000000 ~ U+7FFFFFFF */
51 a_byte
*const str
= (a_byte
*)_str
;
55 str
[5] = (a_byte
)(0x80 | (x
& 0x3F));
59 str
[4] = (a_byte
)(0x80 | (x
& 0x3F));
63 str
[3] = (a_byte
)(0x80 | (x
& 0x3F));
67 str
[2] = (a_byte
)(0x80 | (x
& 0x3F));
71 str
[1] = (a_byte
)(0x80 | (x
& 0x3F));
75 str
[0] = (a_byte
)(mask
| x
);
84 unsigned int a_utf_decode(void const *_str
, a_u32
*val
)
86 a_byte
const *str
= (a_byte
const *)_str
;
87 unsigned int offset
= 0;
88 unsigned int chr
= *str
;
93 if (!chr
) { return offset
; }
97 for (; chr
& 0x40; chr
<<= 1)
99 unsigned int c
= *(++str
);
100 if ((c
& 0xC0) != 0x80) { return offset
; }
101 res
= (res
<< 6) | (c
& 0x3F);
103 offset
= (unsigned int)(str
- (a_byte
const *)_str
);
104 res
|= (a_u32
)(chr
& 0x7F) << (offset
* 5);
106 if (val
) { *val
= res
; }
110 a_size
a_utf_length(void const *_str
)
113 char const *str
= (char const *)_str
;
114 unsigned int offset
= a_utf_decode(str
, A_NULL
);
115 for (; offset
; offset
= a_utf_decode(str
, A_NULL
))