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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 935e28c 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
Line 
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
29/** @addtogroup generic
30 * @{
31 */
32
33/**
34 * @file
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
40 * represented as wchar_t.@n
41 *
42 * Overview of the terminology:@n
43 *
44 * Term Meaning
45 * -------------------- ----------------------------------------------------
46 * byte 8 bits stored in uint8_t (unsigned 8 bit integer)
47 *
48 * character UTF-32 encoded Unicode character, stored in wchar_t
49 * (signed 32 bit integer), code points 0 .. 1114111
50 * are valid
51 *
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
65 * the NULL-terminator), size_t
66 *
67 * [wide] string width number of display cells on a monospace display taken
68 * by a [wide] string, size_t
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 *
78 * length l size_t number of CHARACTERS in a string (excluding the
79 * null terminator)
80 *
81 * width w size_t number of display cells on a monospace display
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)
100 * character index (size_t)
101 *
102 */
103
104#include <str.h>
105#include <print.h>
106#include <cpu.h>
107#include <arch/asm.h>
108#include <arch.h>
109#include <errno.h>
110#include <align.h>
111#include <debug.h>
112#include <macros.h>
113#include <mm/slab.h>
114
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
122/** Byte mask consisting of lowest @n bits (out of 8) */
123#define LO_MASK_8(n) ((uint8_t) ((1 << (n)) - 1))
124
125/** Byte mask consisting of lowest @n bits (out of 32) */
126#define LO_MASK_32(n) ((uint32_t) ((1 << (n)) - 1))
127
128/** Byte mask consisting of highest @n bits (out of 8) */
129#define HI_MASK_8(n) (~LO_MASK_8(8 - (n)))
130
131/** Number of data bits in a UTF-8 continuation byte */
132#define CONT_BITS 6
133
134/** Decode a single character from a string.
135 *
136 * Decode a single character from a string of size @a size. Decoding starts
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
139 * by one. However, offset is never moved beyond size.
140 *
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 *
145 * @return Value of decoded character, U_SPECIAL on decoding error or
146 * NULL if attempt to decode beyond @a size.
147 *
148 */
149wchar_t str_decode(const char *str, size_t *offset, size_t size)
150{
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
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 {
179 /* 10xxxxxx -- unexpected continuation byte */
180 return U_SPECIAL;
181 }
182
183 if (*offset + cbytes > size)
184 return U_SPECIAL;
185
186 wchar_t ch = b0 & LO_MASK_8(b0_bits);
187
188 /* Decode continuation bytes */
189 while (cbytes > 0) {
190 uint8_t b = (uint8_t) str[(*offset)++];
191
192 /* Must be 10xxxxxx */
193 if ((b & 0xc0) != 0x80)
194 return U_SPECIAL;
195
196 /* Shift data bits to ch */
197 ch = (ch << CONT_BITS) | (wchar_t) (b & LO_MASK_8(CONT_BITS));
198 cbytes--;
199 }
200
201 return ch;
202}
203
204/** Encode a single character to string representation.
205 *
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.
209 *
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).
214 *
215 * @return EOK if the character was encoded successfully, EOVERFLOW if there
216 * was not enough space in the output buffer or EINVAL if the character
217 * code was invalid.
218 */
219int chr_encode(const wchar_t ch, char *str, size_t *offset, size_t size)
220{
221 if (*offset >= size)
222 return EOVERFLOW;
223
224 if (!chr_check(ch))
225 return EINVAL;
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
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 {
249 /* Codes longer than 21 bits are not supported */
250 return EINVAL;
251 }
252
253 /* Check for available space in buffer */
254 if (*offset + cbytes >= size)
255 return EOVERFLOW;
256
257 /* Encode continuation bytes */
258 unsigned int i;
259 for (i = cbytes; i > 0; i--) {
260 str[*offset + i] = 0x80 | (cc & LO_MASK_32(CONT_BITS));
261 cc = cc >> CONT_BITS;
262 }
263
264 /* Encode first byte */
265 str[*offset] = (cc & LO_MASK_32(b0_bits)) | HI_MASK_8(8 - b0_bits - 1);
266
267 /* Advance offset */
268 *offset += cbytes + 1;
269
270 return EOK;
271}
272
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
281 *
282 */
283size_t str_size(const char *str)
284{
285 size_t size = 0;
286
287 while (*str++ != 0)
288 size++;
289
290 return size;
291}
292
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.
309 *
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
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.
317 *
318 * @return Number of bytes used by the characters.
319 *
320 */
321size_t str_lsize(const char *str, size_t max_len)
322{
323 size_t len = 0;
324 size_t offset = 0;
325
326 while (len < max_len) {
327 if (str_decode(str, &offset, STR_NO_LIMIT) == 0)
328 break;
329
330 len++;
331 }
332
333 return offset;
334}
335
336/** Get size of wide string with length limit.
337 *
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.
345 *
346 * @return Number of bytes used by the wide characters.
347 *
348 */
349size_t wstr_lsize(const wchar_t *str, size_t max_len)
350{
351 return (wstr_nlength(str, max_len * sizeof(wchar_t)) * sizeof(wchar_t));
352}
353
354/** Get number of characters in a string.
355 *
356 * @param str NULL-terminated string.
357 *
358 * @return Number of characters in string.
359 *
360 */
361size_t str_length(const char *str)
362{
363 size_t len = 0;
364 size_t offset = 0;
365
366 while (str_decode(str, &offset, STR_NO_LIMIT) != 0)
367 len++;
368
369 return len;
370}
371
372/** Get number of characters in a wide string.
373 *
374 * @param str NULL-terminated wide string.
375 *
376 * @return Number of characters in @a str.
377 *
378 */
379size_t wstr_length(const wchar_t *wstr)
380{
381 size_t len = 0;
382
383 while (*wstr++ != 0)
384 len++;
385
386 return len;
387}
388
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.
395 *
396 */
397size_t str_nlength(const char *str, size_t size)
398{
399 size_t len = 0;
400 size_t offset = 0;
401
402 while (str_decode(str, &offset, size) != 0)
403 len++;
404
405 return len;
406}
407
408/** Get number of characters in a string with size limit.
409 *
410 * @param str NULL-terminated string.
411 * @param size Maximum number of bytes to consider.
412 *
413 * @return Number of characters in string.
414 *
415 */
416size_t wstr_nlength(const wchar_t *str, size_t size)
417{
418 size_t len = 0;
419 size_t limit = ALIGN_DOWN(size, sizeof(wchar_t));
420 size_t offset = 0;
421
422 while ((offset < limit) && (*str++ != 0)) {
423 len++;
424 offset += sizeof(wchar_t);
425 }
426
427 return len;
428}
429
430/** Check whether character is plain ASCII.
431 *
432 * @return True if character is plain ASCII.
433 *
434 */
435bool ascii_check(wchar_t ch)
436{
437 if (WCHAR_SIGNED_CHECK(ch >= 0) && (ch <= 127))
438 return true;
439
440 return false;
441}
442
443/** Check whether character is valid
444 *
445 * @return True if character is a valid Unicode code point.
446 *
447 */
448bool chr_check(wchar_t ch)
449{
450 if (WCHAR_SIGNED_CHECK(ch >= 0) && (ch <= 1114111))
451 return true;
452
453 return false;
454}
455
456/** Compare two NULL terminated strings.
457 *
458 * Do a char-by-char comparison of two NULL-terminated strings.
459 * The strings are considered equal iff they consist of the same
460 * characters on the minimum of their lengths.
461 *
462 * @param s1 First string to compare.
463 * @param s2 Second string to compare.
464 *
465 * @return 0 if the strings are equal, -1 if first is smaller,
466 * 1 if second smaller.
467 *
468 */
469int str_cmp(const char *s1, const char *s2)
470{
471 wchar_t c1 = 0;
472 wchar_t c2 = 0;
473
474 size_t off1 = 0;
475 size_t off2 = 0;
476
477 while (true) {
478 c1 = str_decode(s1, &off1, STR_NO_LIMIT);
479 c2 = str_decode(s2, &off2, STR_NO_LIMIT);
480
481 if (c1 < c2)
482 return -1;
483
484 if (c1 > c2)
485 return 1;
486
487 if (c1 == 0 || c2 == 0)
488 break;
489 }
490
491 return 0;
492}
493
494/** Compare two NULL terminated strings with length limit.
495 *
496 * Do a char-by-char comparison of two NULL-terminated strings.
497 * The strings are considered equal iff they consist of the same
498 * characters on the minimum of their lengths and the length limit.
499 *
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.
506 *
507 */
508int str_lcmp(const char *s1, const char *s2, size_t max_len)
509{
510 wchar_t c1 = 0;
511 wchar_t c2 = 0;
512
513 size_t off1 = 0;
514 size_t off2 = 0;
515
516 size_t len = 0;
517
518 while (true) {
519 if (len >= max_len)
520 break;
521
522 c1 = str_decode(s1, &off1, STR_NO_LIMIT);
523 c2 = str_decode(s2, &off2, STR_NO_LIMIT);
524
525 if (c1 < c2)
526 return -1;
527
528 if (c1 > c2)
529 return 1;
530
531 if (c1 == 0 || c2 == 0)
532 break;
533
534 ++len;
535 }
536
537 return 0;
538
539}
540
541/** Copy string.
542 *
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.
547 *
548 * @param dest Destination buffer.
549 * @param count Size of the destination buffer (must be > 0).
550 * @param src Source string.
551 *
552 */
553void str_cpy(char *dest, size_t size, const char *src)
554{
555 /* There must be space for a null terminator in the buffer. */
556 ASSERT(size > 0);
557 ASSERT(src != NULL);
558
559 size_t src_off = 0;
560 size_t dest_off = 0;
561
562 wchar_t ch;
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 }
567
568 dest[dest_off] = '\0';
569}
570
571/** Copy size-limited substring.
572 *
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.
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 *
581 * @param dest Destination buffer.
582 * @param count Size of the destination buffer (must be > 0).
583 * @param src Source string.
584 * @param n Maximum number of bytes to read from @a src.
585 *
586 */
587void str_ncpy(char *dest, size_t size, const char *src, size_t n)
588{
589 /* There must be space for a null terminator in the buffer. */
590 ASSERT(size > 0);
591
592 size_t src_off = 0;
593 size_t dest_off = 0;
594
595 wchar_t ch;
596 while ((ch = str_decode(src, &src_off, n)) != 0) {
597 if (chr_encode(ch, dest, &dest_off, size - 1) != EOK)
598 break;
599 }
600
601 dest[dest_off] = '\0';
602}
603
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
663/** Convert wide string to string.
664 *
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.
668 *
669 * @param dest Destination buffer.
670 * @param size Size of the destination buffer.
671 * @param src Source wide string.
672 */
673void wstr_to_str(char *dest, size_t size, const wchar_t *src)
674{
675 wchar_t ch;
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;
684
685 while ((ch = src[src_idx++]) != 0) {
686 if (chr_encode(ch, dest, &dest_off, size - 1) != EOK)
687 break;
688 }
689
690 dest[dest_off] = '\0';
691}
692
693/** Find first occurence of character in string.
694 *
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.
699 *
700 */
701char *str_chr(const char *str, wchar_t ch)
702{
703 wchar_t acc;
704 size_t off = 0;
705 size_t last = 0;
706
707 while ((acc = str_decode(str, &off, STR_NO_LIMIT)) != 0) {
708 if (acc == ch)
709 return (char *) (str + last);
710 last = off;
711 }
712
713 return NULL;
714}
715
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 */
730bool wstr_linsert(wchar_t *str, wchar_t ch, size_t pos, size_t max_pos)
731{
732 size_t len = wstr_length(str);
733
734 if ((pos > len) || (pos + 1 > max_pos))
735 return false;
736
737 size_t i;
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 */
758bool wstr_remove(wchar_t *str, size_t pos)
759{
760 size_t len = wstr_length(str);
761
762 if (pos >= len)
763 return false;
764
765 size_t i;
766 for (i = pos + 1; i <= len; i++)
767 str[i - 1] = str[i];
768
769 return true;
770}
771
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;
834 default:
835 str--;
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.
899 * @param result Result of the conversion.
900 *
901 * @return EOK if conversion was successful.
902 *
903 */
904int str_uint64_t(const char *nptr, char **endptr, unsigned int base,
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
931void order_suffix(const uint64_t val, uint64_t *rv, char *suffix)
932{
933 if (val > UINT64_C(10000000000000000000)) {
934 *rv = val / UINT64_C(1000000000000000000);
935 *suffix = 'Z';
936 } else if (val > UINT64_C(1000000000000000000)) {
937 *rv = val / UINT64_C(1000000000000000);
938 *suffix = 'E';
939 } else if (val > UINT64_C(1000000000000000)) {
940 *rv = val / UINT64_C(1000000000000);
941 *suffix = 'T';
942 } else if (val > UINT64_C(1000000000000)) {
943 *rv = val / UINT64_C(1000000000);
944 *suffix = 'G';
945 } else if (val > UINT64_C(1000000000)) {
946 *rv = val / UINT64_C(1000000);
947 *suffix = 'M';
948 } else if (val > UINT64_C(1000000)) {
949 *rv = val / UINT64_C(1000);
950 *suffix = 'k';
951 } else {
952 *rv = val;
953 *suffix = ' ';
954 }
955}
956
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
984/** @}
985 */
Note: See TracBrowser for help on using the repository browser.