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

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

Let kernel code get printf via the standard stdio header. Clean up unused includes.

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