source: mainline/kernel/generic/src/lib/string.c@ d09f84e6

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since d09f84e6 was d09f84e6, checked in by Jiri Svoboda <jirik.svoboda@…>, 16 years ago

Return error code from chr_encode() rather than boolean.

  • Property mode set to 100644
File size: 9.6 KB
RevLine 
[16da5f8e]1/*
2 * Copyright (c) 2001-2004 Jakub Jermar
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
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
15 * derived from this software without specific prior written permission.
16 *
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
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
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
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
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
[2f57690]29/** @addtogroup generic
[16da5f8e]30 * @{
31 */
32
33/**
34 * @file
[2f57690]35 * @brief Miscellaneous functions.
[16da5f8e]36 */
37
38#include <string.h>
39#include <print.h>
40#include <cpu.h>
41#include <arch/asm.h>
42#include <arch.h>
[d09f84e6]43#include <errno.h>
[16da5f8e]44#include <console/kconsole.h>
45
[74c8da2c]46char invalch = '?';
47
[32704cb]48/** Byte mask consisting of lowest @n bits (out of eight). */
[0dd1d444]49#define LO_MASK_8(n) ((uint8_t)((1 << (n)) - 1))
50
[32704cb]51/** Byte mask consisting of lowest @n bits (out of 32). */
52#define LO_MASK_32(n) ((uint32_t)((1 << (n)) - 1))
53
54/** Byte mask consisting of highest @n bits (out of eight). */
55#define HI_MASK_8(n) (~LO_MASK_8(8 - (n)))
56
[0dd1d444]57/** Number of data bits in a UTF-8 continuation byte. */
58#define CONT_BITS 6
59
[e1813cf]60/** Decode a single character from a substring.
[21a639b7]61 *
[e1813cf]62 * Decode a single character from a substring of size @a sz. Decoding starts
63 * at @a offset and this offset is moved to the beginning of the next
64 * character. In case of decoding error, offset generally advances at least
65 * by one. However, offset is never moved beyond (str + sz).
[21a639b7]66 *
[e1813cf]67 * @param str String (not necessarily NULL-terminated).
[21a639b7]68 * @param index Index (counted in plain characters) where to start
69 * the decoding.
[e1813cf]70 * @param limit Size of the substring.
[21a639b7]71 *
[e1813cf]72 * @return Value of decoded character or '?' on decoding error.
[21a639b7]73 */
[e1813cf]74wchar_t chr_decode(const char *str, size_t *offset, size_t sz)
[21a639b7]75{
[0dd1d444]76 uint8_t b0, b; /* Bytes read from str. */
77 wchar_t ch;
78
79 int b0_bits; /* Data bits in first byte. */
80 int cbytes; /* Number of continuation bytes. */
81
[e1813cf]82 if (*offset + 1 > sz)
[74c8da2c]83 return invalch;
[0dd1d444]84
[e1813cf]85 b0 = (uint8_t) str[(*offset)++];
[0dd1d444]86
87 /* Determine code length. */
88
89 if ((b0 & 0x80) == 0) {
90 /* 0xxxxxxx (Plain ASCII) */
91 b0_bits = 7;
92 cbytes = 0;
93 } else if ((b0 & 0xe0) == 0xc0) {
94 /* 110xxxxx 10xxxxxx */
95 b0_bits = 5;
96 cbytes = 1;
97 } else if ((b0 & 0xf0) == 0xe0) {
98 /* 1110xxxx 10xxxxxx 10xxxxxx */
99 b0_bits = 4;
100 cbytes = 2;
101 } else if ((b0 & 0xf8) == 0xf0) {
102 /* 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */
103 b0_bits = 3;
104 cbytes = 3;
105 } else {
106 /* 10xxxxxx -- unexpected continuation byte. */
107 return invalch;
[74c8da2c]108 }
[0dd1d444]109
[e1813cf]110 if (*offset + cbytes > sz) {
[0dd1d444]111 return invalch;
[74c8da2c]112 }
[0dd1d444]113
114 ch = b0 & LO_MASK_8(b0_bits);
115
116 /* Decode continuation bytes. */
117 while (cbytes > 0) {
[e1813cf]118 b = (uint8_t) str[(*offset)++];
[0dd1d444]119
120 /* Must be 10xxxxxx. */
121 if ((b & 0xc0) != 0x80) {
[74c8da2c]122 return invalch;
[0dd1d444]123 }
124
125 /* Shift data bits to ch. */
126 ch = (ch << CONT_BITS) | (wchar_t) (b & LO_MASK_8(CONT_BITS));
127 --cbytes;
[74c8da2c]128 }
[0dd1d444]129
130 return ch;
[74c8da2c]131}
132
[e1813cf]133/** Encode a single character to string representation.
[74c8da2c]134 *
[e1813cf]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.
[74c8da2c]138 *
[e1813cf]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.
[74c8da2c]143 *
[d09f84e6]144 * @return EOK if the character was encoded successfully, EOVERFLOW if there
145 * was not enough space in the output buffer or EINVAL if the character
146 * code was invalid.
[74c8da2c]147 */
[d09f84e6]148int chr_encode(const wchar_t ch, char *str, size_t *offset, size_t sz)
[74c8da2c]149{
[32704cb]150 uint32_t cc; /* Unsigned version of ch. */
151
152 int cbytes; /* Number of continuation bytes. */
153 int b0_bits; /* Number of data bits in first byte. */
154 int i;
155
[e1813cf]156 if (*offset >= sz)
[d09f84e6]157 return EOVERFLOW;
[32704cb]158
159 if (ch < 0)
[d09f84e6]160 return EINVAL;
[32704cb]161
162 /* Bit operations should only be done on unsigned numbers. */
163 cc = (uint32_t) ch;
164
165 /* Determine how many continuation bytes are needed. */
166 if ((cc & ~LO_MASK_32(7)) == 0) {
167 b0_bits = 7;
168 cbytes = 0;
169 } else if ((cc & ~LO_MASK_32(11)) == 0) {
170 b0_bits = 5;
171 cbytes = 1;
172 } else if ((cc & ~LO_MASK_32(16)) == 0) {
173 b0_bits = 4;
174 cbytes = 2;
175 } else if ((cc & ~LO_MASK_32(21)) == 0) {
176 b0_bits = 3;
177 cbytes = 3;
178 } else {
179 /* Codes longer than 21 bits are not supported. */
[d09f84e6]180 return EINVAL;
[74c8da2c]181 }
[32704cb]182
183 /* Check for available space in buffer. */
[e1813cf]184 if (*offset + cbytes >= sz)
[d09f84e6]185 return EOVERFLOW;
[32704cb]186
187 /* Encode continuation bytes. */
188 for (i = cbytes; i > 0; --i) {
[e1813cf]189 str[*offset + i] = 0x80 | (cc & LO_MASK_32(CONT_BITS));
[32704cb]190 cc = cc >> CONT_BITS;
[74c8da2c]191 }
[32704cb]192
193 /* Encode first byte. */
[e1813cf]194 str[*offset] = (cc & LO_MASK_32(b0_bits)) | HI_MASK_8(8 - b0_bits - 1);
[32704cb]195
[e1813cf]196 /* Advance offset. */
197 *offset += (1 + cbytes);
[74c8da2c]198
[d09f84e6]199 return EOK;
[74c8da2c]200}
201
[f25b2819]202/** Get size of string, with length limit.
[74c8da2c]203 *
[f25b2819]204 * 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
206 * the length of @a str, the entire string is measured.
[74c8da2c]207 *
[f25b2819]208 * @param str String to consider.
209 * @param count Maximum number of characters to measure.
[74c8da2c]210 *
211 * @return Number of bytes used by the characters.
212 */
[f25b2819]213size_t str_lsize(const char *str, count_t max_len)
[74c8da2c]214{
[f25b2819]215 count_t len = 0;
216 size_t cur = 0;
217 size_t prev;
[b54d2f1]218 wchar_t ch;
[f25b2819]219
[b54d2f1]220 while (true) {
[f25b2819]221 prev = cur;
222 if (len >= max_len)
[b54d2f1]223 break;
[f25b2819]224 ch = chr_decode(str, &cur, UTF8_NO_LIMIT);
[b54d2f1]225 if (ch == '\0') break;
226
[f25b2819]227 len++;
[21a639b7]228 }
[f25b2819]229
230 return prev;
[74c8da2c]231}
232
233/** Check whether character is plain ASCII.
234 *
235 * @return True if character is plain ASCII.
236 *
237 */
238bool ascii_check(const wchar_t ch)
239{
240 if ((ch >= 0) && (ch <= 127))
241 return true;
242
243 return false;
244}
245
246/** Check whether character is Unicode.
247 *
248 * @return True if character is valid Unicode code point.
249 */
250bool unicode_check(const wchar_t ch)
251{
252 if ((ch >= 0) && (ch <= 1114111))
253 return true;
254
255 return false;
[21a639b7]256}
257
[06b785f]258/** Return number of bytes the string occupies.
[2f57690]259 *
[06b785f]260 * @param str A string.
261 * @return Number of bytes in @a str excluding the null terminator.
[16da5f8e]262 */
[06b785f]263size_t str_size(const char *str)
[16da5f8e]264{
[74c8da2c]265 size_t size;
[06b785f]266
267 size = 0;
268 while (*str++ != '\0')
269 ++size;
270
[74c8da2c]271 return size;
272}
273
[f25b2819]274/** Return number of characters in a string.
[74c8da2c]275 *
[f25b2819]276 * @param str NULL-terminated string.
277 * @return Number of characters in string.
[74c8da2c]278 */
[f25b2819]279count_t str_length(const char *str)
[74c8da2c]280{
[f25b2819]281 count_t len = 0;
282 size_t offset = 0;
283
284 while (chr_decode(str, &offset, UTF8_NO_LIMIT) != 0) {
285 len++;
[74c8da2c]286 }
[f25b2819]287
288 return len;
[74c8da2c]289}
290
[f25b2819]291/** Return number of characters in a wide string.
[74c8da2c]292 *
[f25b2819]293 * @param str NULL-terminated wide string.
294 * @return Number of characters in @a str.
[74c8da2c]295 */
[f25b2819]296count_t wstr_length(const wchar_t *wstr)
[74c8da2c]297{
[f25b2819]298 count_t len;
299
300 len = 0;
301 while (*wstr++ != '\0')
302 ++len;
303
304 return len;
[16da5f8e]305}
306
307/** Compare two NULL terminated strings
308 *
309 * Do a char-by-char comparison of two NULL terminated strings.
310 * The strings are considered equal iff they consist of the same
311 * characters on the minimum of their lengths.
312 *
313 * @param src First string to compare.
314 * @param dst Second string to compare.
315 *
316 * @return 0 if the strings are equal, -1 if first is smaller, 1 if second smaller.
317 *
318 */
319int strcmp(const char *src, const char *dst)
320{
321 for (; *src && *dst; src++, dst++) {
322 if (*src < *dst)
323 return -1;
324 if (*src > *dst)
325 return 1;
326 }
327 if (*src == *dst)
328 return 0;
[2f57690]329
[16da5f8e]330 if (!*src)
331 return -1;
[2f57690]332
[16da5f8e]333 return 1;
334}
335
336
337/** Compare two NULL terminated strings
338 *
339 * Do a char-by-char comparison of two NULL terminated strings.
340 * The strings are considered equal iff they consist of the same
341 * characters on the minimum of their lengths and specified maximal
342 * length.
343 *
344 * @param src First string to compare.
345 * @param dst Second string to compare.
346 * @param len Maximal length for comparison.
347 *
348 * @return 0 if the strings are equal, -1 if first is smaller, 1 if second smaller.
349 */
350int strncmp(const char *src, const char *dst, size_t len)
351{
352 unsigned int i;
353
354 for (i = 0; (*src) && (*dst) && (i < len); src++, dst++, i++) {
355 if (*src < *dst)
356 return -1;
[2f57690]357
[16da5f8e]358 if (*src > *dst)
359 return 1;
360 }
[2f57690]361
[16da5f8e]362 if (i == len || *src == *dst)
363 return 0;
[2f57690]364
[16da5f8e]365 if (!*src)
366 return -1;
[2f57690]367
[16da5f8e]368 return 1;
369}
370
371
372
373/** Copy NULL terminated string.
374 *
375 * Copy at most 'len' characters from string 'src' to 'dest'.
376 * If 'src' is shorter than 'len', '\0' is inserted behind the
377 * last copied character.
378 *
[2f57690]379 * @param src Source string.
[16da5f8e]380 * @param dest Destination buffer.
[2f57690]381 * @param len Size of destination buffer.
[16da5f8e]382 */
383void strncpy(char *dest, const char *src, size_t len)
384{
385 unsigned int i;
[2f57690]386
[16da5f8e]387 for (i = 0; i < len; i++) {
388 if (!(dest[i] = src[i]))
389 return;
390 }
[2f57690]391
[16da5f8e]392 dest[i - 1] = '\0';
393}
394
[20f1597]395/** Find first occurence of character in string.
396 *
[2f57690]397 * @param s String to search.
398 * @param i Character to look for.
[20f1597]399 *
[2f57690]400 * @return Pointer to character in @a s or NULL if not found.
[20f1597]401 */
402extern char *strchr(const char *s, int i)
403{
404 while (*s != '\0') {
[2f57690]405 if (*s == i)
406 return (char *) s;
[20f1597]407 ++s;
408 }
[2f57690]409
[20f1597]410 return NULL;
411}
412
[16da5f8e]413/** @}
414 */
Note: See TracBrowser for help on using the repository browser.