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