Rev 4199 | Rev 4205 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 4199 | Rev 4200 | ||
---|---|---|---|
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 Miscellaneous functions. |
35 | * @brief Miscellaneous functions. |
36 | */ |
36 | */ |
37 | 37 | ||
38 | #include <string.h> |
38 | #include <string.h> |
39 | #include <print.h> |
39 | #include <print.h> |
40 | #include <cpu.h> |
40 | #include <cpu.h> |
41 | #include <arch/asm.h> |
41 | #include <arch/asm.h> |
42 | #include <arch.h> |
42 | #include <arch.h> |
43 | #include <console/kconsole.h> |
43 | #include <console/kconsole.h> |
44 | 44 | ||
45 | char invalch = '?'; |
45 | char invalch = '?'; |
46 | 46 | ||
47 | /** Byte mask consisting of lowest @n bits (out of eight). */ |
47 | /** Byte mask consisting of lowest @n bits (out of eight). */ |
48 | #define LO_MASK_8(n) ((uint8_t)((1 << (n)) - 1)) |
48 | #define LO_MASK_8(n) ((uint8_t)((1 << (n)) - 1)) |
49 | 49 | ||
50 | /** Byte mask consisting of lowest @n bits (out of 32). */ |
50 | /** Byte mask consisting of lowest @n bits (out of 32). */ |
51 | #define LO_MASK_32(n) ((uint32_t)((1 << (n)) - 1)) |
51 | #define LO_MASK_32(n) ((uint32_t)((1 << (n)) - 1)) |
52 | 52 | ||
53 | /** Byte mask consisting of highest @n bits (out of eight). */ |
53 | /** Byte mask consisting of highest @n bits (out of eight). */ |
54 | #define HI_MASK_8(n) (~LO_MASK_8(8 - (n))) |
54 | #define HI_MASK_8(n) (~LO_MASK_8(8 - (n))) |
55 | 55 | ||
56 | /** Number of data bits in a UTF-8 continuation byte. */ |
56 | /** Number of data bits in a UTF-8 continuation byte. */ |
57 | #define CONT_BITS 6 |
57 | #define CONT_BITS 6 |
58 | 58 | ||
59 | /** Decode a single UTF-8 character from a NULL-terminated string. |
59 | /** Decode a single character from a substring. |
60 | * |
60 | * |
61 | * Decode a single UTF-8 character from a plain char NULL-terminated |
61 | * Decode a single character from a substring of size @a sz. Decoding starts |
62 | * string. Decoding starts at @index and this index is moved to the |
62 | * at @a offset and this offset is moved to the beginning of the next |
63 | * beginning of the next character. In case of decoding error, |
63 | * character. In case of decoding error, offset generally advances at least |
64 | * index advances. However, index is never moved beyond (str+limit). |
64 | * by one. However, offset is never moved beyond (str + sz). |
65 | * |
65 | * |
66 | * @param str Plain character NULL-terminated string. |
66 | * @param str String (not necessarily NULL-terminated). |
67 | * @param index Index (counted in plain characters) where to start |
67 | * @param index Index (counted in plain characters) where to start |
68 | * the decoding. |
68 | * the decoding. |
69 | * @param limit Maximal allowed value of index. |
69 | * @param limit Size of the substring. |
70 | * |
70 | * |
71 | * @return Decoded character in UTF-32 or '?' if the encoding is wrong. |
71 | * @return Value of decoded character or '?' on decoding error. |
72 | * |
72 | * |
73 | */ |
73 | */ |
74 | wchar_t utf8_decode(const char *str, index_t *index, index_t limit) |
74 | wchar_t chr_decode(const char *str, size_t *offset, size_t sz) |
75 | { |
75 | { |
76 | uint8_t b0, b; /* Bytes read from str. */ |
76 | uint8_t b0, b; /* Bytes read from str. */ |
77 | wchar_t ch; |
77 | wchar_t ch; |
78 | 78 | ||
79 | int b0_bits; /* Data bits in first byte. */ |
79 | int b0_bits; /* Data bits in first byte. */ |
80 | int cbytes; /* Number of continuation bytes. */ |
80 | int cbytes; /* Number of continuation bytes. */ |
81 | 81 | ||
82 | if (*index + 1 > limit) |
82 | if (*offset + 1 > sz) |
83 | return invalch; |
83 | return invalch; |
84 | 84 | ||
85 | b0 = (uint8_t) str[(*index)++]; |
85 | b0 = (uint8_t) str[(*offset)++]; |
86 | 86 | ||
87 | /* Determine code length. */ |
87 | /* Determine code length. */ |
88 | 88 | ||
89 | if ((b0 & 0x80) == 0) { |
89 | if ((b0 & 0x80) == 0) { |
90 | /* 0xxxxxxx (Plain ASCII) */ |
90 | /* 0xxxxxxx (Plain ASCII) */ |
91 | b0_bits = 7; |
91 | b0_bits = 7; |
92 | cbytes = 0; |
92 | cbytes = 0; |
93 | } else if ((b0 & 0xe0) == 0xc0) { |
93 | } else if ((b0 & 0xe0) == 0xc0) { |
94 | /* 110xxxxx 10xxxxxx */ |
94 | /* 110xxxxx 10xxxxxx */ |
95 | b0_bits = 5; |
95 | b0_bits = 5; |
96 | cbytes = 1; |
96 | cbytes = 1; |
97 | } else if ((b0 & 0xf0) == 0xe0) { |
97 | } else if ((b0 & 0xf0) == 0xe0) { |
98 | /* 1110xxxx 10xxxxxx 10xxxxxx */ |
98 | /* 1110xxxx 10xxxxxx 10xxxxxx */ |
99 | b0_bits = 4; |
99 | b0_bits = 4; |
100 | cbytes = 2; |
100 | cbytes = 2; |
101 | } else if ((b0 & 0xf8) == 0xf0) { |
101 | } else if ((b0 & 0xf8) == 0xf0) { |
102 | /* 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */ |
102 | /* 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */ |
103 | b0_bits = 3; |
103 | b0_bits = 3; |
104 | cbytes = 3; |
104 | cbytes = 3; |
105 | } else { |
105 | } else { |
106 | /* 10xxxxxx -- unexpected continuation byte. */ |
106 | /* 10xxxxxx -- unexpected continuation byte. */ |
107 | return invalch; |
107 | return invalch; |
108 | } |
108 | } |
109 | 109 | ||
110 | if (*index + cbytes > limit) { |
110 | if (*offset + cbytes > sz) { |
111 | return invalch; |
111 | return invalch; |
112 | } |
112 | } |
113 | 113 | ||
114 | ch = b0 & LO_MASK_8(b0_bits); |
114 | ch = b0 & LO_MASK_8(b0_bits); |
115 | 115 | ||
116 | /* Decode continuation bytes. */ |
116 | /* Decode continuation bytes. */ |
117 | while (cbytes > 0) { |
117 | while (cbytes > 0) { |
118 | b = (uint8_t) str[(*index)++]; |
118 | b = (uint8_t) str[(*offset)++]; |
119 | 119 | ||
120 | /* Must be 10xxxxxx. */ |
120 | /* Must be 10xxxxxx. */ |
121 | if ((b & 0xc0) != 0x80) { |
121 | if ((b & 0xc0) != 0x80) { |
122 | return invalch; |
122 | return invalch; |
123 | } |
123 | } |
124 | 124 | ||
125 | /* Shift data bits to ch. */ |
125 | /* Shift data bits to ch. */ |
126 | ch = (ch << CONT_BITS) | (wchar_t) (b & LO_MASK_8(CONT_BITS)); |
126 | ch = (ch << CONT_BITS) | (wchar_t) (b & LO_MASK_8(CONT_BITS)); |
127 | --cbytes; |
127 | --cbytes; |
128 | } |
128 | } |
129 | 129 | ||
130 | return ch; |
130 | return ch; |
131 | } |
131 | } |
132 | 132 | ||
133 | /** Encode a single UTF-32 character as UTF-8 |
- | |
134 | * |
- | |
135 | * Encode a single UTF-32 character as UTF-8 and store it into |
133 | /** Encode a single character to string representation. |
136 | * the given buffer at @index. Encoding starts at @index and |
- | |
137 | * this index is moved at the position where the next character |
- | |
138 | * can be written to. |
- | |
139 | * |
- | |
140 | * @param ch Input UTF-32 character. |
- | |
141 | * @param str Output buffer. |
- | |
142 | * @param index Index (counted in plain characters) where to start |
- | |
143 | * the encoding |
- | |
144 | * @param limit Maximal allowed value of index. |
- | |
145 | * |
- | |
146 | * @return True if the character was encoded or false if there is not |
- | |
147 | * enought space in the output buffer or the character is invalid |
- | |
148 | * Unicode code point. |
- | |
149 | * |
134 | * |
- | 135 | * 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 |
|
- | 137 | * is moved to the position where the next character can be written to. |
|
- | 138 | * |
|
- | 139 | * @param ch Input character. |
|
- | 140 | * @param str Output buffer. |
|
- | 141 | * @param offset Offset (in bytes) where to start writing. |
|
- | 142 | * @param sz Size of the output buffer. |
|
- | 143 | * |
|
- | 144 | * @return True if the character was encoded successfully or false if there |
|
- | 145 | * was not enough space in the output buffer or the character code |
|
- | 146 | * was invalid. |
|
150 | */ |
147 | */ |
151 | bool utf8_encode(const wchar_t ch, char *str, index_t *index, index_t limit) |
148 | bool chr_encode(const wchar_t ch, char *str, size_t *offset, size_t sz) |
152 | { |
149 | { |
153 | uint32_t cc; /* Unsigned version of ch. */ |
150 | uint32_t cc; /* Unsigned version of ch. */ |
154 | 151 | ||
155 | int cbytes; /* Number of continuation bytes. */ |
152 | int cbytes; /* Number of continuation bytes. */ |
156 | int b0_bits; /* Number of data bits in first byte. */ |
153 | int b0_bits; /* Number of data bits in first byte. */ |
157 | int i; |
154 | int i; |
158 | 155 | ||
159 | if (*index >= limit) |
156 | if (*offset >= sz) |
160 | return false; |
157 | return false; |
161 | 158 | ||
162 | if (ch < 0) |
159 | if (ch < 0) |
163 | return false; |
160 | return false; |
164 | 161 | ||
165 | /* Bit operations should only be done on unsigned numbers. */ |
162 | /* Bit operations should only be done on unsigned numbers. */ |
166 | cc = (uint32_t) ch; |
163 | cc = (uint32_t) ch; |
167 | 164 | ||
168 | /* Determine how many continuation bytes are needed. */ |
165 | /* Determine how many continuation bytes are needed. */ |
169 | if ((cc & ~LO_MASK_32(7)) == 0) { |
166 | if ((cc & ~LO_MASK_32(7)) == 0) { |
170 | b0_bits = 7; |
167 | b0_bits = 7; |
171 | cbytes = 0; |
168 | cbytes = 0; |
172 | } else if ((cc & ~LO_MASK_32(11)) == 0) { |
169 | } else if ((cc & ~LO_MASK_32(11)) == 0) { |
173 | b0_bits = 5; |
170 | b0_bits = 5; |
174 | cbytes = 1; |
171 | cbytes = 1; |
175 | } else if ((cc & ~LO_MASK_32(16)) == 0) { |
172 | } else if ((cc & ~LO_MASK_32(16)) == 0) { |
176 | b0_bits = 4; |
173 | b0_bits = 4; |
177 | cbytes = 2; |
174 | cbytes = 2; |
178 | } else if ((cc & ~LO_MASK_32(21)) == 0) { |
175 | } else if ((cc & ~LO_MASK_32(21)) == 0) { |
179 | b0_bits = 3; |
176 | b0_bits = 3; |
180 | cbytes = 3; |
177 | cbytes = 3; |
181 | } else { |
178 | } else { |
182 | /* Codes longer than 21 bits are not supported. */ |
179 | /* Codes longer than 21 bits are not supported. */ |
183 | return false; |
180 | return false; |
184 | } |
181 | } |
185 | 182 | ||
186 | /* Check for available space in buffer. */ |
183 | /* Check for available space in buffer. */ |
187 | if (*index + cbytes >= limit) |
184 | if (*offset + cbytes >= sz) |
188 | return false; |
185 | return false; |
189 | 186 | ||
190 | /* Encode continuation bytes. */ |
187 | /* Encode continuation bytes. */ |
191 | for (i = cbytes; i > 0; --i) { |
188 | for (i = cbytes; i > 0; --i) { |
192 | str[*index + i] = 0x80 | (cc & LO_MASK_32(CONT_BITS)); |
189 | str[*offset + i] = 0x80 | (cc & LO_MASK_32(CONT_BITS)); |
193 | cc = cc >> CONT_BITS; |
190 | cc = cc >> CONT_BITS; |
194 | } |
191 | } |
195 | 192 | ||
196 | /* Encode first byte. */ |
193 | /* Encode first byte. */ |
197 | str[*index] = (cc & LO_MASK_32(b0_bits)) | HI_MASK_8(8 - b0_bits - 1); |
194 | str[*offset] = (cc & LO_MASK_32(b0_bits)) | HI_MASK_8(8 - b0_bits - 1); |
198 | 195 | ||
199 | /* Advance index. */ |
196 | /* Advance offset. */ |
200 | *index += (1 + cbytes); |
197 | *offset += (1 + cbytes); |
201 | 198 | ||
202 | return true; |
199 | return true; |
203 | } |
200 | } |
204 | 201 | ||
205 | /** Get bytes used by UTF-8 characters. |
202 | /** Get bytes used by UTF-8 characters. |
206 | * |
203 | * |
207 | * Get the number of bytes (count of plain characters) which |
204 | * Get the number of bytes (count of plain characters) which |
208 | * are used by a given count of UTF-8 characters in a string. |
205 | * are used by a given count of UTF-8 characters in a string. |
209 | * As UTF-8 encoding is multibyte, there is no constant |
206 | * As UTF-8 encoding is multibyte, there is no constant |
210 | * correspondence between number of characters and used bytes. |
207 | * correspondence between number of characters and used bytes. |
211 | * |
208 | * |
212 | * @param str UTF-8 string to consider. |
209 | * @param str UTF-8 string to consider. |
213 | * @param count Number of UTF-8 characters to count. |
210 | * @param count Number of UTF-8 characters to count. |
214 | * |
211 | * |
215 | * @return Number of bytes used by the characters. |
212 | * @return Number of bytes used by the characters. |
216 | * |
213 | * |
217 | */ |
214 | */ |
218 | size_t utf8_count_bytes(const char *str, count_t count) |
215 | size_t utf8_count_bytes(const char *str, count_t count) |
219 | { |
216 | { |
220 | size_t size = 0; |
217 | size_t size = 0; |
221 | index_t index = 0; |
218 | index_t index = 0; |
222 | index_t iprev; |
219 | index_t iprev; |
223 | wchar_t ch; |
220 | wchar_t ch; |
224 | 221 | ||
225 | while (true) { |
222 | while (true) { |
226 | iprev = index; |
223 | iprev = index; |
227 | if (size >= count) |
224 | if (size >= count) |
228 | break; |
225 | break; |
229 | ch = utf8_decode(str, &index, UTF8_NO_LIMIT); |
226 | ch = chr_decode(str, &index, UTF8_NO_LIMIT); |
230 | if (ch == '\0') break; |
227 | if (ch == '\0') break; |
231 | 228 | ||
232 | size++; |
229 | size++; |
233 | } |
230 | } |
234 | 231 | ||
235 | return iprev; |
232 | return iprev; |
236 | } |
233 | } |
237 | 234 | ||
238 | /** Check whether character is plain ASCII. |
235 | /** Check whether character is plain ASCII. |
239 | * |
236 | * |
240 | * @return True if character is plain ASCII. |
237 | * @return True if character is plain ASCII. |
241 | * |
238 | * |
242 | */ |
239 | */ |
243 | bool ascii_check(const wchar_t ch) |
240 | bool ascii_check(const wchar_t ch) |
244 | { |
241 | { |
245 | if ((ch >= 0) && (ch <= 127)) |
242 | if ((ch >= 0) && (ch <= 127)) |
246 | return true; |
243 | return true; |
247 | 244 | ||
248 | return false; |
245 | return false; |
249 | } |
246 | } |
250 | 247 | ||
251 | /** Check whether character is Unicode. |
248 | /** Check whether character is Unicode. |
252 | * |
249 | * |
253 | * @return True if character is valid Unicode code point. |
250 | * @return True if character is valid Unicode code point. |
254 | * |
251 | * |
255 | */ |
252 | */ |
256 | bool unicode_check(const wchar_t ch) |
253 | bool unicode_check(const wchar_t ch) |
257 | { |
254 | { |
258 | if ((ch >= 0) && (ch <= 1114111)) |
255 | if ((ch >= 0) && (ch <= 1114111)) |
259 | return true; |
256 | return true; |
260 | 257 | ||
261 | return false; |
258 | return false; |
262 | } |
259 | } |
263 | 260 | ||
264 | /** Return number of plain characters in a string. |
261 | /** Return number of plain characters in a string. |
265 | * |
262 | * |
266 | * @param str NULL-terminated string. |
263 | * @param str NULL-terminated string. |
267 | * |
264 | * |
268 | * @return Number of characters in str. |
265 | * @return Number of characters in str. |
269 | * |
266 | * |
270 | */ |
267 | */ |
271 | size_t strlen(const char *str) |
268 | size_t strlen(const char *str) |
272 | { |
269 | { |
273 | size_t size; |
270 | size_t size; |
274 | for (size = 0; str[size]; size++); |
271 | for (size = 0; str[size]; size++); |
275 | 272 | ||
276 | return size; |
273 | return size; |
277 | } |
274 | } |
278 | 275 | ||
279 | /** Return number of UTF-8 characters in a string. |
276 | /** Return number of UTF-8 characters in a string. |
280 | * |
277 | * |
281 | * @param str NULL-terminated UTF-8 string. |
278 | * @param str NULL-terminated UTF-8 string. |
282 | * |
279 | * |
283 | * @return Number of UTF-8 characters in str. |
280 | * @return Number of UTF-8 characters in str. |
284 | * |
281 | * |
285 | */ |
282 | */ |
286 | size_t strlen_utf8(const char *str) |
283 | size_t strlen_utf8(const char *str) |
287 | { |
284 | { |
288 | size_t size = 0; |
285 | size_t size = 0; |
289 | index_t index = 0; |
286 | index_t index = 0; |
290 | 287 | ||
291 | while (utf8_decode(str, &index, UTF8_NO_LIMIT) != 0) { |
288 | while (chr_decode(str, &index, UTF8_NO_LIMIT) != 0) { |
292 | size++; |
289 | size++; |
293 | } |
290 | } |
294 | 291 | ||
295 | return size; |
292 | return size; |
296 | } |
293 | } |
297 | 294 | ||
298 | /** Return number of UTF-32 characters in a string. |
295 | /** Return number of UTF-32 characters in a string. |
299 | * |
296 | * |
300 | * @param str NULL-terminated UTF-32 string. |
297 | * @param str NULL-terminated UTF-32 string. |
301 | * |
298 | * |
302 | * @return Number of UTF-32 characters in str. |
299 | * @return Number of UTF-32 characters in str. |
303 | * |
300 | * |
304 | */ |
301 | */ |
305 | size_t strlen_utf32(const wchar_t *str) |
302 | size_t strlen_utf32(const wchar_t *str) |
306 | { |
303 | { |
307 | size_t size; |
304 | size_t size; |
308 | for (size = 0; str[size]; size++); |
305 | for (size = 0; str[size]; size++); |
309 | 306 | ||
310 | return size; |
307 | return size; |
311 | } |
308 | } |
312 | 309 | ||
313 | /** Compare two NULL terminated strings |
310 | /** Compare two NULL terminated strings |
314 | * |
311 | * |
315 | * Do a char-by-char comparison of two NULL terminated strings. |
312 | * Do a char-by-char comparison of two NULL terminated strings. |
316 | * The strings are considered equal iff they consist of the same |
313 | * The strings are considered equal iff they consist of the same |
317 | * characters on the minimum of their lengths. |
314 | * characters on the minimum of their lengths. |
318 | * |
315 | * |
319 | * @param src First string to compare. |
316 | * @param src First string to compare. |
320 | * @param dst Second string to compare. |
317 | * @param dst Second string to compare. |
321 | * |
318 | * |
322 | * @return 0 if the strings are equal, -1 if first is smaller, 1 if second smaller. |
319 | * @return 0 if the strings are equal, -1 if first is smaller, 1 if second smaller. |
323 | * |
320 | * |
324 | */ |
321 | */ |
325 | int strcmp(const char *src, const char *dst) |
322 | int strcmp(const char *src, const char *dst) |
326 | { |
323 | { |
327 | for (; *src && *dst; src++, dst++) { |
324 | for (; *src && *dst; src++, dst++) { |
328 | if (*src < *dst) |
325 | if (*src < *dst) |
329 | return -1; |
326 | return -1; |
330 | if (*src > *dst) |
327 | if (*src > *dst) |
331 | return 1; |
328 | return 1; |
332 | } |
329 | } |
333 | if (*src == *dst) |
330 | if (*src == *dst) |
334 | return 0; |
331 | return 0; |
335 | 332 | ||
336 | if (!*src) |
333 | if (!*src) |
337 | return -1; |
334 | return -1; |
338 | 335 | ||
339 | return 1; |
336 | return 1; |
340 | } |
337 | } |
341 | 338 | ||
342 | 339 | ||
343 | /** Compare two NULL terminated strings |
340 | /** Compare two NULL terminated strings |
344 | * |
341 | * |
345 | * Do a char-by-char comparison of two NULL terminated strings. |
342 | * Do a char-by-char comparison of two NULL terminated strings. |
346 | * The strings are considered equal iff they consist of the same |
343 | * The strings are considered equal iff they consist of the same |
347 | * characters on the minimum of their lengths and specified maximal |
344 | * characters on the minimum of their lengths and specified maximal |
348 | * length. |
345 | * length. |
349 | * |
346 | * |
350 | * @param src First string to compare. |
347 | * @param src First string to compare. |
351 | * @param dst Second string to compare. |
348 | * @param dst Second string to compare. |
352 | * @param len Maximal length for comparison. |
349 | * @param len Maximal length for comparison. |
353 | * |
350 | * |
354 | * @return 0 if the strings are equal, -1 if first is smaller, 1 if second smaller. |
351 | * @return 0 if the strings are equal, -1 if first is smaller, 1 if second smaller. |
355 | * |
352 | * |
356 | */ |
353 | */ |
357 | int strncmp(const char *src, const char *dst, size_t len) |
354 | int strncmp(const char *src, const char *dst, size_t len) |
358 | { |
355 | { |
359 | unsigned int i; |
356 | unsigned int i; |
360 | 357 | ||
361 | for (i = 0; (*src) && (*dst) && (i < len); src++, dst++, i++) { |
358 | for (i = 0; (*src) && (*dst) && (i < len); src++, dst++, i++) { |
362 | if (*src < *dst) |
359 | if (*src < *dst) |
363 | return -1; |
360 | return -1; |
364 | 361 | ||
365 | if (*src > *dst) |
362 | if (*src > *dst) |
366 | return 1; |
363 | return 1; |
367 | } |
364 | } |
368 | 365 | ||
369 | if (i == len || *src == *dst) |
366 | if (i == len || *src == *dst) |
370 | return 0; |
367 | return 0; |
371 | 368 | ||
372 | if (!*src) |
369 | if (!*src) |
373 | return -1; |
370 | return -1; |
374 | 371 | ||
375 | return 1; |
372 | return 1; |
376 | } |
373 | } |
377 | 374 | ||
378 | 375 | ||
379 | 376 | ||
380 | /** Copy NULL terminated string. |
377 | /** Copy NULL terminated string. |
381 | * |
378 | * |
382 | * Copy at most 'len' characters from string 'src' to 'dest'. |
379 | * Copy at most 'len' characters from string 'src' to 'dest'. |
383 | * If 'src' is shorter than 'len', '\0' is inserted behind the |
380 | * If 'src' is shorter than 'len', '\0' is inserted behind the |
384 | * last copied character. |
381 | * last copied character. |
385 | * |
382 | * |
386 | * @param src Source string. |
383 | * @param src Source string. |
387 | * @param dest Destination buffer. |
384 | * @param dest Destination buffer. |
388 | * @param len Size of destination buffer. |
385 | * @param len Size of destination buffer. |
389 | * |
386 | * |
390 | */ |
387 | */ |
391 | void strncpy(char *dest, const char *src, size_t len) |
388 | void strncpy(char *dest, const char *src, size_t len) |
392 | { |
389 | { |
393 | unsigned int i; |
390 | unsigned int i; |
394 | 391 | ||
395 | for (i = 0; i < len; i++) { |
392 | for (i = 0; i < len; i++) { |
396 | if (!(dest[i] = src[i])) |
393 | if (!(dest[i] = src[i])) |
397 | return; |
394 | return; |
398 | } |
395 | } |
399 | 396 | ||
400 | dest[i - 1] = '\0'; |
397 | dest[i - 1] = '\0'; |
401 | } |
398 | } |
402 | 399 | ||
403 | /** Find first occurence of character in string. |
400 | /** Find first occurence of character in string. |
404 | * |
401 | * |
405 | * @param s String to search. |
402 | * @param s String to search. |
406 | * @param i Character to look for. |
403 | * @param i Character to look for. |
407 | * |
404 | * |
408 | * @return Pointer to character in @a s or NULL if not found. |
405 | * @return Pointer to character in @a s or NULL if not found. |
409 | */ |
406 | */ |
410 | extern char *strchr(const char *s, int i) |
407 | extern char *strchr(const char *s, int i) |
411 | { |
408 | { |
412 | while (*s != '\0') { |
409 | while (*s != '\0') { |
413 | if (*s == i) |
410 | if (*s == i) |
414 | return (char *) s; |
411 | return (char *) s; |
415 | ++s; |
412 | ++s; |
416 | } |
413 | } |
417 | 414 | ||
418 | return NULL; |
415 | return NULL; |
419 | } |
416 | } |
420 | 417 | ||
421 | /** @} |
418 | /** @} |
422 | */ |
419 | */ |
423 | 420 |