source: mainline/kernel/generic/src/lib/str.c@ bc216a0

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since bc216a0 was 1066041, checked in by Adam Hraska <adam.hraska+hos@…>, 13 years ago

preemption_disable: Turned functions into macros. Moved THREAD, AS, TASK, CPU into thread.h, as.h, task.h, cpu.h to fix the include hell that ensued.

  • Property mode set to 100644
File size: 23.5 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
[82bb9c1]35 * @brief String functions.
36 *
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
39 * in UTF-32) are supported to a limited degree. A single character is
[b888d5f]40 * represented as wchar_t.@n
[82bb9c1]41 *
[b888d5f]42 * Overview of the terminology:@n
[82bb9c1]43 *
[b888d5f]44 * Term Meaning
45 * -------------------- ----------------------------------------------------
46 * byte 8 bits stored in uint8_t (unsigned 8 bit integer)
[82bb9c1]47 *
[b888d5f]48 * character UTF-32 encoded Unicode character, stored in wchar_t
49 * (signed 32 bit integer), code points 0 .. 1114111
50 * are valid
[82bb9c1]51 *
[b888d5f]52 * ASCII character 7 bit encoded ASCII character, stored in char
53 * (usually signed 8 bit integer), code points 0 .. 127
54 * are valid
55 *
56 * string UTF-8 encoded NULL-terminated Unicode string, char *
57 *
58 * wide string UTF-32 encoded NULL-terminated Unicode string,
59 * wchar_t *
60 *
61 * [wide] string size number of BYTES in a [wide] string (excluding
62 * the NULL-terminator), size_t
63 *
64 * [wide] string length number of CHARACTERS in a [wide] string (excluding
[98000fb]65 * the NULL-terminator), size_t
[b888d5f]66 *
67 * [wide] string width number of display cells on a monospace display taken
[98000fb]68 * by a [wide] string, size_t
[b888d5f]69 *
70 *
71 * Overview of string metrics:@n
72 *
73 * Metric Abbrev. Type Meaning
74 * ------ ------ ------ -------------------------------------------------
75 * size n size_t number of BYTES in a string (excluding the
76 * NULL-terminator)
77 *
[98000fb]78 * length l size_t number of CHARACTERS in a string (excluding the
[b888d5f]79 * null terminator)
80 *
[98000fb]81 * width w size_t number of display cells on a monospace display
[b888d5f]82 * taken by a string
83 *
84 *
85 * Function naming prefixes:@n
86 *
87 * chr_ operate on characters
88 * ascii_ operate on ASCII characters
89 * str_ operate on strings
90 * wstr_ operate on wide strings
91 *
92 * [w]str_[n|l|w] operate on a prefix limited by size, length
93 * or width
94 *
95 *
96 * A specific character inside a [wide] string can be referred to by:@n
97 *
98 * pointer (char *, wchar_t *)
99 * byte offset (size_t)
[98000fb]100 * character index (size_t)
[82bb9c1]101 *
[16da5f8e]102 */
103
[19f857a]104#include <str.h>
[16da5f8e]105#include <print.h>
106#include <cpu.h>
107#include <arch/asm.h>
108#include <arch.h>
[d09f84e6]109#include <errno.h>
[b888d5f]110#include <align.h>
[6700ee2]111#include <debug.h>
[30a5470]112#include <macros.h>
[1066041]113#include <mm/slab.h>
[16da5f8e]114
[8e893ae]115/** Check the condition if wchar_t is signed */
116#ifdef WCHAR_IS_UNSIGNED
117 #define WCHAR_SIGNED_CHECK(cond) (true)
118#else
119 #define WCHAR_SIGNED_CHECK(cond) (cond)
120#endif
121
[b888d5f]122/** Byte mask consisting of lowest @n bits (out of 8) */
123#define LO_MASK_8(n) ((uint8_t) ((1 << (n)) - 1))
[0dd1d444]124
[b888d5f]125/** Byte mask consisting of lowest @n bits (out of 32) */
126#define LO_MASK_32(n) ((uint32_t) ((1 << (n)) - 1))
[32704cb]127
[b888d5f]128/** Byte mask consisting of highest @n bits (out of 8) */
129#define HI_MASK_8(n) (~LO_MASK_8(8 - (n)))
[32704cb]130
[b888d5f]131/** Number of data bits in a UTF-8 continuation byte */
132#define CONT_BITS 6
[0dd1d444]133
[b888d5f]134/** Decode a single character from a string.
[21a639b7]135 *
[b888d5f]136 * Decode a single character from a string of size @a size. Decoding starts
[e1813cf]137 * at @a offset and this offset is moved to the beginning of the next
138 * character. In case of decoding error, offset generally advances at least
[b888d5f]139 * by one. However, offset is never moved beyond size.
[21a639b7]140 *
[b888d5f]141 * @param str String (not necessarily NULL-terminated).
142 * @param offset Byte offset in string where to start decoding.
143 * @param size Size of the string (in bytes).
144 *
[c8bf88d]145 * @return Value of decoded character, U_SPECIAL on decoding error or
[b888d5f]146 * NULL if attempt to decode beyond @a size.
[21a639b7]147 *
148 */
[b888d5f]149wchar_t str_decode(const char *str, size_t *offset, size_t size)
[21a639b7]150{
[b888d5f]151 if (*offset + 1 > size)
152 return 0;
153
154 /* First byte read from string */
155 uint8_t b0 = (uint8_t) str[(*offset)++];
156
157 /* Determine code length */
158
159 unsigned int b0_bits; /* Data bits in first byte */
160 unsigned int cbytes; /* Number of continuation bytes */
161
[0dd1d444]162 if ((b0 & 0x80) == 0) {
163 /* 0xxxxxxx (Plain ASCII) */
164 b0_bits = 7;
165 cbytes = 0;
166 } else if ((b0 & 0xe0) == 0xc0) {
167 /* 110xxxxx 10xxxxxx */
168 b0_bits = 5;
169 cbytes = 1;
170 } else if ((b0 & 0xf0) == 0xe0) {
171 /* 1110xxxx 10xxxxxx 10xxxxxx */
172 b0_bits = 4;
173 cbytes = 2;
174 } else if ((b0 & 0xf8) == 0xf0) {
175 /* 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */
176 b0_bits = 3;
177 cbytes = 3;
178 } else {
[b888d5f]179 /* 10xxxxxx -- unexpected continuation byte */
[c8bf88d]180 return U_SPECIAL;
[74c8da2c]181 }
[b888d5f]182
183 if (*offset + cbytes > size)
[c8bf88d]184 return U_SPECIAL;
[b888d5f]185
186 wchar_t ch = b0 & LO_MASK_8(b0_bits);
187
188 /* Decode continuation bytes */
[0dd1d444]189 while (cbytes > 0) {
[b888d5f]190 uint8_t b = (uint8_t) str[(*offset)++];
191
192 /* Must be 10xxxxxx */
193 if ((b & 0xc0) != 0x80)
[c8bf88d]194 return U_SPECIAL;
[b888d5f]195
196 /* Shift data bits to ch */
[0dd1d444]197 ch = (ch << CONT_BITS) | (wchar_t) (b & LO_MASK_8(CONT_BITS));
[b888d5f]198 cbytes--;
[74c8da2c]199 }
[b888d5f]200
[0dd1d444]201 return ch;
[74c8da2c]202}
203
[e1813cf]204/** Encode a single character to string representation.
[74c8da2c]205 *
[e1813cf]206 * Encode a single character to string representation (i.e. UTF-8) and store
207 * it into a buffer at @a offset. Encoding starts at @a offset and this offset
208 * is moved to the position where the next character can be written to.
[74c8da2c]209 *
[b888d5f]210 * @param ch Input character.
211 * @param str Output buffer.
212 * @param offset Byte offset where to start writing.
213 * @param size Size of the output buffer (in bytes).
[74c8da2c]214 *
[d09f84e6]215 * @return EOK if the character was encoded successfully, EOVERFLOW if there
[8e893ae]216 * was not enough space in the output buffer or EINVAL if the character
217 * code was invalid.
[74c8da2c]218 */
[8e893ae]219int chr_encode(const wchar_t ch, char *str, size_t *offset, size_t size)
[74c8da2c]220{
[b888d5f]221 if (*offset >= size)
[d09f84e6]222 return EOVERFLOW;
[b888d5f]223
224 if (!chr_check(ch))
[d09f84e6]225 return EINVAL;
[b888d5f]226
227 /* Unsigned version of ch (bit operations should only be done
228 on unsigned types). */
229 uint32_t cc = (uint32_t) ch;
230
231 /* Determine how many continuation bytes are needed */
232
233 unsigned int b0_bits; /* Data bits in first byte */
234 unsigned int cbytes; /* Number of continuation bytes */
235
[32704cb]236 if ((cc & ~LO_MASK_32(7)) == 0) {
237 b0_bits = 7;
238 cbytes = 0;
239 } else if ((cc & ~LO_MASK_32(11)) == 0) {
240 b0_bits = 5;
241 cbytes = 1;
242 } else if ((cc & ~LO_MASK_32(16)) == 0) {
243 b0_bits = 4;
244 cbytes = 2;
245 } else if ((cc & ~LO_MASK_32(21)) == 0) {
246 b0_bits = 3;
247 cbytes = 3;
248 } else {
[b888d5f]249 /* Codes longer than 21 bits are not supported */
[d09f84e6]250 return EINVAL;
[74c8da2c]251 }
[b888d5f]252
253 /* Check for available space in buffer */
254 if (*offset + cbytes >= size)
[d09f84e6]255 return EOVERFLOW;
[b888d5f]256
257 /* Encode continuation bytes */
258 unsigned int i;
259 for (i = cbytes; i > 0; i--) {
[e1813cf]260 str[*offset + i] = 0x80 | (cc & LO_MASK_32(CONT_BITS));
[32704cb]261 cc = cc >> CONT_BITS;
[74c8da2c]262 }
[b888d5f]263
264 /* Encode first byte */
[e1813cf]265 str[*offset] = (cc & LO_MASK_32(b0_bits)) | HI_MASK_8(8 - b0_bits - 1);
[b888d5f]266
267 /* Advance offset */
268 *offset += cbytes + 1;
[74c8da2c]269
[d09f84e6]270 return EOK;
[74c8da2c]271}
272
[b888d5f]273/** Get size of string.
274 *
275 * Get the number of bytes which are used by the string @a str (excluding the
276 * NULL-terminator).
277 *
278 * @param str String to consider.
279 *
280 * @return Number of bytes used by the string
[82bb9c1]281 *
282 */
[b888d5f]283size_t str_size(const char *str)
[82bb9c1]284{
[b888d5f]285 size_t size = 0;
286
287 while (*str++ != 0)
288 size++;
289
290 return size;
[82bb9c1]291}
292
[b888d5f]293/** Get size of wide string.
294 *
295 * Get the number of bytes which are used by the wide string @a str (excluding the
296 * NULL-terminator).
297 *
298 * @param str Wide string to consider.
299 *
300 * @return Number of bytes used by the wide string
301 *
302 */
303size_t wstr_size(const wchar_t *str)
304{
305 return (wstr_length(str) * sizeof(wchar_t));
306}
307
308/** Get size of string with length limit.
[74c8da2c]309 *
[f25b2819]310 * Get the number of bytes which are used by up to @a max_len first
311 * characters in the string @a str. If @a max_len is greater than
[b888d5f]312 * the length of @a str, the entire string is measured (excluding the
313 * NULL-terminator).
314 *
315 * @param str String to consider.
316 * @param max_len Maximum number of characters to measure.
[74c8da2c]317 *
[b888d5f]318 * @return Number of bytes used by the characters.
[74c8da2c]319 *
320 */
[98000fb]321size_t str_lsize(const char *str, size_t max_len)
[74c8da2c]322{
[98000fb]323 size_t len = 0;
[b888d5f]324 size_t offset = 0;
325
326 while (len < max_len) {
327 if (str_decode(str, &offset, STR_NO_LIMIT) == 0)
[b54d2f1]328 break;
[b888d5f]329
[f25b2819]330 len++;
[21a639b7]331 }
[b888d5f]332
333 return offset;
[74c8da2c]334}
335
[b888d5f]336/** Get size of wide string with length limit.
[82bb9c1]337 *
[b888d5f]338 * Get the number of bytes which are used by up to @a max_len first
339 * wide characters in the wide string @a str. If @a max_len is greater than
340 * the length of @a str, the entire wide string is measured (excluding the
341 * NULL-terminator).
342 *
343 * @param str Wide string to consider.
344 * @param max_len Maximum number of wide characters to measure.
[82bb9c1]345 *
[b888d5f]346 * @return Number of bytes used by the wide characters.
[82bb9c1]347 *
348 */
[98000fb]349size_t wstr_lsize(const wchar_t *str, size_t max_len)
[82bb9c1]350{
[b888d5f]351 return (wstr_nlength(str, max_len * sizeof(wchar_t)) * sizeof(wchar_t));
[82bb9c1]352}
353
[b888d5f]354/** Get number of characters in a string.
[82bb9c1]355 *
[b888d5f]356 * @param str NULL-terminated string.
[82bb9c1]357 *
[b888d5f]358 * @return Number of characters in string.
[82bb9c1]359 *
360 */
[98000fb]361size_t str_length(const char *str)
[82bb9c1]362{
[98000fb]363 size_t len = 0;
[b888d5f]364 size_t offset = 0;
365
366 while (str_decode(str, &offset, STR_NO_LIMIT) != 0)
367 len++;
368
369 return len;
[82bb9c1]370}
371
[b888d5f]372/** Get number of characters in a wide string.
[74c8da2c]373 *
[b888d5f]374 * @param str NULL-terminated wide string.
375 *
376 * @return Number of characters in @a str.
[74c8da2c]377 *
378 */
[98000fb]379size_t wstr_length(const wchar_t *wstr)
[74c8da2c]380{
[98000fb]381 size_t len = 0;
[74c8da2c]382
[b888d5f]383 while (*wstr++ != 0)
384 len++;
385
386 return len;
[74c8da2c]387}
388
[b888d5f]389/** Get number of characters in a string with size limit.
390 *
391 * @param str NULL-terminated string.
392 * @param size Maximum number of bytes to consider.
393 *
394 * @return Number of characters in string.
[74c8da2c]395 *
396 */
[98000fb]397size_t str_nlength(const char *str, size_t size)
[74c8da2c]398{
[98000fb]399 size_t len = 0;
[b888d5f]400 size_t offset = 0;
[74c8da2c]401
[b888d5f]402 while (str_decode(str, &offset, size) != 0)
403 len++;
404
405 return len;
[21a639b7]406}
407
[b888d5f]408/** Get number of characters in a string with size limit.
[2f57690]409 *
[b888d5f]410 * @param str NULL-terminated string.
411 * @param size Maximum number of bytes to consider.
[74c8da2c]412 *
[f25b2819]413 * @return Number of characters in string.
[b888d5f]414 *
[74c8da2c]415 */
[98000fb]416size_t wstr_nlength(const wchar_t *str, size_t size)
[74c8da2c]417{
[98000fb]418 size_t len = 0;
419 size_t limit = ALIGN_DOWN(size, sizeof(wchar_t));
420 size_t offset = 0;
[b888d5f]421
422 while ((offset < limit) && (*str++ != 0)) {
[f25b2819]423 len++;
[b888d5f]424 offset += sizeof(wchar_t);
[74c8da2c]425 }
[b888d5f]426
[f25b2819]427 return len;
[74c8da2c]428}
429
[b888d5f]430/** Check whether character is plain ASCII.
431 *
432 * @return True if character is plain ASCII.
[74c8da2c]433 *
434 */
[f2b8cdc]435bool ascii_check(wchar_t ch)
[74c8da2c]436{
[8e893ae]437 if (WCHAR_SIGNED_CHECK(ch >= 0) && (ch <= 127))
[b888d5f]438 return true;
439
440 return false;
441}
[f25b2819]442
[b888d5f]443/** Check whether character is valid
444 *
445 * @return True if character is a valid Unicode code point.
446 *
447 */
[f2b8cdc]448bool chr_check(wchar_t ch)
[b888d5f]449{
[8e893ae]450 if (WCHAR_SIGNED_CHECK(ch >= 0) && (ch <= 1114111))
[b888d5f]451 return true;
452
453 return false;
[16da5f8e]454}
455
[b888d5f]456/** Compare two NULL terminated strings.
[16da5f8e]457 *
[b888d5f]458 * Do a char-by-char comparison of two NULL-terminated strings.
[16da5f8e]459 * The strings are considered equal iff they consist of the same
460 * characters on the minimum of their lengths.
461 *
[b888d5f]462 * @param s1 First string to compare.
463 * @param s2 Second string to compare.
[16da5f8e]464 *
[b888d5f]465 * @return 0 if the strings are equal, -1 if first is smaller,
466 * 1 if second smaller.
[16da5f8e]467 *
468 */
[b888d5f]469int str_cmp(const char *s1, const char *s2)
[16da5f8e]470{
[a7b1071]471 wchar_t c1 = 0;
472 wchar_t c2 = 0;
[b888d5f]473
474 size_t off1 = 0;
475 size_t off2 = 0;
[a7b1071]476
477 while (true) {
478 c1 = str_decode(s1, &off1, STR_NO_LIMIT);
479 c2 = str_decode(s2, &off2, STR_NO_LIMIT);
480
[b888d5f]481 if (c1 < c2)
[16da5f8e]482 return -1;
[b888d5f]483
484 if (c1 > c2)
[16da5f8e]485 return 1;
[a7b1071]486
487 if (c1 == 0 || c2 == 0)
488 break;
[16da5f8e]489 }
[a7b1071]490
491 return 0;
[16da5f8e]492}
493
[b888d5f]494/** Compare two NULL terminated strings with length limit.
[16da5f8e]495 *
[b888d5f]496 * Do a char-by-char comparison of two NULL-terminated strings.
[16da5f8e]497 * The strings are considered equal iff they consist of the same
[b888d5f]498 * characters on the minimum of their lengths and the length limit.
[16da5f8e]499 *
[b888d5f]500 * @param s1 First string to compare.
501 * @param s2 Second string to compare.
502 * @param max_len Maximum number of characters to consider.
503 *
504 * @return 0 if the strings are equal, -1 if first is smaller,
505 * 1 if second smaller.
[16da5f8e]506 *
507 */
[98000fb]508int str_lcmp(const char *s1, const char *s2, size_t max_len)
[16da5f8e]509{
[b888d5f]510 wchar_t c1 = 0;
511 wchar_t c2 = 0;
[16da5f8e]512
[b888d5f]513 size_t off1 = 0;
514 size_t off2 = 0;
515
[98000fb]516 size_t len = 0;
[a7b1071]517
518 while (true) {
519 if (len >= max_len)
[b888d5f]520 break;
[a7b1071]521
522 c1 = str_decode(s1, &off1, STR_NO_LIMIT);
523 c2 = str_decode(s2, &off2, STR_NO_LIMIT);
524
[b888d5f]525 if (c1 < c2)
[16da5f8e]526 return -1;
[a7b1071]527
[b888d5f]528 if (c1 > c2)
[16da5f8e]529 return 1;
[a7b1071]530
531 if (c1 == 0 || c2 == 0)
532 break;
533
534 ++len;
[16da5f8e]535 }
[a7b1071]536
537 return 0;
538
[16da5f8e]539}
540
[f4b1535]541/** Copy string.
[b888d5f]542 *
[f4b1535]543 * Copy source string @a src to destination buffer @a dest.
544 * No more than @a size bytes are written. If the size of the output buffer
545 * is at least one byte, the output string will always be well-formed, i.e.
546 * null-terminated and containing only complete characters.
[b888d5f]547 *
[abf09311]548 * @param dest Destination buffer.
[6700ee2]549 * @param count Size of the destination buffer (must be > 0).
[f4b1535]550 * @param src Source string.
[abf09311]551 *
[b888d5f]552 */
[f4b1535]553void str_cpy(char *dest, size_t size, const char *src)
[b888d5f]554{
[6700ee2]555 /* There must be space for a null terminator in the buffer. */
556 ASSERT(size > 0);
[181a746]557 ASSERT(src != NULL);
[b888d5f]558
[abf09311]559 size_t src_off = 0;
560 size_t dest_off = 0;
561
562 wchar_t ch;
[f4b1535]563 while ((ch = str_decode(src, &src_off, STR_NO_LIMIT)) != 0) {
564 if (chr_encode(ch, dest, &dest_off, size - 1) != EOK)
565 break;
566 }
[abf09311]567
[f4b1535]568 dest[dest_off] = '\0';
569}
570
571/** Copy size-limited substring.
572 *
[6700ee2]573 * Copy prefix of string @a src of max. size @a size to destination buffer
574 * @a dest. No more than @a size bytes are written. The output string will
575 * always be well-formed, i.e. null-terminated and containing only complete
576 * characters.
[f4b1535]577 *
578 * No more than @a n bytes are read from the input string, so it does not
579 * have to be null-terminated.
580 *
[abf09311]581 * @param dest Destination buffer.
[6700ee2]582 * @param count Size of the destination buffer (must be > 0).
[f4b1535]583 * @param src Source string.
[abf09311]584 * @param n Maximum number of bytes to read from @a src.
585 *
[f4b1535]586 */
587void str_ncpy(char *dest, size_t size, const char *src, size_t n)
588{
[6700ee2]589 /* There must be space for a null terminator in the buffer. */
590 ASSERT(size > 0);
[b888d5f]591
[abf09311]592 size_t src_off = 0;
593 size_t dest_off = 0;
594
595 wchar_t ch;
[f4b1535]596 while ((ch = str_decode(src, &src_off, n)) != 0) {
597 if (chr_encode(ch, dest, &dest_off, size - 1) != EOK)
[b888d5f]598 break;
599 }
[abf09311]600
[f4b1535]601 dest[dest_off] = '\0';
[b888d5f]602}
[16da5f8e]603
[abf09311]604/** Duplicate string.
605 *
606 * Allocate a new string and copy characters from the source
607 * string into it. The duplicate string is allocated via sleeping
608 * malloc(), thus this function can sleep in no memory conditions.
609 *
610 * The allocation cannot fail and the return value is always
611 * a valid pointer. The duplicate string is always a well-formed
612 * null-terminated UTF-8 string, but it can differ from the source
613 * string on the byte level.
614 *
615 * @param src Source string.
616 *
617 * @return Duplicate string.
618 *
619 */
620char *str_dup(const char *src)
621{
622 size_t size = str_size(src) + 1;
623 char *dest = malloc(size, 0);
624 ASSERT(dest);
625
626 str_cpy(dest, size, src);
627 return dest;
628}
629
630/** Duplicate string with size limit.
631 *
632 * Allocate a new string and copy up to @max_size bytes from the source
633 * string into it. The duplicate string is allocated via sleeping
634 * malloc(), thus this function can sleep in no memory conditions.
635 * No more than @max_size + 1 bytes is allocated, but if the size
636 * occupied by the source string is smaller than @max_size + 1,
637 * less is allocated.
638 *
639 * The allocation cannot fail and the return value is always
640 * a valid pointer. The duplicate string is always a well-formed
641 * null-terminated UTF-8 string, but it can differ from the source
642 * string on the byte level.
643 *
644 * @param src Source string.
645 * @param n Maximum number of bytes to duplicate.
646 *
647 * @return Duplicate string.
648 *
649 */
650char *str_ndup(const char *src, size_t n)
651{
652 size_t size = str_size(src);
653 if (size > n)
654 size = n;
655
656 char *dest = malloc(size + 1, 0);
657 ASSERT(dest);
658
659 str_ncpy(dest, size + 1, src, size);
660 return dest;
661}
662
[0f06dbc]663/** Convert wide string to string.
[b888d5f]664 *
[0f06dbc]665 * Convert wide string @a src to string. The output is written to the buffer
666 * specified by @a dest and @a size. @a size must be non-zero and the string
667 * written will always be well-formed.
[16da5f8e]668 *
[0f06dbc]669 * @param dest Destination buffer.
670 * @param size Size of the destination buffer.
671 * @param src Source wide string.
[16da5f8e]672 */
[0f06dbc]673void wstr_to_str(char *dest, size_t size, const wchar_t *src)
[16da5f8e]674{
[b888d5f]675 wchar_t ch;
[0f06dbc]676 size_t src_idx;
677 size_t dest_off;
678
679 /* There must be space for a null terminator in the buffer. */
680 ASSERT(size > 0);
681
682 src_idx = 0;
683 dest_off = 0;
[b888d5f]684
685 while ((ch = src[src_idx++]) != 0) {
[0f06dbc]686 if (chr_encode(ch, dest, &dest_off, size - 1) != EOK)
[b888d5f]687 break;
[16da5f8e]688 }
[0f06dbc]689
690 dest[dest_off] = '\0';
[16da5f8e]691}
692
[20f1597]693/** Find first occurence of character in string.
694 *
[b888d5f]695 * @param str String to search.
696 * @param ch Character to look for.
697 *
698 * @return Pointer to character in @a str or NULL if not found.
[20f1597]699 *
700 */
[dd2cfa7]701char *str_chr(const char *str, wchar_t ch)
[20f1597]702{
[b888d5f]703 wchar_t acc;
704 size_t off = 0;
[f2d2c7ba]705 size_t last = 0;
[b888d5f]706
[a7b1071]707 while ((acc = str_decode(str, &off, STR_NO_LIMIT)) != 0) {
[b888d5f]708 if (acc == ch)
[dd2cfa7]709 return (char *) (str + last);
[f2d2c7ba]710 last = off;
[20f1597]711 }
[2f57690]712
[20f1597]713 return NULL;
714}
715
[b888d5f]716/** Insert a wide character into a wide string.
717 *
718 * Insert a wide character into a wide string at position
719 * @a pos. The characters after the position are shifted.
720 *
721 * @param str String to insert to.
722 * @param ch Character to insert to.
723 * @param pos Character index where to insert.
724 @ @param max_pos Characters in the buffer.
725 *
726 * @return True if the insertion was sucessful, false if the position
727 * is out of bounds.
728 *
729 */
[98000fb]730bool wstr_linsert(wchar_t *str, wchar_t ch, size_t pos, size_t max_pos)
[b888d5f]731{
[98000fb]732 size_t len = wstr_length(str);
[b888d5f]733
734 if ((pos > len) || (pos + 1 > max_pos))
735 return false;
736
[98000fb]737 size_t i;
[b888d5f]738 for (i = len; i + 1 > pos; i--)
739 str[i + 1] = str[i];
740
741 str[pos] = ch;
742
743 return true;
744}
745
746/** Remove a wide character from a wide string.
747 *
748 * Remove a wide character from a wide string at position
749 * @a pos. The characters after the position are shifted.
750 *
751 * @param str String to remove from.
752 * @param pos Character index to remove.
753 *
754 * @return True if the removal was sucessful, false if the position
755 * is out of bounds.
756 *
757 */
[98000fb]758bool wstr_remove(wchar_t *str, size_t pos)
[b888d5f]759{
[98000fb]760 size_t len = wstr_length(str);
[b888d5f]761
762 if (pos >= len)
763 return false;
764
[98000fb]765 size_t i;
[b888d5f]766 for (i = pos + 1; i <= len; i++)
767 str[i - 1] = str[i];
768
769 return true;
770}
771
[30a5470]772/** Convert string to uint64_t (internal variant).
773 *
774 * @param nptr Pointer to string.
775 * @param endptr Pointer to the first invalid character is stored here.
776 * @param base Zero or number between 2 and 36 inclusive.
777 * @param neg Indication of unary minus is stored here.
778 * @apram result Result of the conversion.
779 *
780 * @return EOK if conversion was successful.
781 *
782 */
783static int str_uint(const char *nptr, char **endptr, unsigned int base,
784 bool *neg, uint64_t *result)
785{
786 ASSERT(endptr != NULL);
787 ASSERT(neg != NULL);
788 ASSERT(result != NULL);
789
790 *neg = false;
791 const char *str = nptr;
792
793 /* Ignore leading whitespace */
794 while (isspace(*str))
795 str++;
796
797 if (*str == '-') {
798 *neg = true;
799 str++;
800 } else if (*str == '+')
801 str++;
802
803 if (base == 0) {
804 /* Decode base if not specified */
805 base = 10;
806
807 if (*str == '0') {
808 base = 8;
809 str++;
810
811 switch (*str) {
812 case 'b':
813 case 'B':
814 base = 2;
815 str++;
816 break;
817 case 'o':
818 case 'O':
819 base = 8;
820 str++;
821 break;
822 case 'd':
823 case 'D':
824 case 't':
825 case 'T':
826 base = 10;
827 str++;
828 break;
829 case 'x':
830 case 'X':
831 base = 16;
832 str++;
833 break;
[4ce914d4]834 default:
835 str--;
[30a5470]836 }
837 }
838 } else {
839 /* Check base range */
840 if ((base < 2) || (base > 36)) {
841 *endptr = (char *) str;
842 return EINVAL;
843 }
844 }
845
846 *result = 0;
847 const char *startstr = str;
848
849 while (*str != 0) {
850 unsigned int digit;
851
852 if ((*str >= 'a') && (*str <= 'z'))
853 digit = *str - 'a' + 10;
854 else if ((*str >= 'A') && (*str <= 'Z'))
855 digit = *str - 'A' + 10;
856 else if ((*str >= '0') && (*str <= '9'))
857 digit = *str - '0';
858 else
859 break;
860
861 if (digit >= base)
862 break;
863
864 uint64_t prev = *result;
865 *result = (*result) * base + digit;
866
867 if (*result < prev) {
868 /* Overflow */
869 *endptr = (char *) str;
870 return EOVERFLOW;
871 }
872
873 str++;
874 }
875
876 if (str == startstr) {
877 /*
878 * No digits were decoded => first invalid character is
879 * the first character of the string.
880 */
881 str = nptr;
882 }
883
884 *endptr = (char *) str;
885
886 if (str == nptr)
887 return EINVAL;
888
889 return EOK;
890}
891
892/** Convert string to uint64_t.
893 *
894 * @param nptr Pointer to string.
895 * @param endptr If not NULL, pointer to the first invalid character
896 * is stored here.
897 * @param base Zero or number between 2 and 36 inclusive.
898 * @param strict Do not allow any trailing characters.
[4ce914d4]899 * @param result Result of the conversion.
[30a5470]900 *
901 * @return EOK if conversion was successful.
902 *
903 */
[059a8e4]904int str_uint64_t(const char *nptr, char **endptr, unsigned int base,
[30a5470]905 bool strict, uint64_t *result)
906{
907 ASSERT(result != NULL);
908
909 bool neg;
910 char *lendptr;
911 int ret = str_uint(nptr, &lendptr, base, &neg, result);
912
913 if (endptr != NULL)
914 *endptr = (char *) lendptr;
915
916 if (ret != EOK)
917 return ret;
918
919 /* Do not allow negative values */
920 if (neg)
921 return EINVAL;
922
923 /* Check whether we are at the end of
924 the string in strict mode */
925 if ((strict) && (*lendptr != 0))
926 return EINVAL;
927
928 return EOK;
929}
930
[e535eeb]931void order_suffix(const uint64_t val, uint64_t *rv, char *suffix)
932{
[933cadf]933 if (val > UINT64_C(10000000000000000000)) {
934 *rv = val / UINT64_C(1000000000000000000);
[e535eeb]935 *suffix = 'Z';
[933cadf]936 } else if (val > UINT64_C(1000000000000000000)) {
937 *rv = val / UINT64_C(1000000000000000);
[e535eeb]938 *suffix = 'E';
[933cadf]939 } else if (val > UINT64_C(1000000000000000)) {
940 *rv = val / UINT64_C(1000000000000);
[e535eeb]941 *suffix = 'T';
[933cadf]942 } else if (val > UINT64_C(1000000000000)) {
943 *rv = val / UINT64_C(1000000000);
[e535eeb]944 *suffix = 'G';
[933cadf]945 } else if (val > UINT64_C(1000000000)) {
946 *rv = val / UINT64_C(1000000);
[e535eeb]947 *suffix = 'M';
[933cadf]948 } else if (val > UINT64_C(1000000)) {
949 *rv = val / UINT64_C(1000);
[e535eeb]950 *suffix = 'k';
951 } else {
952 *rv = val;
953 *suffix = ' ';
954 }
955}
956
[933cadf]957void bin_order_suffix(const uint64_t val, uint64_t *rv, const char **suffix,
958 bool fixed)
959{
960 if (val > UINT64_C(1152921504606846976)) {
961 *rv = val / UINT64_C(1125899906842624);
962 *suffix = "EiB";
963 } else if (val > UINT64_C(1125899906842624)) {
964 *rv = val / UINT64_C(1099511627776);
965 *suffix = "TiB";
966 } else if (val > UINT64_C(1099511627776)) {
967 *rv = val / UINT64_C(1073741824);
968 *suffix = "GiB";
969 } else if (val > UINT64_C(1073741824)) {
970 *rv = val / UINT64_C(1048576);
971 *suffix = "MiB";
972 } else if (val > UINT64_C(1048576)) {
973 *rv = val / UINT64_C(1024);
974 *suffix = "KiB";
975 } else {
976 *rv = val;
977 if (fixed)
978 *suffix = "B ";
979 else
980 *suffix = "B";
981 }
982}
983
[16da5f8e]984/** @}
985 */
Note: See TracBrowser for help on using the repository browser.