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

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

Fix block comment formatting (ccheck).

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