Rev 4221 | Rev 4420 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 4221 | Rev 4296 | ||
---|---|---|---|
Line 30... | Line 30... | ||
30 | * @{ |
30 | * @{ |
31 | */ |
31 | */ |
32 | 32 | ||
33 | /** |
33 | /** |
34 | * @file |
34 | * @file |
35 | * @brief Miscellaneous functions. |
35 | * @brief String functions. |
- | 36 | * |
|
- | 37 | * Strings and characters use the Universal Character Set (UCS). The standard |
|
- | 38 | * strings, called just strings are encoded in UTF-8. Wide strings (encoded |
|
- | 39 | * in UTF-32) are supported to a limited degree. A single character is |
|
- | 40 | * represented as wchar_t.@n |
|
- | 41 | * |
|
- | 42 | * Overview of the terminology:@n |
|
- | 43 | * |
|
- | 44 | * Term Meaning |
|
- | 45 | * -------------------- ---------------------------------------------------- |
|
- | 46 | * byte 8 bits stored in uint8_t (unsigned 8 bit integer) |
|
- | 47 | * |
|
- | 48 | * character UTF-32 encoded Unicode character, stored in wchar_t |
|
- | 49 | * (signed 32 bit integer), code points 0 .. 1114111 |
|
- | 50 | * are valid |
|
- | 51 | * |
|
- | 52 | * ASCII character 7 bit encoded ASCII character, stored in char |
|
- | 53 | * (usually signed 8 bit integer), code points 0 .. 127 |
|
- | 54 | * are valid |
|
- | 55 | * |
|
- | 56 | * string UTF-8 encoded NULL-terminated Unicode string, char * |
|
- | 57 | * |
|
- | 58 | * wide string UTF-32 encoded NULL-terminated Unicode string, |
|
- | 59 | * wchar_t * |
|
- | 60 | * |
|
- | 61 | * [wide] string size number of BYTES in a [wide] string (excluding |
|
- | 62 | * the NULL-terminator), size_t |
|
- | 63 | * |
|
- | 64 | * [wide] string length number of CHARACTERS in a [wide] string (excluding |
|
- | 65 | * the NULL-terminator), count_t |
|
- | 66 | * |
|
- | 67 | * [wide] string width number of display cells on a monospace display taken |
|
- | 68 | * by a [wide] string, count_t |
|
- | 69 | * |
|
- | 70 | * |
|
- | 71 | * Overview of string metrics:@n |
|
- | 72 | * |
|
- | 73 | * Metric Abbrev. Type Meaning |
|
- | 74 | * ------ ------ ------ ------------------------------------------------- |
|
- | 75 | * size n size_t number of BYTES in a string (excluding the |
|
- | 76 | * NULL-terminator) |
|
- | 77 | * |
|
- | 78 | * length l count_t number of CHARACTERS in a string (excluding the |
|
- | 79 | * null terminator) |
|
- | 80 | * |
|
- | 81 | * width w count_t number of display cells on a monospace display |
|
- | 82 | * taken by a string |
|
- | 83 | * |
|
- | 84 | * |
|
- | 85 | * Function naming prefixes:@n |
|
- | 86 | * |
|
- | 87 | * chr_ operate on characters |
|
- | 88 | * ascii_ operate on ASCII characters |
|
- | 89 | * str_ operate on strings |
|
- | 90 | * wstr_ operate on wide strings |
|
- | 91 | * |
|
- | 92 | * [w]str_[n|l|w] operate on a prefix limited by size, length |
|
- | 93 | * or width |
|
- | 94 | * |
|
- | 95 | * |
|
- | 96 | * A specific character inside a [wide] string can be referred to by:@n |
|
- | 97 | * |
|
- | 98 | * pointer (char *, wchar_t *) |
|
- | 99 | * byte offset (size_t) |
|
- | 100 | * character index (count_t) |
|
- | 101 | * |
|
36 | */ |
102 | */ |
37 | 103 | ||
38 | #include <string.h> |
104 | #include <string.h> |
39 | #include <print.h> |
105 | #include <print.h> |
40 | #include <cpu.h> |
106 | #include <cpu.h> |
41 | #include <arch/asm.h> |
107 | #include <arch/asm.h> |
42 | #include <arch.h> |
108 | #include <arch.h> |
43 | #include <console/kconsole.h> |
109 | #include <errno.h> |
44 | - | ||
45 | char invalch = '?'; |
110 | #include <align.h> |
- | 111 | #include <debug.h> |
|
46 | 112 | ||
47 | /** Byte mask consisting of lowest @n bits (out of eight). */ |
113 | /** Byte mask consisting of lowest @n bits (out of 8) */ |
48 | #define LO_MASK_8(n) ((uint8_t)((1 << (n)) - 1)) |
114 | #define LO_MASK_8(n) ((uint8_t) ((1 << (n)) - 1)) |
49 | 115 | ||
50 | /** Byte mask consisting of lowest @n bits (out of 32). */ |
116 | /** Byte mask consisting of lowest @n bits (out of 32) */ |
51 | #define LO_MASK_32(n) ((uint32_t)((1 << (n)) - 1)) |
117 | #define LO_MASK_32(n) ((uint32_t) ((1 << (n)) - 1)) |
52 | 118 | ||
53 | /** Byte mask consisting of highest @n bits (out of eight). */ |
119 | /** Byte mask consisting of highest @n bits (out of 8) */ |
54 | #define HI_MASK_8(n) (~LO_MASK_8(8 - (n))) |
120 | #define HI_MASK_8(n) (~LO_MASK_8(8 - (n))) |
55 | 121 | ||
56 | /** Number of data bits in a UTF-8 continuation byte. */ |
122 | /** Number of data bits in a UTF-8 continuation byte */ |
57 | #define CONT_BITS 6 |
123 | #define CONT_BITS 6 |
58 | 124 | ||
59 | /** Decode a single character from a substring. |
125 | /** Decode a single character from a string. |
60 | * |
126 | * |
61 | * Decode a single character from a substring of size @a sz. Decoding starts |
127 | * Decode a single character from a string of size @a size. Decoding starts |
62 | * at @a offset and this offset is moved to the beginning of the next |
128 | * at @a offset and this offset is moved to the beginning of the next |
63 | * character. In case of decoding error, offset generally advances at least |
129 | * character. In case of decoding error, offset generally advances at least |
64 | * by one. However, offset is never moved beyond (str + sz). |
130 | * by one. However, offset is never moved beyond size. |
65 | * |
131 | * |
66 | * @param str String (not necessarily NULL-terminated). |
132 | * @param str String (not necessarily NULL-terminated). |
67 | * @param index Index (counted in plain characters) where to start |
133 | * @param offset Byte offset in string where to start decoding. |
68 | * the decoding. |
- | |
69 | * @param limit Size of the substring. |
134 | * @param size Size of the string (in bytes). |
70 | * |
135 | * |
71 | * @return Value of decoded character or '?' on decoding error. |
136 | * @return Value of decoded character, U_SPECIAL on decoding error or |
- | 137 | * NULL if attempt to decode beyond @a size. |
|
72 | * |
138 | * |
73 | */ |
139 | */ |
74 | wchar_t chr_decode(const char *str, size_t *offset, size_t sz) |
140 | wchar_t str_decode(const char *str, size_t *offset, size_t size) |
75 | { |
141 | { |
76 | uint8_t b0, b; /* Bytes read from str. */ |
142 | if (*offset + 1 > size) |
77 | wchar_t ch; |
143 | return 0; |
78 | 144 | ||
79 | int b0_bits; /* Data bits in first byte. */ |
145 | /* First byte read from string */ |
80 | int cbytes; /* Number of continuation bytes. */ |
146 | uint8_t b0 = (uint8_t) str[(*offset)++]; |
81 | 147 | ||
82 | if (*offset + 1 > sz) |
- | |
83 | return invalch; |
148 | /* Determine code length */ |
84 | 149 | ||
85 | b0 = (uint8_t) str[(*offset)++]; |
150 | unsigned int b0_bits; /* Data bits in first byte */ |
86 | - | ||
87 | /* Determine code length. */ |
151 | unsigned int cbytes; /* Number of continuation bytes */ |
88 | 152 | ||
89 | if ((b0 & 0x80) == 0) { |
153 | if ((b0 & 0x80) == 0) { |
90 | /* 0xxxxxxx (Plain ASCII) */ |
154 | /* 0xxxxxxx (Plain ASCII) */ |
91 | b0_bits = 7; |
155 | b0_bits = 7; |
92 | cbytes = 0; |
156 | cbytes = 0; |
93 | } else if ((b0 & 0xe0) == 0xc0) { |
157 | } else if ((b0 & 0xe0) == 0xc0) { |
Line 101... | Line 165... | ||
101 | } else if ((b0 & 0xf8) == 0xf0) { |
165 | } else if ((b0 & 0xf8) == 0xf0) { |
102 | /* 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */ |
166 | /* 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */ |
103 | b0_bits = 3; |
167 | b0_bits = 3; |
104 | cbytes = 3; |
168 | cbytes = 3; |
105 | } else { |
169 | } else { |
106 | /* 10xxxxxx -- unexpected continuation byte. */ |
170 | /* 10xxxxxx -- unexpected continuation byte */ |
107 | return invalch; |
- | |
108 | } |
- | |
109 | - | ||
110 | if (*offset + cbytes > sz) { |
- | |
111 | return invalch; |
171 | return U_SPECIAL; |
112 | } |
172 | } |
- | 173 | ||
- | 174 | if (*offset + cbytes > size) |
|
- | 175 | return U_SPECIAL; |
|
113 | 176 | ||
114 | ch = b0 & LO_MASK_8(b0_bits); |
177 | wchar_t ch = b0 & LO_MASK_8(b0_bits); |
115 | 178 | ||
116 | /* Decode continuation bytes. */ |
179 | /* Decode continuation bytes */ |
117 | while (cbytes > 0) { |
180 | while (cbytes > 0) { |
118 | b = (uint8_t) str[(*offset)++]; |
181 | uint8_t b = (uint8_t) str[(*offset)++]; |
119 | 182 | ||
120 | /* Must be 10xxxxxx. */ |
183 | /* Must be 10xxxxxx */ |
121 | if ((b & 0xc0) != 0x80) { |
184 | if ((b & 0xc0) != 0x80) |
122 | return invalch; |
185 | return U_SPECIAL; |
123 | } |
186 | |
124 | - | ||
125 | /* Shift data bits to ch. */ |
187 | /* Shift data bits to ch */ |
126 | ch = (ch << CONT_BITS) | (wchar_t) (b & LO_MASK_8(CONT_BITS)); |
188 | ch = (ch << CONT_BITS) | (wchar_t) (b & LO_MASK_8(CONT_BITS)); |
127 | --cbytes; |
189 | cbytes--; |
128 | } |
190 | } |
129 | 191 | ||
130 | return ch; |
192 | return ch; |
131 | } |
193 | } |
132 | 194 | ||
133 | /** Encode a single character to string representation. |
195 | /** Encode a single character to string representation. |
134 | * |
196 | * |
135 | * Encode a single character to string representation (i.e. UTF-8) and store |
197 | * Encode a single character to string representation (i.e. UTF-8) and store |
136 | * it into a buffer at @a offset. Encoding starts at @a offset and this offset |
198 | * it into a buffer at @a offset. Encoding starts at @a offset and this offset |
137 | * is moved to the position where the next character can be written to. |
199 | * is moved to the position where the next character can be written to. |
138 | * |
200 | * |
139 | * @param ch Input character. |
201 | * @param ch Input character. |
140 | * @param str Output buffer. |
202 | * @param str Output buffer. |
141 | * @param offset Offset (in bytes) where to start writing. |
203 | * @param offset Byte offset where to start writing. |
142 | * @param sz Size of the output buffer. |
204 | * @param size Size of the output buffer (in bytes). |
143 | * |
205 | * |
144 | * @return True if the character was encoded successfully or false if there |
206 | * @return EOK if the character was encoded successfully, EOVERFLOW if there |
145 | * was not enough space in the output buffer or the character code |
207 | * was not enough space in the output buffer or EINVAL if the character |
146 | * was invalid. |
208 | * code was invalid. |
147 | */ |
209 | */ |
148 | bool chr_encode(const wchar_t ch, char *str, size_t *offset, size_t sz) |
210 | int chr_encode(wchar_t ch, char *str, size_t *offset, size_t size) |
149 | { |
211 | { |
150 | uint32_t cc; /* Unsigned version of ch. */ |
- | |
151 | - | ||
152 | int cbytes; /* Number of continuation bytes. */ |
- | |
153 | int b0_bits; /* Number of data bits in first byte. */ |
- | |
154 | int i; |
- | |
155 | - | ||
156 | if (*offset >= sz) |
212 | if (*offset >= size) |
157 | return false; |
213 | return EOVERFLOW; |
158 | 214 | ||
159 | if (ch < 0) |
215 | if (!chr_check(ch)) |
160 | return false; |
216 | return EINVAL; |
161 | 217 | ||
162 | /* Bit operations should only be done on unsigned numbers. */ |
218 | /* Unsigned version of ch (bit operations should only be done |
- | 219 | on unsigned types). */ |
|
163 | cc = (uint32_t) ch; |
220 | uint32_t cc = (uint32_t) ch; |
164 | 221 | ||
165 | /* Determine how many continuation bytes are needed. */ |
222 | /* Determine how many continuation bytes are needed */ |
- | 223 | ||
- | 224 | unsigned int b0_bits; /* Data bits in first byte */ |
|
- | 225 | unsigned int cbytes; /* Number of continuation bytes */ |
|
- | 226 | ||
166 | if ((cc & ~LO_MASK_32(7)) == 0) { |
227 | if ((cc & ~LO_MASK_32(7)) == 0) { |
167 | b0_bits = 7; |
228 | b0_bits = 7; |
168 | cbytes = 0; |
229 | cbytes = 0; |
169 | } else if ((cc & ~LO_MASK_32(11)) == 0) { |
230 | } else if ((cc & ~LO_MASK_32(11)) == 0) { |
170 | b0_bits = 5; |
231 | b0_bits = 5; |
Line 174... | Line 235... | ||
174 | cbytes = 2; |
235 | cbytes = 2; |
175 | } else if ((cc & ~LO_MASK_32(21)) == 0) { |
236 | } else if ((cc & ~LO_MASK_32(21)) == 0) { |
176 | b0_bits = 3; |
237 | b0_bits = 3; |
177 | cbytes = 3; |
238 | cbytes = 3; |
178 | } else { |
239 | } else { |
179 | /* Codes longer than 21 bits are not supported. */ |
240 | /* Codes longer than 21 bits are not supported */ |
180 | return false; |
241 | return EINVAL; |
181 | } |
242 | } |
182 | 243 | ||
183 | /* Check for available space in buffer. */ |
244 | /* Check for available space in buffer */ |
184 | if (*offset + cbytes >= sz) |
245 | if (*offset + cbytes >= size) |
185 | return false; |
246 | return EOVERFLOW; |
186 | 247 | ||
187 | /* Encode continuation bytes. */ |
248 | /* Encode continuation bytes */ |
- | 249 | unsigned int i; |
|
188 | for (i = cbytes; i > 0; --i) { |
250 | for (i = cbytes; i > 0; i--) { |
189 | str[*offset + i] = 0x80 | (cc & LO_MASK_32(CONT_BITS)); |
251 | str[*offset + i] = 0x80 | (cc & LO_MASK_32(CONT_BITS)); |
190 | cc = cc >> CONT_BITS; |
252 | cc = cc >> CONT_BITS; |
191 | } |
253 | } |
192 | 254 | ||
193 | /* Encode first byte. */ |
255 | /* Encode first byte */ |
194 | str[*offset] = (cc & LO_MASK_32(b0_bits)) | HI_MASK_8(8 - b0_bits - 1); |
256 | str[*offset] = (cc & LO_MASK_32(b0_bits)) | HI_MASK_8(8 - b0_bits - 1); |
- | 257 | ||
- | 258 | /* Advance offset */ |
|
- | 259 | *offset += cbytes + 1; |
|
- | 260 | ||
- | 261 | return EOK; |
|
- | 262 | } |
|
195 | 263 | ||
196 | /* Advance offset. */ |
264 | /** Get size of string. |
- | 265 | * |
|
- | 266 | * Get the number of bytes which are used by the string @a str (excluding the |
|
- | 267 | * NULL-terminator). |
|
- | 268 | * |
|
- | 269 | * @param str String to consider. |
|
- | 270 | * |
|
- | 271 | * @return Number of bytes used by the string |
|
- | 272 | * |
|
- | 273 | */ |
|
- | 274 | size_t str_size(const char *str) |
|
- | 275 | { |
|
197 | *offset += (1 + cbytes); |
276 | size_t size = 0; |
198 | 277 | ||
- | 278 | while (*str++ != 0) |
|
- | 279 | size++; |
|
- | 280 | ||
199 | return true; |
281 | return size; |
- | 282 | } |
|
- | 283 | ||
- | 284 | /** Get size of wide string. |
|
- | 285 | * |
|
- | 286 | * Get the number of bytes which are used by the wide string @a str (excluding the |
|
- | 287 | * NULL-terminator). |
|
- | 288 | * |
|
- | 289 | * @param str Wide string to consider. |
|
- | 290 | * |
|
- | 291 | * @return Number of bytes used by the wide string |
|
- | 292 | * |
|
- | 293 | */ |
|
- | 294 | size_t wstr_size(const wchar_t *str) |
|
- | 295 | { |
|
- | 296 | return (wstr_length(str) * sizeof(wchar_t)); |
|
200 | } |
297 | } |
201 | 298 | ||
202 | /** Get size of string, with length limit. |
299 | /** Get size of string with length limit. |
203 | * |
300 | * |
204 | * Get the number of bytes which are used by up to @a max_len first |
301 | * Get the number of bytes which are used by up to @a max_len first |
205 | * characters in the string @a str. If @a max_len is greater than |
302 | * characters in the string @a str. If @a max_len is greater than |
206 | * the length of @a str, the entire string is measured. |
303 | * the length of @a str, the entire string is measured (excluding the |
- | 304 | * NULL-terminator). |
|
207 | * |
305 | * |
208 | * @param str String to consider. |
306 | * @param str String to consider. |
209 | * @param count Maximum number of characters to measure. |
307 | * @param max_len Maximum number of characters to measure. |
210 | * |
308 | * |
211 | * @return Number of bytes used by the characters. |
309 | * @return Number of bytes used by the characters. |
- | 310 | * |
|
212 | */ |
311 | */ |
213 | size_t str_lsize(const char *str, count_t max_len) |
312 | size_t str_lsize(const char *str, count_t max_len) |
214 | { |
313 | { |
215 | count_t len = 0; |
314 | count_t len = 0; |
216 | size_t cur = 0; |
315 | size_t offset = 0; |
217 | size_t prev; |
- | |
218 | wchar_t ch; |
- | |
219 | 316 | ||
220 | while (true) { |
317 | while (len < max_len) { |
221 | prev = cur; |
- | |
222 | if (len >= max_len) |
318 | if (str_decode(str, &offset, STR_NO_LIMIT) == 0) |
223 | break; |
319 | break; |
224 | ch = chr_decode(str, &cur, UTF8_NO_LIMIT); |
- | |
225 | if (ch == '\0') break; |
- | |
226 | 320 | ||
227 | len++; |
321 | len++; |
228 | } |
322 | } |
229 | 323 | ||
230 | return prev; |
324 | return offset; |
231 | } |
325 | } |
232 | 326 | ||
233 | /** Check whether character is plain ASCII. |
327 | /** Get size of wide string with length limit. |
234 | * |
328 | * |
- | 329 | * Get the number of bytes which are used by up to @a max_len first |
|
- | 330 | * wide characters in the wide string @a str. If @a max_len is greater than |
|
- | 331 | * the length of @a str, the entire wide string is measured (excluding the |
|
- | 332 | * NULL-terminator). |
|
- | 333 | * |
|
- | 334 | * @param str Wide string to consider. |
|
- | 335 | * @param max_len Maximum number of wide characters to measure. |
|
- | 336 | * |
|
235 | * @return True if character is plain ASCII. |
337 | * @return Number of bytes used by the wide characters. |
236 | * |
338 | * |
237 | */ |
339 | */ |
238 | bool ascii_check(const wchar_t ch) |
340 | size_t wstr_lsize(const wchar_t *str, count_t max_len) |
239 | { |
341 | { |
240 | if ((ch >= 0) && (ch <= 127)) |
342 | return (wstr_nlength(str, max_len * sizeof(wchar_t)) * sizeof(wchar_t)); |
241 | return true; |
- | |
242 | - | ||
243 | return false; |
- | |
244 | } |
343 | } |
245 | 344 | ||
246 | /** Check whether character is Unicode. |
345 | /** Get number of characters in a string. |
- | 346 | * |
|
- | 347 | * @param str NULL-terminated string. |
|
247 | * |
348 | * |
248 | * @return True if character is valid Unicode code point. |
349 | * @return Number of characters in string. |
249 | * |
350 | * |
250 | */ |
351 | */ |
251 | bool unicode_check(const wchar_t ch) |
352 | count_t str_length(const char *str) |
252 | { |
353 | { |
253 | if ((ch >= 0) && (ch <= 1114111)) |
354 | count_t len = 0; |
254 | return true; |
355 | size_t offset = 0; |
255 | 356 | ||
- | 357 | while (str_decode(str, &offset, STR_NO_LIMIT) != 0) |
|
- | 358 | len++; |
|
- | 359 | ||
256 | return false; |
360 | return len; |
257 | } |
361 | } |
258 | 362 | ||
259 | /** Return number of plain characters in a string. |
363 | /** Get number of characters in a wide string. |
260 | * |
364 | * |
261 | * @param str NULL-terminated string. |
365 | * @param str NULL-terminated wide string. |
262 | * |
366 | * |
263 | * @return Number of characters in @a str. |
367 | * @return Number of characters in @a str. |
264 | * |
368 | * |
265 | */ |
369 | */ |
266 | size_t strlen(const char *str) |
370 | count_t wstr_length(const wchar_t *wstr) |
267 | { |
371 | { |
268 | size_t size; |
372 | count_t len = 0; |
269 | for (size = 0; str[size]; size++); |
- | |
270 | 373 | ||
- | 374 | while (*wstr++ != 0) |
|
- | 375 | len++; |
|
- | 376 | ||
271 | return size; |
377 | return len; |
272 | } |
378 | } |
273 | 379 | ||
274 | /** Return number of characters in a string. |
380 | /** Get number of characters in a string with size limit. |
- | 381 | * |
|
- | 382 | * @param str NULL-terminated string. |
|
- | 383 | * @param size Maximum number of bytes to consider. |
|
275 | * |
384 | * |
276 | * @param str NULL-terminated string. |
- | |
277 | * @return Number of characters in string. |
385 | * @return Number of characters in string. |
- | 386 | * |
|
278 | */ |
387 | */ |
279 | count_t str_length(const char *str) |
388 | count_t str_nlength(const char *str, size_t size) |
280 | { |
389 | { |
281 | count_t len = 0; |
390 | count_t len = 0; |
282 | size_t offset = 0; |
391 | size_t offset = 0; |
- | 392 | ||
- | 393 | while (str_decode(str, &offset, size) != 0) |
|
- | 394 | len++; |
|
- | 395 | ||
- | 396 | return len; |
|
- | 397 | } |
|
283 | 398 | ||
- | 399 | /** Get number of characters in a string with size limit. |
|
- | 400 | * |
|
- | 401 | * @param str NULL-terminated string. |
|
- | 402 | * @param size Maximum number of bytes to consider. |
|
- | 403 | * |
|
- | 404 | * @return Number of characters in string. |
|
- | 405 | * |
|
- | 406 | */ |
|
- | 407 | count_t wstr_nlength(const wchar_t *str, size_t size) |
|
- | 408 | { |
|
- | 409 | count_t len = 0; |
|
- | 410 | count_t limit = ALIGN_DOWN(size, sizeof(wchar_t)); |
|
- | 411 | count_t offset = 0; |
|
- | 412 | ||
284 | while (chr_decode(str, &offset, UTF8_NO_LIMIT) != 0) { |
413 | while ((offset < limit) && (*str++ != 0)) { |
285 | len++; |
414 | len++; |
- | 415 | offset += sizeof(wchar_t); |
|
286 | } |
416 | } |
287 | 417 | ||
288 | return len; |
418 | return len; |
289 | } |
419 | } |
290 | 420 | ||
- | 421 | /** Check whether character is plain ASCII. |
|
- | 422 | * |
|
291 | /** Return number of characters in a wide string. |
423 | * @return True if character is plain ASCII. |
292 | * |
424 | * |
293 | * @param str NULL-terminated wide string. |
- | |
294 | * @return Number of characters in @a str. |
- | |
295 | */ |
425 | */ |
296 | count_t wstr_length(const wchar_t *wstr) |
426 | bool ascii_check(wchar_t ch) |
297 | { |
427 | { |
- | 428 | if ((ch >= 0) && (ch <= 127)) |
|
298 | count_t len; |
429 | return true; |
299 | 430 | ||
300 | len = 0; |
431 | return false; |
301 | while (*wstr++ != '\0') |
- | |
302 | ++len; |
432 | } |
303 | 433 | ||
- | 434 | /** Check whether character is valid |
|
- | 435 | * |
|
- | 436 | * @return True if character is a valid Unicode code point. |
|
- | 437 | * |
|
- | 438 | */ |
|
- | 439 | bool chr_check(wchar_t ch) |
|
- | 440 | { |
|
- | 441 | if ((ch >= 0) && (ch <= 1114111)) |
|
- | 442 | return true; |
|
- | 443 | ||
304 | return len; |
444 | return false; |
305 | } |
445 | } |
306 | 446 | ||
307 | /** Compare two NULL terminated strings |
447 | /** Compare two NULL terminated strings. |
308 | * |
448 | * |
309 | * Do a char-by-char comparison of two NULL terminated strings. |
449 | * Do a char-by-char comparison of two NULL-terminated strings. |
310 | * The strings are considered equal iff they consist of the same |
450 | * The strings are considered equal iff they consist of the same |
311 | * characters on the minimum of their lengths. |
451 | * characters on the minimum of their lengths. |
312 | * |
452 | * |
313 | * @param src First string to compare. |
453 | * @param s1 First string to compare. |
314 | * @param dst Second string to compare. |
454 | * @param s2 Second string to compare. |
315 | * |
455 | * |
316 | * @return 0 if the strings are equal, -1 if first is smaller, 1 if second smaller. |
456 | * @return 0 if the strings are equal, -1 if first is smaller, |
- | 457 | * 1 if second smaller. |
|
317 | * |
458 | * |
318 | */ |
459 | */ |
319 | int strcmp(const char *src, const char *dst) |
460 | int str_cmp(const char *s1, const char *s2) |
320 | { |
461 | { |
- | 462 | wchar_t c1 = 0; |
|
- | 463 | wchar_t c2 = 0; |
|
- | 464 | ||
- | 465 | size_t off1 = 0; |
|
- | 466 | size_t off2 = 0; |
|
- | 467 | ||
- | 468 | while (true) { |
|
- | 469 | c1 = str_decode(s1, &off1, STR_NO_LIMIT); |
|
321 | for (; *src && *dst; src++, dst++) { |
470 | c2 = str_decode(s2, &off2, STR_NO_LIMIT); |
- | 471 | ||
322 | if (*src < *dst) |
472 | if (c1 < c2) |
323 | return -1; |
473 | return -1; |
- | 474 | ||
324 | if (*src > *dst) |
475 | if (c1 > c2) |
325 | return 1; |
476 | return 1; |
- | 477 | ||
- | 478 | if (c1 == 0 || c2 == 0) |
|
- | 479 | break; |
|
326 | } |
480 | } |
327 | if (*src == *dst) |
- | |
328 | return 0; |
- | |
329 | - | ||
330 | if (!*src) |
- | |
331 | return -1; |
- | |
332 | - | ||
333 | return 1; |
- | |
334 | } |
- | |
335 | 481 | ||
- | 482 | return 0; |
|
- | 483 | } |
|
336 | 484 | ||
337 | /** Compare two NULL terminated strings |
485 | /** Compare two NULL terminated strings with length limit. |
338 | * |
486 | * |
339 | * Do a char-by-char comparison of two NULL terminated strings. |
487 | * Do a char-by-char comparison of two NULL-terminated strings. |
340 | * The strings are considered equal iff they consist of the same |
488 | * The strings are considered equal iff they consist of the same |
341 | * characters on the minimum of their lengths and specified maximal |
489 | * characters on the minimum of their lengths and the length limit. |
342 | * length. |
- | |
343 | * |
490 | * |
344 | * @param src First string to compare. |
491 | * @param s1 First string to compare. |
345 | * @param dst Second string to compare. |
492 | * @param s2 Second string to compare. |
346 | * @param len Maximal length for comparison. |
493 | * @param max_len Maximum number of characters to consider. |
347 | * |
494 | * |
348 | * @return 0 if the strings are equal, -1 if first is smaller, 1 if second smaller. |
495 | * @return 0 if the strings are equal, -1 if first is smaller, |
- | 496 | * 1 if second smaller. |
|
349 | * |
497 | * |
350 | */ |
498 | */ |
351 | int strncmp(const char *src, const char *dst, size_t len) |
499 | int str_lcmp(const char *s1, const char *s2, count_t max_len) |
352 | { |
500 | { |
- | 501 | wchar_t c1 = 0; |
|
- | 502 | wchar_t c2 = 0; |
|
- | 503 | ||
353 | unsigned int i; |
504 | size_t off1 = 0; |
- | 505 | size_t off2 = 0; |
|
354 | 506 | ||
- | 507 | count_t len = 0; |
|
- | 508 | ||
- | 509 | while (true) { |
|
- | 510 | if (len >= max_len) |
|
- | 511 | break; |
|
- | 512 | ||
- | 513 | c1 = str_decode(s1, &off1, STR_NO_LIMIT); |
|
355 | for (i = 0; (*src) && (*dst) && (i < len); src++, dst++, i++) { |
514 | c2 = str_decode(s2, &off2, STR_NO_LIMIT); |
- | 515 | ||
356 | if (*src < *dst) |
516 | if (c1 < c2) |
357 | return -1; |
517 | return -1; |
358 | 518 | ||
359 | if (*src > *dst) |
519 | if (c1 > c2) |
360 | return 1; |
520 | return 1; |
- | 521 | ||
- | 522 | if (c1 == 0 || c2 == 0) |
|
- | 523 | break; |
|
- | 524 | ||
- | 525 | ++len; |
|
361 | } |
526 | } |
- | 527 | ||
- | 528 | return 0; |
|
- | 529 | ||
- | 530 | } |
|
- | 531 | ||
- | 532 | /** Copy string. |
|
- | 533 | * |
|
- | 534 | * Copy source string @a src to destination buffer @a dest. |
|
- | 535 | * No more than @a size bytes are written. If the size of the output buffer |
|
- | 536 | * is at least one byte, the output string will always be well-formed, i.e. |
|
- | 537 | * null-terminated and containing only complete characters. |
|
- | 538 | * |
|
- | 539 | * @param dst Destination buffer. |
|
- | 540 | * @param count Size of the destination buffer (must be > 0). |
|
- | 541 | * @param src Source string. |
|
- | 542 | */ |
|
- | 543 | void str_cpy(char *dest, size_t size, const char *src) |
|
- | 544 | { |
|
- | 545 | wchar_t ch; |
|
- | 546 | size_t src_off; |
|
- | 547 | size_t dest_off; |
|
- | 548 | ||
- | 549 | /* There must be space for a null terminator in the buffer. */ |
|
- | 550 | ASSERT(size > 0); |
|
362 | 551 | ||
363 | if (i == len || *src == *dst) |
552 | src_off = 0; |
364 | return 0; |
553 | dest_off = 0; |
365 | 554 | ||
- | 555 | while ((ch = str_decode(src, &src_off, STR_NO_LIMIT)) != 0) { |
|
366 | if (!*src) |
556 | if (chr_encode(ch, dest, &dest_off, size - 1) != EOK) |
367 | return -1; |
557 | break; |
368 | 558 | } |
|
- | 559 | ||
369 | return 1; |
560 | dest[dest_off] = '\0'; |
370 | } |
561 | } |
371 | 562 | ||
- | 563 | /** Copy size-limited substring. |
|
- | 564 | * |
|
- | 565 | * Copy prefix of string @a src of max. size @a size to destination buffer |
|
- | 566 | * @a dest. No more than @a size bytes are written. The output string will |
|
- | 567 | * always be well-formed, i.e. null-terminated and containing only complete |
|
- | 568 | * characters. |
|
- | 569 | * |
|
- | 570 | * No more than @a n bytes are read from the input string, so it does not |
|
- | 571 | * have to be null-terminated. |
|
- | 572 | * |
|
- | 573 | * @param dst Destination buffer. |
|
- | 574 | * @param count Size of the destination buffer (must be > 0). |
|
- | 575 | * @param src Source string. |
|
- | 576 | * @param n Maximum number of bytes to read from @a src. |
|
- | 577 | */ |
|
- | 578 | void str_ncpy(char *dest, size_t size, const char *src, size_t n) |
|
- | 579 | { |
|
- | 580 | wchar_t ch; |
|
- | 581 | size_t src_off; |
|
- | 582 | size_t dest_off; |
|
- | 583 | ||
- | 584 | /* There must be space for a null terminator in the buffer. */ |
|
- | 585 | ASSERT(size > 0); |
|
- | 586 | ||
- | 587 | src_off = 0; |
|
- | 588 | dest_off = 0; |
|
- | 589 | ||
- | 590 | while ((ch = str_decode(src, &src_off, n)) != 0) { |
|
- | 591 | if (chr_encode(ch, dest, &dest_off, size - 1) != EOK) |
|
- | 592 | break; |
|
- | 593 | } |
|
372 | 594 | ||
- | 595 | dest[dest_off] = '\0'; |
|
- | 596 | } |
|
373 | 597 | ||
374 | /** Copy NULL terminated string. |
598 | /** Copy NULL-terminated wide string to string |
375 | * |
599 | * |
376 | * Copy at most 'len' characters from string 'src' to 'dest'. |
600 | * Copy source wide string @a src to destination buffer @a dst. |
- | 601 | * No more than @a size bytes are written. NULL-terminator is always |
|
377 | * If 'src' is shorter than 'len', '\0' is inserted behind the |
602 | * written after the last succesfully copied character (i.e. if the |
- | 603 | * destination buffer is has at least 1 byte, it will be always |
|
378 | * last copied character. |
604 | * NULL-terminated). |
379 | * |
605 | * |
380 | * @param src Source string. |
606 | * @param src Source wide string. |
381 | * @param dest Destination buffer. |
607 | * @param dst Destination buffer. |
382 | * @param len Size of destination buffer. |
608 | * @param count Size of the destination buffer. |
383 | * |
609 | * |
384 | */ |
610 | */ |
385 | void strncpy(char *dest, const char *src, size_t len) |
611 | void wstr_nstr(char *dst, const wchar_t *src, size_t size) |
386 | { |
612 | { |
- | 613 | /* No space for the NULL-terminator in the buffer */ |
|
387 | unsigned int i; |
614 | if (size == 0) |
- | 615 | return; |
|
388 | 616 | ||
- | 617 | wchar_t ch; |
|
389 | for (i = 0; i < len; i++) { |
618 | count_t src_idx = 0; |
390 | if (!(dest[i] = src[i])) |
619 | size_t dst_off = 0; |
- | 620 | ||
- | 621 | while ((ch = src[src_idx++]) != 0) { |
|
- | 622 | if (chr_encode(ch, dst, &dst_off, size) != EOK) |
|
391 | return; |
623 | break; |
392 | } |
624 | } |
393 | 625 | ||
- | 626 | if (dst_off >= size) |
|
394 | dest[i - 1] = '\0'; |
627 | dst[size - 1] = 0; |
- | 628 | else |
|
- | 629 | dst[dst_off] = 0; |
|
395 | } |
630 | } |
396 | 631 | ||
397 | /** Find first occurence of character in string. |
632 | /** Find first occurence of character in string. |
398 | * |
633 | * |
399 | * @param s String to search. |
634 | * @param str String to search. |
400 | * @param i Character to look for. |
635 | * @param ch Character to look for. |
- | 636 | * |
|
- | 637 | * @return Pointer to character in @a str or NULL if not found. |
|
401 | * |
638 | * |
402 | * @return Pointer to character in @a s or NULL if not found. |
- | |
403 | */ |
639 | */ |
404 | extern char *strchr(const char *s, int i) |
640 | const char *str_chr(const char *str, wchar_t ch) |
405 | { |
641 | { |
- | 642 | wchar_t acc; |
|
406 | while (*s != '\0') { |
643 | size_t off = 0; |
- | 644 | ||
- | 645 | while ((acc = str_decode(str, &off, STR_NO_LIMIT)) != 0) { |
|
407 | if (*s == i) |
646 | if (acc == ch) |
408 | return (char *) s; |
647 | return (str + off); |
409 | ++s; |
- | |
410 | } |
648 | } |
411 | 649 | ||
412 | return NULL; |
650 | return NULL; |
413 | } |
651 | } |
414 | 652 | ||
- | 653 | /** Insert a wide character into a wide string. |
|
- | 654 | * |
|
- | 655 | * Insert a wide character into a wide string at position |
|
- | 656 | * @a pos. The characters after the position are shifted. |
|
- | 657 | * |
|
- | 658 | * @param str String to insert to. |
|
- | 659 | * @param ch Character to insert to. |
|
- | 660 | * @param pos Character index where to insert. |
|
- | 661 | @ @param max_pos Characters in the buffer. |
|
- | 662 | * |
|
- | 663 | * @return True if the insertion was sucessful, false if the position |
|
- | 664 | * is out of bounds. |
|
- | 665 | * |
|
- | 666 | */ |
|
- | 667 | bool wstr_linsert(wchar_t *str, wchar_t ch, count_t pos, count_t max_pos) |
|
- | 668 | { |
|
- | 669 | count_t len = wstr_length(str); |
|
- | 670 | ||
- | 671 | if ((pos > len) || (pos + 1 > max_pos)) |
|
- | 672 | return false; |
|
- | 673 | ||
- | 674 | count_t i; |
|
- | 675 | for (i = len; i + 1 > pos; i--) |
|
- | 676 | str[i + 1] = str[i]; |
|
- | 677 | ||
- | 678 | str[pos] = ch; |
|
- | 679 | ||
- | 680 | return true; |
|
- | 681 | } |
|
- | 682 | ||
- | 683 | /** Remove a wide character from a wide string. |
|
- | 684 | * |
|
- | 685 | * Remove a wide character from a wide string at position |
|
- | 686 | * @a pos. The characters after the position are shifted. |
|
- | 687 | * |
|
- | 688 | * @param str String to remove from. |
|
- | 689 | * @param pos Character index to remove. |
|
- | 690 | * |
|
- | 691 | * @return True if the removal was sucessful, false if the position |
|
- | 692 | * is out of bounds. |
|
- | 693 | * |
|
- | 694 | */ |
|
- | 695 | bool wstr_remove(wchar_t *str, count_t pos) |
|
- | 696 | { |
|
- | 697 | count_t len = wstr_length(str); |
|
- | 698 | ||
- | 699 | if (pos >= len) |
|
- | 700 | return false; |
|
- | 701 | ||
- | 702 | count_t i; |
|
- | 703 | for (i = pos + 1; i <= len; i++) |
|
- | 704 | str[i - 1] = str[i]; |
|
- | 705 | ||
- | 706 | return true; |
|
- | 707 | } |
|
- | 708 | ||
415 | /** @} |
709 | /** @} |
416 | */ |
710 | */ |