Rev 4014 | Rev 4179 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 4014 | Rev 4175 | ||
---|---|---|---|
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 | /** Decode a single UTF-8 character from a NULL-terminated string. |
|
- | 46 | * |
|
- | 47 | * Decode a single UTF-8 character from a plain char NULL-terminated |
|
- | 48 | * string. Decoding starts at @index and this index is incremented |
|
- | 49 | * if the current UTF-8 string is encoded in more than a single byte. |
|
- | 50 | * |
|
- | 51 | * @param str Plain character NULL-terminated string. |
|
- | 52 | * @param index Index (counted in plain characters) where to start |
|
- | 53 | * the decoding. |
|
- | 54 | * |
|
- | 55 | * @return Decoded character in UTF-32 or '?' if the encoding is wrong. |
|
- | 56 | * |
|
- | 57 | */ |
|
- | 58 | wchar_t utf8_decode(const char *str, index_t *index) |
|
- | 59 | { |
|
- | 60 | uint8_t c1; /* First plain character from str */ |
|
- | 61 | uint8_t c2; /* Second plain character from str */ |
|
- | 62 | uint8_t c3; /* Third plain character from str */ |
|
- | 63 | uint8_t c4; /* Fourth plain character from str */ |
|
- | 64 | ||
- | 65 | c1 = (uint8_t) str[*index]; |
|
- | 66 | ||
- | 67 | if ((c1 & 0x80) == 0) { |
|
- | 68 | /* Plain ASCII (code points 0 .. 127) */ |
|
- | 69 | return (wchar_t) c1; |
|
- | 70 | } else if ((c1 & 0xe0) == 0xc0) { |
|
- | 71 | /* Code points 128 .. 2047 */ |
|
- | 72 | c2 = (uint8_t) str[*index + 1]; |
|
- | 73 | if ((c2 & 0xc0) == 0x80) { |
|
- | 74 | (*index)++; |
|
- | 75 | return ((wchar_t) ((c1 & 0x1f) << 6) | (c2 & 0x3f)); |
|
- | 76 | } else |
|
- | 77 | return ((wchar_t) '?'); |
|
- | 78 | } else if ((c1 & 0xf0) == 0xe0) { |
|
- | 79 | /* Code points 2048 .. 65535 */ |
|
- | 80 | c2 = (uint8_t) str[*index + 1]; |
|
- | 81 | if ((c2 & 0xc0) == 0x80) { |
|
- | 82 | (*index)++; |
|
- | 83 | c3 = (uint8_t) str[*index + 1]; |
|
- | 84 | if ((c3 & 0xc0) == 0x80) { |
|
- | 85 | (*index)++; |
|
- | 86 | return ((wchar_t) ((c1 & 0x0f) << 12) | ((c2 & 0x3f) << 6) | (c3 & 0x3f)); |
|
- | 87 | } else |
|
- | 88 | return ((wchar_t) '?'); |
|
- | 89 | } else |
|
- | 90 | return ((wchar_t) '?'); |
|
- | 91 | } else if ((c1 & 0xf8) == 0xf0) { |
|
- | 92 | /* Code points 65536 .. 1114111 */ |
|
- | 93 | c2 = (uint8_t) str[*index + 1]; |
|
- | 94 | if ((c2 & 0xc0) == 0x80) { |
|
- | 95 | (*index)++; |
|
- | 96 | c3 = (uint8_t) str[*index + 1]; |
|
- | 97 | if ((c3 & 0xc0) == 0x80) { |
|
- | 98 | (*index)++; |
|
- | 99 | c4 = (uint8_t) str[*index + 1]; |
|
- | 100 | if ((c4 & 0xc0) == 0x80) { |
|
- | 101 | (*index)++; |
|
- | 102 | return ((wchar_t) ((c1 & 0x07) << 18) | ((c2 & 0x3f) << 12) | ((c3 & 0x3f) << 6) | (c4 & 0x3f)); |
|
- | 103 | } else |
|
- | 104 | return ((wchar_t) '?'); |
|
- | 105 | } else |
|
- | 106 | return ((wchar_t) '?'); |
|
- | 107 | } else |
|
- | 108 | return ((wchar_t) '?'); |
|
- | 109 | } |
|
- | 110 | ||
- | 111 | return ((wchar_t) '?'); |
|
- | 112 | } |
|
- | 113 | ||
45 | /** Return number of characters in a string. |
114 | /** Return number of characters in a string. |
46 | * |
115 | * |
47 | * @param str NULL terminated string. |
116 | * @param str NULL terminated string. |
48 | * |
117 | * |
49 | * @return Number of characters in str. |
118 | * @return Number of characters in str. |
50 | * |
119 | * |
51 | */ |
120 | */ |
52 | size_t strlen(const char *str) |
121 | size_t strlen(const char *str) |
53 | { |
122 | { |
54 | int i; |
123 | int i; |
55 | 124 | ||
56 | for (i = 0; str[i]; i++); |
125 | for (i = 0; str[i]; i++); |
57 | 126 | ||
58 | return i; |
127 | return i; |
59 | } |
128 | } |
60 | 129 | ||
61 | /** Compare two NULL terminated strings |
130 | /** Compare two NULL terminated strings |
62 | * |
131 | * |
63 | * Do a char-by-char comparison of two NULL terminated strings. |
132 | * Do a char-by-char comparison of two NULL terminated strings. |
64 | * The strings are considered equal iff they consist of the same |
133 | * The strings are considered equal iff they consist of the same |
65 | * characters on the minimum of their lengths. |
134 | * characters on the minimum of their lengths. |
66 | * |
135 | * |
67 | * @param src First string to compare. |
136 | * @param src First string to compare. |
68 | * @param dst Second string to compare. |
137 | * @param dst Second string to compare. |
69 | * |
138 | * |
70 | * @return 0 if the strings are equal, -1 if first is smaller, 1 if second smaller. |
139 | * @return 0 if the strings are equal, -1 if first is smaller, 1 if second smaller. |
71 | * |
140 | * |
72 | */ |
141 | */ |
73 | int strcmp(const char *src, const char *dst) |
142 | int strcmp(const char *src, const char *dst) |
74 | { |
143 | { |
75 | for (; *src && *dst; src++, dst++) { |
144 | for (; *src && *dst; src++, dst++) { |
76 | if (*src < *dst) |
145 | if (*src < *dst) |
77 | return -1; |
146 | return -1; |
78 | if (*src > *dst) |
147 | if (*src > *dst) |
79 | return 1; |
148 | return 1; |
80 | } |
149 | } |
81 | if (*src == *dst) |
150 | if (*src == *dst) |
82 | return 0; |
151 | return 0; |
83 | 152 | ||
84 | if (!*src) |
153 | if (!*src) |
85 | return -1; |
154 | return -1; |
86 | 155 | ||
87 | return 1; |
156 | return 1; |
88 | } |
157 | } |
89 | 158 | ||
90 | 159 | ||
91 | /** Compare two NULL terminated strings |
160 | /** Compare two NULL terminated strings |
92 | * |
161 | * |
93 | * Do a char-by-char comparison of two NULL terminated strings. |
162 | * Do a char-by-char comparison of two NULL terminated strings. |
94 | * The strings are considered equal iff they consist of the same |
163 | * The strings are considered equal iff they consist of the same |
95 | * characters on the minimum of their lengths and specified maximal |
164 | * characters on the minimum of their lengths and specified maximal |
96 | * length. |
165 | * length. |
97 | * |
166 | * |
98 | * @param src First string to compare. |
167 | * @param src First string to compare. |
99 | * @param dst Second string to compare. |
168 | * @param dst Second string to compare. |
100 | * @param len Maximal length for comparison. |
169 | * @param len Maximal length for comparison. |
101 | * |
170 | * |
102 | * @return 0 if the strings are equal, -1 if first is smaller, 1 if second smaller. |
171 | * @return 0 if the strings are equal, -1 if first is smaller, 1 if second smaller. |
103 | * |
172 | * |
104 | */ |
173 | */ |
105 | int strncmp(const char *src, const char *dst, size_t len) |
174 | int strncmp(const char *src, const char *dst, size_t len) |
106 | { |
175 | { |
107 | unsigned int i; |
176 | unsigned int i; |
108 | 177 | ||
109 | for (i = 0; (*src) && (*dst) && (i < len); src++, dst++, i++) { |
178 | for (i = 0; (*src) && (*dst) && (i < len); src++, dst++, i++) { |
110 | if (*src < *dst) |
179 | if (*src < *dst) |
111 | return -1; |
180 | return -1; |
112 | 181 | ||
113 | if (*src > *dst) |
182 | if (*src > *dst) |
114 | return 1; |
183 | return 1; |
115 | } |
184 | } |
116 | 185 | ||
117 | if (i == len || *src == *dst) |
186 | if (i == len || *src == *dst) |
118 | return 0; |
187 | return 0; |
119 | 188 | ||
120 | if (!*src) |
189 | if (!*src) |
121 | return -1; |
190 | return -1; |
122 | 191 | ||
123 | return 1; |
192 | return 1; |
124 | } |
193 | } |
125 | 194 | ||
126 | 195 | ||
127 | 196 | ||
128 | /** Copy NULL terminated string. |
197 | /** Copy NULL terminated string. |
129 | * |
198 | * |
130 | * Copy at most 'len' characters from string 'src' to 'dest'. |
199 | * Copy at most 'len' characters from string 'src' to 'dest'. |
131 | * If 'src' is shorter than 'len', '\0' is inserted behind the |
200 | * If 'src' is shorter than 'len', '\0' is inserted behind the |
132 | * last copied character. |
201 | * last copied character. |
133 | * |
202 | * |
134 | * @param src Source string. |
203 | * @param src Source string. |
135 | * @param dest Destination buffer. |
204 | * @param dest Destination buffer. |
136 | * @param len Size of destination buffer. |
205 | * @param len Size of destination buffer. |
137 | * |
206 | * |
138 | */ |
207 | */ |
139 | void strncpy(char *dest, const char *src, size_t len) |
208 | void strncpy(char *dest, const char *src, size_t len) |
140 | { |
209 | { |
141 | unsigned int i; |
210 | unsigned int i; |
142 | 211 | ||
143 | for (i = 0; i < len; i++) { |
212 | for (i = 0; i < len; i++) { |
144 | if (!(dest[i] = src[i])) |
213 | if (!(dest[i] = src[i])) |
145 | return; |
214 | return; |
146 | } |
215 | } |
147 | 216 | ||
148 | dest[i - 1] = '\0'; |
217 | dest[i - 1] = '\0'; |
149 | } |
218 | } |
150 | 219 | ||
151 | /** Find first occurence of character in string. |
220 | /** Find first occurence of character in string. |
152 | * |
221 | * |
153 | * @param s String to search. |
222 | * @param s String to search. |
154 | * @param i Character to look for. |
223 | * @param i Character to look for. |
155 | * |
224 | * |
156 | * @return Pointer to character in @a s or NULL if not found. |
225 | * @return Pointer to character in @a s or NULL if not found. |
157 | */ |
226 | */ |
158 | extern char *strchr(const char *s, int i) |
227 | extern char *strchr(const char *s, int i) |
159 | { |
228 | { |
160 | while (*s != '\0') { |
229 | while (*s != '\0') { |
161 | if (*s == i) |
230 | if (*s == i) |
162 | return (char *) s; |
231 | return (char *) s; |
163 | ++s; |
232 | ++s; |
164 | } |
233 | } |
165 | 234 | ||
166 | return NULL; |
235 | return NULL; |
167 | } |
236 | } |
168 | 237 | ||
169 | /** @} |
238 | /** @} |
170 | */ |
239 | */ |
171 | 240 |