comparison foosdk/sdk/pfc/utf8.cpp @ 1:20d02a178406 default tip

*: check in everything else yay
author Paper <paper@tflc.us>
date Mon, 05 Jan 2026 02:15:46 -0500
parents
children
comparison
equal deleted inserted replaced
0:e9bb126753e7 1:20d02a178406
1 #include "pfc-lite.h"
2 #include "string_base.h"
3
4 namespace pfc {
5 //utf8 stuff
6 #include "pocket_char_ops.h"
7
8 #ifdef _MSC_VER
9 t_size utf16_decode_char(const wchar_t * p_source,unsigned * p_out,t_size p_source_length) throw() {
10 PFC_STATIC_ASSERT( sizeof(wchar_t) == sizeof(char16_t) );
11 return wide_decode_char( p_source, p_out, p_source_length );
12 }
13 t_size utf16_encode_char(unsigned c,wchar_t * out) throw() {
14 PFC_STATIC_ASSERT( sizeof(wchar_t) == sizeof(char16_t) );
15 return wide_encode_char( c, out );
16 }
17 #endif
18
19 t_size wide_decode_char(const wchar_t * p_source,unsigned * p_out,t_size p_source_length) throw() {
20 PFC_STATIC_ASSERT( sizeof( wchar_t ) == sizeof( char16_t ) || sizeof( wchar_t ) == sizeof( unsigned ) );
21 if constexpr (sizeof( wchar_t ) == sizeof( char16_t ) ) {
22 return utf16_decode_char( reinterpret_cast< const char16_t *>(p_source), p_out, p_source_length );
23 } else {
24 if (p_source_length == 0) { * p_out = 0; return 0; }
25 * p_out = p_source [ 0 ];
26 return 1;
27 }
28 }
29 t_size wide_encode_char(unsigned c,wchar_t * out) throw() {
30 PFC_STATIC_ASSERT( sizeof( wchar_t ) == sizeof( char16_t ) || sizeof( wchar_t ) == sizeof( unsigned ) );
31 if constexpr (sizeof( wchar_t ) == sizeof( char16_t ) ) {
32 return utf16_encode_char( c, reinterpret_cast< char16_t * >(out) );
33 } else {
34 * out = (wchar_t) c;
35 return 1;
36 }
37 }
38
39 size_t uni_decode_char(const char16_t * p_source, unsigned & p_out, size_t p_source_length) noexcept {
40 return utf16_decode_char(p_source, &p_out, p_source_length);
41 }
42 size_t uni_decode_char(const char * p_source, unsigned & p_out, size_t p_source_length) noexcept {
43 return utf8_decode_char(p_source, p_out, p_source_length);
44 }
45 size_t uni_decode_char(const wchar_t * p_source, unsigned & p_out, size_t p_source_length) noexcept {
46 if constexpr ( sizeof(wchar_t) == sizeof(char16_t)) {
47 return utf16_decode_char( reinterpret_cast<const char16_t*>(p_source), &p_out, p_source_length);
48 } else {
49 if (p_source_length > 0) {
50 unsigned c = (unsigned)*p_source;
51 if (c != 0) {
52 p_out = c; return 1;
53 }
54 }
55 p_out = 0; return 0;
56 }
57 }
58
59 size_t uni_char_length(const char * arg) {
60 return utf8_char_len(arg);
61 }
62 size_t uni_char_length(const char16_t * arg) {
63 unsigned dontcare;
64 return utf16_decode_char(arg, &dontcare);
65 }
66 size_t uni_char_length(const wchar_t * arg) {
67 if constexpr ( sizeof(wchar_t) == sizeof(char16_t) ) {
68 unsigned dontcare;
69 return utf16_decode_char(reinterpret_cast<const char16_t*>(arg), &dontcare);
70 } else {
71 return *arg == 0 ? 0 : 1;
72 }
73 }
74
75 size_t uni_encode_char(unsigned c, char* out) noexcept {
76 PFC_ASSERT(c != 0);
77 return utf8_encode_char(c, out);
78 }
79 size_t uni_encode_char(unsigned c, char16_t* out) noexcept {
80 PFC_ASSERT(c != 0);
81 return utf16_encode_char(c, out);
82 }
83 size_t uni_encode_char(unsigned c, wchar_t* out) noexcept {
84 PFC_ASSERT(c != 0);
85 if constexpr ( sizeof(wchar_t) == sizeof(char16_t)) {
86 return utf16_encode_char(c, reinterpret_cast<char16_t*>(out));
87 } else {
88 *out = (wchar_t)c; return 1;
89 }
90 }
91
92
93 bool is_lower_ascii(const char * param)
94 {
95 while(*param)
96 {
97 if (*param<0) return false;
98 param++;
99 }
100 return true;
101 }
102
103 static bool check_end_of_string(const char * ptr)
104 {
105 return !*ptr;
106 }
107
108 size_t strcpy_utf8_truncate(const char * src,char * out,size_t maxbytes)
109 {
110 size_t rv = 0 , ptr = 0;
111 if (maxbytes>0)
112 {
113 maxbytes--;//for null
114 while(!check_end_of_string(src) && maxbytes>0)
115 {
116 size_t delta = utf8_char_len(src);
117 if (delta>maxbytes || delta==0) break;
118 maxbytes -= delta;
119 do
120 {
121 out[ptr++] = *(src++);
122 } while(--delta);
123 rv = ptr;
124 }
125 out[rv]=0;
126 }
127 return rv;
128 }
129
130 t_size strlen_utf8(const char * p,t_size num) noexcept
131 {
132 unsigned w;
133 t_size d;
134 t_size ret = 0;
135 for(;num;)
136 {
137 d = utf8_decode_char(p,w);
138 if (w==0 || d<=0) break;
139 ret++;
140 p+=d;
141 num-=d;
142 }
143 return ret;
144 }
145
146 t_size utf8_chars_to_bytes(const char * string,t_size count) noexcept
147 {
148 t_size bytes = 0;
149 while(count)
150 {
151 unsigned dummy;
152 t_size delta = utf8_decode_char(string+bytes,dummy);
153 if (delta==0) break;
154 bytes += delta;
155 count--;
156 }
157 return bytes;
158 }
159
160 }