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