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