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