| 1 | /*
|
|---|
| 2 | * Copyright (c) 2001-2004 Jakub Jermar
|
|---|
| 3 | * All rights reserved.
|
|---|
| 4 | *
|
|---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 6 | * modification, are permitted provided that the following conditions
|
|---|
| 7 | * are met:
|
|---|
| 8 | *
|
|---|
| 9 | * - Redistributions of source code must retain the above copyright
|
|---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 13 | * documentation and/or other materials provided with the distribution.
|
|---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 15 | * derived from this software without specific prior written permission.
|
|---|
| 16 | *
|
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | /** @addtogroup generic
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 |
|
|---|
| 33 | /**
|
|---|
| 34 | * @file
|
|---|
| 35 | * @brief String functions.
|
|---|
| 36 | *
|
|---|
| 37 | * Strings and characters use the Universal Character Set (UCS). The standard
|
|---|
| 38 | * strings, called just strings are encoded in UTF-8. Wide strings (encoded
|
|---|
| 39 | * in UTF-32) are supported to a limited degree. A single character is
|
|---|
| 40 | * represented as wchar_t.@n
|
|---|
| 41 | *
|
|---|
| 42 | * Overview of the terminology:@n
|
|---|
| 43 | *
|
|---|
| 44 | * Term Meaning
|
|---|
| 45 | * -------------------- ----------------------------------------------------
|
|---|
| 46 | * byte 8 bits stored in uint8_t (unsigned 8 bit integer)
|
|---|
| 47 | *
|
|---|
| 48 | * character UTF-32 encoded Unicode character, stored in wchar_t
|
|---|
| 49 | * (signed 32 bit integer), code points 0 .. 1114111
|
|---|
| 50 | * are valid
|
|---|
| 51 | *
|
|---|
| 52 | * ASCII character 7 bit encoded ASCII character, stored in char
|
|---|
| 53 | * (usually signed 8 bit integer), code points 0 .. 127
|
|---|
| 54 | * are valid
|
|---|
| 55 | *
|
|---|
| 56 | * string UTF-8 encoded NULL-terminated Unicode string, char *
|
|---|
| 57 | *
|
|---|
| 58 | * wide string UTF-32 encoded NULL-terminated Unicode string,
|
|---|
| 59 | * wchar_t *
|
|---|
| 60 | *
|
|---|
| 61 | * [wide] string size number of BYTES in a [wide] string (excluding
|
|---|
| 62 | * the NULL-terminator), size_t
|
|---|
| 63 | *
|
|---|
| 64 | * [wide] string length number of CHARACTERS in a [wide] string (excluding
|
|---|
| 65 | * the NULL-terminator), size_t
|
|---|
| 66 | *
|
|---|
| 67 | * [wide] string width number of display cells on a monospace display taken
|
|---|
| 68 | * by a [wide] string, size_t
|
|---|
| 69 | *
|
|---|
| 70 | *
|
|---|
| 71 | * Overview of string metrics:@n
|
|---|
| 72 | *
|
|---|
| 73 | * Metric Abbrev. Type Meaning
|
|---|
| 74 | * ------ ------ ------ -------------------------------------------------
|
|---|
| 75 | * size n size_t number of BYTES in a string (excluding the
|
|---|
| 76 | * NULL-terminator)
|
|---|
| 77 | *
|
|---|
| 78 | * length l size_t number of CHARACTERS in a string (excluding the
|
|---|
| 79 | * null terminator)
|
|---|
| 80 | *
|
|---|
| 81 | * width w size_t number of display cells on a monospace display
|
|---|
| 82 | * taken by a string
|
|---|
| 83 | *
|
|---|
| 84 | *
|
|---|
| 85 | * Function naming prefixes:@n
|
|---|
| 86 | *
|
|---|
| 87 | * chr_ operate on characters
|
|---|
| 88 | * ascii_ operate on ASCII characters
|
|---|
| 89 | * str_ operate on strings
|
|---|
| 90 | * wstr_ operate on wide strings
|
|---|
| 91 | *
|
|---|
| 92 | * [w]str_[n|l|w] operate on a prefix limited by size, length
|
|---|
| 93 | * or width
|
|---|
| 94 | *
|
|---|
| 95 | *
|
|---|
| 96 | * A specific character inside a [wide] string can be referred to by:@n
|
|---|
| 97 | *
|
|---|
| 98 | * pointer (char *, wchar_t *)
|
|---|
| 99 | * byte offset (size_t)
|
|---|
| 100 | * character index (size_t)
|
|---|
| 101 | *
|
|---|
| 102 | */
|
|---|
| 103 |
|
|---|
| 104 | #include <str.h>
|
|---|
| 105 | #include <print.h>
|
|---|
| 106 | #include <cpu.h>
|
|---|
| 107 | #include <arch/asm.h>
|
|---|
| 108 | #include <arch.h>
|
|---|
| 109 | #include <errno.h>
|
|---|
| 110 | #include <align.h>
|
|---|
| 111 | #include <debug.h>
|
|---|
| 112 | #include <macros.h>
|
|---|
| 113 |
|
|---|
| 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 |
|
|---|
| 121 | /** Byte mask consisting of lowest @n bits (out of 8) */
|
|---|
| 122 | #define LO_MASK_8(n) ((uint8_t) ((1 << (n)) - 1))
|
|---|
| 123 |
|
|---|
| 124 | /** Byte mask consisting of lowest @n bits (out of 32) */
|
|---|
| 125 | #define LO_MASK_32(n) ((uint32_t) ((1 << (n)) - 1))
|
|---|
| 126 |
|
|---|
| 127 | /** Byte mask consisting of highest @n bits (out of 8) */
|
|---|
| 128 | #define HI_MASK_8(n) (~LO_MASK_8(8 - (n)))
|
|---|
| 129 |
|
|---|
| 130 | /** Number of data bits in a UTF-8 continuation byte */
|
|---|
| 131 | #define CONT_BITS 6
|
|---|
| 132 |
|
|---|
| 133 | /** Decode a single character from a string.
|
|---|
| 134 | *
|
|---|
| 135 | * Decode a single character from a string of size @a size. Decoding starts
|
|---|
| 136 | * at @a offset and this offset is moved to the beginning of the next
|
|---|
| 137 | * character. In case of decoding error, offset generally advances at least
|
|---|
| 138 | * by one. However, offset is never moved beyond size.
|
|---|
| 139 | *
|
|---|
| 140 | * @param str String (not necessarily NULL-terminated).
|
|---|
| 141 | * @param offset Byte offset in string where to start decoding.
|
|---|
| 142 | * @param size Size of the string (in bytes).
|
|---|
| 143 | *
|
|---|
| 144 | * @return Value of decoded character, U_SPECIAL on decoding error or
|
|---|
| 145 | * NULL if attempt to decode beyond @a size.
|
|---|
| 146 | *
|
|---|
| 147 | */
|
|---|
| 148 | wchar_t str_decode(const char *str, size_t *offset, size_t size)
|
|---|
| 149 | {
|
|---|
| 150 | if (*offset + 1 > size)
|
|---|
| 151 | return 0;
|
|---|
| 152 |
|
|---|
| 153 | /* First byte read from string */
|
|---|
| 154 | uint8_t b0 = (uint8_t) str[(*offset)++];
|
|---|
| 155 |
|
|---|
| 156 | /* Determine code length */
|
|---|
| 157 |
|
|---|
| 158 | unsigned int b0_bits; /* Data bits in first byte */
|
|---|
| 159 | unsigned int cbytes; /* Number of continuation bytes */
|
|---|
| 160 |
|
|---|
| 161 | if ((b0 & 0x80) == 0) {
|
|---|
| 162 | /* 0xxxxxxx (Plain ASCII) */
|
|---|
| 163 | b0_bits = 7;
|
|---|
| 164 | cbytes = 0;
|
|---|
| 165 | } else if ((b0 & 0xe0) == 0xc0) {
|
|---|
| 166 | /* 110xxxxx 10xxxxxx */
|
|---|
| 167 | b0_bits = 5;
|
|---|
| 168 | cbytes = 1;
|
|---|
| 169 | } else if ((b0 & 0xf0) == 0xe0) {
|
|---|
| 170 | /* 1110xxxx 10xxxxxx 10xxxxxx */
|
|---|
| 171 | b0_bits = 4;
|
|---|
| 172 | cbytes = 2;
|
|---|
| 173 | } else if ((b0 & 0xf8) == 0xf0) {
|
|---|
| 174 | /* 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */
|
|---|
| 175 | b0_bits = 3;
|
|---|
| 176 | cbytes = 3;
|
|---|
| 177 | } else {
|
|---|
| 178 | /* 10xxxxxx -- unexpected continuation byte */
|
|---|
| 179 | return U_SPECIAL;
|
|---|
| 180 | }
|
|---|
| 181 |
|
|---|
| 182 | if (*offset + cbytes > size)
|
|---|
| 183 | return U_SPECIAL;
|
|---|
| 184 |
|
|---|
| 185 | wchar_t ch = b0 & LO_MASK_8(b0_bits);
|
|---|
| 186 |
|
|---|
| 187 | /* Decode continuation bytes */
|
|---|
| 188 | while (cbytes > 0) {
|
|---|
| 189 | uint8_t b = (uint8_t) str[(*offset)++];
|
|---|
| 190 |
|
|---|
| 191 | /* Must be 10xxxxxx */
|
|---|
| 192 | if ((b & 0xc0) != 0x80)
|
|---|
| 193 | return U_SPECIAL;
|
|---|
| 194 |
|
|---|
| 195 | /* Shift data bits to ch */
|
|---|
| 196 | ch = (ch << CONT_BITS) | (wchar_t) (b & LO_MASK_8(CONT_BITS));
|
|---|
| 197 | cbytes--;
|
|---|
| 198 | }
|
|---|
| 199 |
|
|---|
| 200 | return ch;
|
|---|
| 201 | }
|
|---|
| 202 |
|
|---|
| 203 | /** Encode a single character to string representation.
|
|---|
| 204 | *
|
|---|
| 205 | * Encode a single character to string representation (i.e. UTF-8) and store
|
|---|
| 206 | * it into a buffer at @a offset. Encoding starts at @a offset and this offset
|
|---|
| 207 | * is moved to the position where the next character can be written to.
|
|---|
| 208 | *
|
|---|
| 209 | * @param ch Input character.
|
|---|
| 210 | * @param str Output buffer.
|
|---|
| 211 | * @param offset Byte offset where to start writing.
|
|---|
| 212 | * @param size Size of the output buffer (in bytes).
|
|---|
| 213 | *
|
|---|
| 214 | * @return EOK if the character was encoded successfully, EOVERFLOW if there
|
|---|
| 215 | * was not enough space in the output buffer or EINVAL if the character
|
|---|
| 216 | * code was invalid.
|
|---|
| 217 | */
|
|---|
| 218 | int chr_encode(const wchar_t ch, char *str, size_t *offset, size_t size)
|
|---|
| 219 | {
|
|---|
| 220 | if (*offset >= size)
|
|---|
| 221 | return EOVERFLOW;
|
|---|
| 222 |
|
|---|
| 223 | if (!chr_check(ch))
|
|---|
| 224 | return EINVAL;
|
|---|
| 225 |
|
|---|
| 226 | /* 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 |
|
|---|
| 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 {
|
|---|
| 248 | /* Codes longer than 21 bits are not supported */
|
|---|
| 249 | return EINVAL;
|
|---|
| 250 | }
|
|---|
| 251 |
|
|---|
| 252 | /* Check for available space in buffer */
|
|---|
| 253 | if (*offset + cbytes >= size)
|
|---|
| 254 | return EOVERFLOW;
|
|---|
| 255 |
|
|---|
| 256 | /* Encode continuation bytes */
|
|---|
| 257 | unsigned int i;
|
|---|
| 258 | for (i = cbytes; i > 0; i--) {
|
|---|
| 259 | str[*offset + i] = 0x80 | (cc & LO_MASK_32(CONT_BITS));
|
|---|
| 260 | cc = cc >> CONT_BITS;
|
|---|
| 261 | }
|
|---|
| 262 |
|
|---|
| 263 | /* Encode first byte */
|
|---|
| 264 | str[*offset] = (cc & LO_MASK_32(b0_bits)) | HI_MASK_8(8 - b0_bits - 1);
|
|---|
| 265 |
|
|---|
| 266 | /* Advance offset */
|
|---|
| 267 | *offset += cbytes + 1;
|
|---|
| 268 |
|
|---|
| 269 | return EOK;
|
|---|
| 270 | }
|
|---|
| 271 |
|
|---|
| 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
|
|---|
| 280 | *
|
|---|
| 281 | */
|
|---|
| 282 | size_t str_size(const char *str)
|
|---|
| 283 | {
|
|---|
| 284 | size_t size = 0;
|
|---|
| 285 |
|
|---|
| 286 | while (*str++ != 0)
|
|---|
| 287 | size++;
|
|---|
| 288 |
|
|---|
| 289 | return size;
|
|---|
| 290 | }
|
|---|
| 291 |
|
|---|
| 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.
|
|---|
| 308 | *
|
|---|
| 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
|
|---|
| 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.
|
|---|
| 316 | *
|
|---|
| 317 | * @return Number of bytes used by the characters.
|
|---|
| 318 | *
|
|---|
| 319 | */
|
|---|
| 320 | size_t str_lsize(const char *str, size_t max_len)
|
|---|
| 321 | {
|
|---|
| 322 | size_t len = 0;
|
|---|
| 323 | size_t offset = 0;
|
|---|
| 324 |
|
|---|
| 325 | while (len < max_len) {
|
|---|
| 326 | if (str_decode(str, &offset, STR_NO_LIMIT) == 0)
|
|---|
| 327 | break;
|
|---|
| 328 |
|
|---|
| 329 | len++;
|
|---|
| 330 | }
|
|---|
| 331 |
|
|---|
| 332 | return offset;
|
|---|
| 333 | }
|
|---|
| 334 |
|
|---|
| 335 | /** Get size of wide string with length limit.
|
|---|
| 336 | *
|
|---|
| 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.
|
|---|
| 344 | *
|
|---|
| 345 | * @return Number of bytes used by the wide characters.
|
|---|
| 346 | *
|
|---|
| 347 | */
|
|---|
| 348 | size_t wstr_lsize(const wchar_t *str, size_t max_len)
|
|---|
| 349 | {
|
|---|
| 350 | return (wstr_nlength(str, max_len * sizeof(wchar_t)) * sizeof(wchar_t));
|
|---|
| 351 | }
|
|---|
| 352 |
|
|---|
| 353 | /** Get number of characters in a string.
|
|---|
| 354 | *
|
|---|
| 355 | * @param str NULL-terminated string.
|
|---|
| 356 | *
|
|---|
| 357 | * @return Number of characters in string.
|
|---|
| 358 | *
|
|---|
| 359 | */
|
|---|
| 360 | size_t str_length(const char *str)
|
|---|
| 361 | {
|
|---|
| 362 | size_t len = 0;
|
|---|
| 363 | size_t offset = 0;
|
|---|
| 364 |
|
|---|
| 365 | while (str_decode(str, &offset, STR_NO_LIMIT) != 0)
|
|---|
| 366 | len++;
|
|---|
| 367 |
|
|---|
| 368 | return len;
|
|---|
| 369 | }
|
|---|
| 370 |
|
|---|
| 371 | /** Get number of characters in a wide string.
|
|---|
| 372 | *
|
|---|
| 373 | * @param str NULL-terminated wide string.
|
|---|
| 374 | *
|
|---|
| 375 | * @return Number of characters in @a str.
|
|---|
| 376 | *
|
|---|
| 377 | */
|
|---|
| 378 | size_t wstr_length(const wchar_t *wstr)
|
|---|
| 379 | {
|
|---|
| 380 | size_t len = 0;
|
|---|
| 381 |
|
|---|
| 382 | while (*wstr++ != 0)
|
|---|
| 383 | len++;
|
|---|
| 384 |
|
|---|
| 385 | return len;
|
|---|
| 386 | }
|
|---|
| 387 |
|
|---|
| 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.
|
|---|
| 394 | *
|
|---|
| 395 | */
|
|---|
| 396 | size_t str_nlength(const char *str, size_t size)
|
|---|
| 397 | {
|
|---|
| 398 | size_t len = 0;
|
|---|
| 399 | size_t offset = 0;
|
|---|
| 400 |
|
|---|
| 401 | while (str_decode(str, &offset, size) != 0)
|
|---|
| 402 | len++;
|
|---|
| 403 |
|
|---|
| 404 | return len;
|
|---|
| 405 | }
|
|---|
| 406 |
|
|---|
| 407 | /** Get number of characters in a string with size limit.
|
|---|
| 408 | *
|
|---|
| 409 | * @param str NULL-terminated string.
|
|---|
| 410 | * @param size Maximum number of bytes to consider.
|
|---|
| 411 | *
|
|---|
| 412 | * @return Number of characters in string.
|
|---|
| 413 | *
|
|---|
| 414 | */
|
|---|
| 415 | size_t wstr_nlength(const wchar_t *str, size_t size)
|
|---|
| 416 | {
|
|---|
| 417 | size_t len = 0;
|
|---|
| 418 | size_t limit = ALIGN_DOWN(size, sizeof(wchar_t));
|
|---|
| 419 | size_t offset = 0;
|
|---|
| 420 |
|
|---|
| 421 | while ((offset < limit) && (*str++ != 0)) {
|
|---|
| 422 | len++;
|
|---|
| 423 | offset += sizeof(wchar_t);
|
|---|
| 424 | }
|
|---|
| 425 |
|
|---|
| 426 | return len;
|
|---|
| 427 | }
|
|---|
| 428 |
|
|---|
| 429 | /** Check whether character is plain ASCII.
|
|---|
| 430 | *
|
|---|
| 431 | * @return True if character is plain ASCII.
|
|---|
| 432 | *
|
|---|
| 433 | */
|
|---|
| 434 | bool ascii_check(wchar_t ch)
|
|---|
| 435 | {
|
|---|
| 436 | if (WCHAR_SIGNED_CHECK(ch >= 0) && (ch <= 127))
|
|---|
| 437 | return true;
|
|---|
| 438 |
|
|---|
| 439 | return false;
|
|---|
| 440 | }
|
|---|
| 441 |
|
|---|
| 442 | /** Check whether character is valid
|
|---|
| 443 | *
|
|---|
| 444 | * @return True if character is a valid Unicode code point.
|
|---|
| 445 | *
|
|---|
| 446 | */
|
|---|
| 447 | bool chr_check(wchar_t ch)
|
|---|
| 448 | {
|
|---|
| 449 | if (WCHAR_SIGNED_CHECK(ch >= 0) && (ch <= 1114111))
|
|---|
| 450 | return true;
|
|---|
| 451 |
|
|---|
| 452 | return false;
|
|---|
| 453 | }
|
|---|
| 454 |
|
|---|
| 455 | /** Compare two NULL terminated strings.
|
|---|
| 456 | *
|
|---|
| 457 | * Do a char-by-char comparison of two NULL-terminated strings.
|
|---|
| 458 | * The strings are considered equal iff their length is equal
|
|---|
| 459 | * and both strings consist of the same sequence of characters.
|
|---|
| 460 | *
|
|---|
| 461 | * A string S1 is less than another string S2 if it has a character with
|
|---|
| 462 | * lower value at the first character position where the strings differ.
|
|---|
| 463 | * If the strings differ in length, the shorter one is treated as if
|
|---|
| 464 | * padded by characters with a value of zero.
|
|---|
| 465 | *
|
|---|
| 466 | * @param s1 First string to compare.
|
|---|
| 467 | * @param s2 Second string to compare.
|
|---|
| 468 | *
|
|---|
| 469 | * @return 0 if the strings are equal, -1 if the first is less than the second,
|
|---|
| 470 | * 1 if the second is less than the first.
|
|---|
| 471 | *
|
|---|
| 472 | */
|
|---|
| 473 | int str_cmp(const char *s1, const char *s2)
|
|---|
| 474 | {
|
|---|
| 475 | wchar_t c1 = 0;
|
|---|
| 476 | wchar_t c2 = 0;
|
|---|
| 477 |
|
|---|
| 478 | size_t off1 = 0;
|
|---|
| 479 | size_t off2 = 0;
|
|---|
| 480 |
|
|---|
| 481 | while (true) {
|
|---|
| 482 | c1 = str_decode(s1, &off1, STR_NO_LIMIT);
|
|---|
| 483 | c2 = str_decode(s2, &off2, STR_NO_LIMIT);
|
|---|
| 484 |
|
|---|
| 485 | if (c1 < c2)
|
|---|
| 486 | return -1;
|
|---|
| 487 |
|
|---|
| 488 | if (c1 > c2)
|
|---|
| 489 | return 1;
|
|---|
| 490 |
|
|---|
| 491 | if (c1 == 0 || c2 == 0)
|
|---|
| 492 | break;
|
|---|
| 493 | }
|
|---|
| 494 |
|
|---|
| 495 | return 0;
|
|---|
| 496 | }
|
|---|
| 497 |
|
|---|
| 498 | /** Compare two NULL terminated strings with length limit.
|
|---|
| 499 | *
|
|---|
| 500 | * Do a char-by-char comparison of two NULL-terminated strings.
|
|---|
| 501 | * The strings are considered equal iff
|
|---|
| 502 | * min(str_length(s1), max_len) == min(str_length(s2), max_len)
|
|---|
| 503 | * and both strings consist of the same sequence of characters,
|
|---|
| 504 | * up to max_len characters.
|
|---|
| 505 | *
|
|---|
| 506 | * A string S1 is less than another string S2 if it has a character with
|
|---|
| 507 | * lower value at the first character position where the strings differ.
|
|---|
| 508 | * If the strings differ in length, the shorter one is treated as if
|
|---|
| 509 | * padded by characters with a value of zero. Only the first max_len
|
|---|
| 510 | * characters are considered.
|
|---|
| 511 | *
|
|---|
| 512 | * @param s1 First string to compare.
|
|---|
| 513 | * @param s2 Second string to compare.
|
|---|
| 514 | * @param max_len Maximum number of characters to consider.
|
|---|
| 515 | *
|
|---|
| 516 | * @return 0 if the strings are equal, -1 if the first is less than the second,
|
|---|
| 517 | * 1 if the second is less than the first.
|
|---|
| 518 | *
|
|---|
| 519 | */
|
|---|
| 520 | int str_lcmp(const char *s1, const char *s2, size_t max_len)
|
|---|
| 521 | {
|
|---|
| 522 | wchar_t c1 = 0;
|
|---|
| 523 | wchar_t c2 = 0;
|
|---|
| 524 |
|
|---|
| 525 | size_t off1 = 0;
|
|---|
| 526 | size_t off2 = 0;
|
|---|
| 527 |
|
|---|
| 528 | size_t len = 0;
|
|---|
| 529 |
|
|---|
| 530 | while (true) {
|
|---|
| 531 | if (len >= max_len)
|
|---|
| 532 | break;
|
|---|
| 533 |
|
|---|
| 534 | c1 = str_decode(s1, &off1, STR_NO_LIMIT);
|
|---|
| 535 | c2 = str_decode(s2, &off2, STR_NO_LIMIT);
|
|---|
| 536 |
|
|---|
| 537 | if (c1 < c2)
|
|---|
| 538 | return -1;
|
|---|
| 539 |
|
|---|
| 540 | if (c1 > c2)
|
|---|
| 541 | return 1;
|
|---|
| 542 |
|
|---|
| 543 | if (c1 == 0 || c2 == 0)
|
|---|
| 544 | break;
|
|---|
| 545 |
|
|---|
| 546 | ++len;
|
|---|
| 547 | }
|
|---|
| 548 |
|
|---|
| 549 | return 0;
|
|---|
| 550 |
|
|---|
| 551 | }
|
|---|
| 552 |
|
|---|
| 553 | /** Copy string.
|
|---|
| 554 | *
|
|---|
| 555 | * Copy source string @a src to destination buffer @a dest.
|
|---|
| 556 | * No more than @a size bytes are written. If the size of the output buffer
|
|---|
| 557 | * is at least one byte, the output string will always be well-formed, i.e.
|
|---|
| 558 | * null-terminated and containing only complete characters.
|
|---|
| 559 | *
|
|---|
| 560 | * @param dest Destination buffer.
|
|---|
| 561 | * @param count Size of the destination buffer (must be > 0).
|
|---|
| 562 | * @param src Source string.
|
|---|
| 563 | *
|
|---|
| 564 | */
|
|---|
| 565 | void str_cpy(char *dest, size_t size, const char *src)
|
|---|
| 566 | {
|
|---|
| 567 | /* There must be space for a null terminator in the buffer. */
|
|---|
| 568 | ASSERT(size > 0);
|
|---|
| 569 |
|
|---|
| 570 | size_t src_off = 0;
|
|---|
| 571 | size_t dest_off = 0;
|
|---|
| 572 |
|
|---|
| 573 | wchar_t ch;
|
|---|
| 574 | while ((ch = str_decode(src, &src_off, STR_NO_LIMIT)) != 0) {
|
|---|
| 575 | if (chr_encode(ch, dest, &dest_off, size - 1) != EOK)
|
|---|
| 576 | break;
|
|---|
| 577 | }
|
|---|
| 578 |
|
|---|
| 579 | dest[dest_off] = '\0';
|
|---|
| 580 | }
|
|---|
| 581 |
|
|---|
| 582 | /** Copy size-limited substring.
|
|---|
| 583 | *
|
|---|
| 584 | * Copy prefix of string @a src of max. size @a size to destination buffer
|
|---|
| 585 | * @a dest. No more than @a size bytes are written. The output string will
|
|---|
| 586 | * always be well-formed, i.e. null-terminated and containing only complete
|
|---|
| 587 | * characters.
|
|---|
| 588 | *
|
|---|
| 589 | * No more than @a n bytes are read from the input string, so it does not
|
|---|
| 590 | * have to be null-terminated.
|
|---|
| 591 | *
|
|---|
| 592 | * @param dest Destination buffer.
|
|---|
| 593 | * @param count Size of the destination buffer (must be > 0).
|
|---|
| 594 | * @param src Source string.
|
|---|
| 595 | * @param n Maximum number of bytes to read from @a src.
|
|---|
| 596 | *
|
|---|
| 597 | */
|
|---|
| 598 | void str_ncpy(char *dest, size_t size, const char *src, size_t n)
|
|---|
| 599 | {
|
|---|
| 600 | /* There must be space for a null terminator in the buffer. */
|
|---|
| 601 | ASSERT(size > 0);
|
|---|
| 602 |
|
|---|
| 603 | size_t src_off = 0;
|
|---|
| 604 | size_t dest_off = 0;
|
|---|
| 605 |
|
|---|
| 606 | wchar_t ch;
|
|---|
| 607 | while ((ch = str_decode(src, &src_off, n)) != 0) {
|
|---|
| 608 | if (chr_encode(ch, dest, &dest_off, size - 1) != EOK)
|
|---|
| 609 | break;
|
|---|
| 610 | }
|
|---|
| 611 |
|
|---|
| 612 | dest[dest_off] = '\0';
|
|---|
| 613 | }
|
|---|
| 614 |
|
|---|
| 615 | /** Duplicate string.
|
|---|
| 616 | *
|
|---|
| 617 | * Allocate a new string and copy characters from the source
|
|---|
| 618 | * string into it. The duplicate string is allocated via sleeping
|
|---|
| 619 | * malloc(), thus this function can sleep in no memory conditions.
|
|---|
| 620 | *
|
|---|
| 621 | * The allocation cannot fail and the return value is always
|
|---|
| 622 | * a valid pointer. The duplicate string is always a well-formed
|
|---|
| 623 | * null-terminated UTF-8 string, but it can differ from the source
|
|---|
| 624 | * string on the byte level.
|
|---|
| 625 | *
|
|---|
| 626 | * @param src Source string.
|
|---|
| 627 | *
|
|---|
| 628 | * @return Duplicate string.
|
|---|
| 629 | *
|
|---|
| 630 | */
|
|---|
| 631 | char *str_dup(const char *src)
|
|---|
| 632 | {
|
|---|
| 633 | size_t size = str_size(src) + 1;
|
|---|
| 634 | char *dest = malloc(size, 0);
|
|---|
| 635 | ASSERT(dest);
|
|---|
| 636 |
|
|---|
| 637 | str_cpy(dest, size, src);
|
|---|
| 638 | return dest;
|
|---|
| 639 | }
|
|---|
| 640 |
|
|---|
| 641 | /** Duplicate string with size limit.
|
|---|
| 642 | *
|
|---|
| 643 | * Allocate a new string and copy up to @max_size bytes from the source
|
|---|
| 644 | * string into it. The duplicate string is allocated via sleeping
|
|---|
| 645 | * malloc(), thus this function can sleep in no memory conditions.
|
|---|
| 646 | * No more than @max_size + 1 bytes is allocated, but if the size
|
|---|
| 647 | * occupied by the source string is smaller than @max_size + 1,
|
|---|
| 648 | * less is allocated.
|
|---|
| 649 | *
|
|---|
| 650 | * The allocation cannot fail and the return value is always
|
|---|
| 651 | * a valid pointer. The duplicate string is always a well-formed
|
|---|
| 652 | * null-terminated UTF-8 string, but it can differ from the source
|
|---|
| 653 | * string on the byte level.
|
|---|
| 654 | *
|
|---|
| 655 | * @param src Source string.
|
|---|
| 656 | * @param n Maximum number of bytes to duplicate.
|
|---|
| 657 | *
|
|---|
| 658 | * @return Duplicate string.
|
|---|
| 659 | *
|
|---|
| 660 | */
|
|---|
| 661 | char *str_ndup(const char *src, size_t n)
|
|---|
| 662 | {
|
|---|
| 663 | size_t size = str_size(src);
|
|---|
| 664 | if (size > n)
|
|---|
| 665 | size = n;
|
|---|
| 666 |
|
|---|
| 667 | char *dest = malloc(size + 1, 0);
|
|---|
| 668 | ASSERT(dest);
|
|---|
| 669 |
|
|---|
| 670 | str_ncpy(dest, size + 1, src, size);
|
|---|
| 671 | return dest;
|
|---|
| 672 | }
|
|---|
| 673 |
|
|---|
| 674 | /** Convert wide string to string.
|
|---|
| 675 | *
|
|---|
| 676 | * Convert wide string @a src to string. The output is written to the buffer
|
|---|
| 677 | * specified by @a dest and @a size. @a size must be non-zero and the string
|
|---|
| 678 | * written will always be well-formed.
|
|---|
| 679 | *
|
|---|
| 680 | * @param dest Destination buffer.
|
|---|
| 681 | * @param size Size of the destination buffer.
|
|---|
| 682 | * @param src Source wide string.
|
|---|
| 683 | */
|
|---|
| 684 | void wstr_to_str(char *dest, size_t size, const wchar_t *src)
|
|---|
| 685 | {
|
|---|
| 686 | wchar_t ch;
|
|---|
| 687 | size_t src_idx;
|
|---|
| 688 | size_t dest_off;
|
|---|
| 689 |
|
|---|
| 690 | /* There must be space for a null terminator in the buffer. */
|
|---|
| 691 | ASSERT(size > 0);
|
|---|
| 692 |
|
|---|
| 693 | src_idx = 0;
|
|---|
| 694 | dest_off = 0;
|
|---|
| 695 |
|
|---|
| 696 | while ((ch = src[src_idx++]) != 0) {
|
|---|
| 697 | if (chr_encode(ch, dest, &dest_off, size - 1) != EOK)
|
|---|
| 698 | break;
|
|---|
| 699 | }
|
|---|
| 700 |
|
|---|
| 701 | dest[dest_off] = '\0';
|
|---|
| 702 | }
|
|---|
| 703 |
|
|---|
| 704 | /** Find first occurence of character in string.
|
|---|
| 705 | *
|
|---|
| 706 | * @param str String to search.
|
|---|
| 707 | * @param ch Character to look for.
|
|---|
| 708 | *
|
|---|
| 709 | * @return Pointer to character in @a str or NULL if not found.
|
|---|
| 710 | *
|
|---|
| 711 | */
|
|---|
| 712 | char *str_chr(const char *str, wchar_t ch)
|
|---|
| 713 | {
|
|---|
| 714 | wchar_t acc;
|
|---|
| 715 | size_t off = 0;
|
|---|
| 716 | size_t last = 0;
|
|---|
| 717 |
|
|---|
| 718 | while ((acc = str_decode(str, &off, STR_NO_LIMIT)) != 0) {
|
|---|
| 719 | if (acc == ch)
|
|---|
| 720 | return (char *) (str + last);
|
|---|
| 721 | last = off;
|
|---|
| 722 | }
|
|---|
| 723 |
|
|---|
| 724 | return NULL;
|
|---|
| 725 | }
|
|---|
| 726 |
|
|---|
| 727 | /** Insert a wide character into a wide string.
|
|---|
| 728 | *
|
|---|
| 729 | * Insert a wide character into a wide string at position
|
|---|
| 730 | * @a pos. The characters after the position are shifted.
|
|---|
| 731 | *
|
|---|
| 732 | * @param str String to insert to.
|
|---|
| 733 | * @param ch Character to insert to.
|
|---|
| 734 | * @param pos Character index where to insert.
|
|---|
| 735 | @ @param max_pos Characters in the buffer.
|
|---|
| 736 | *
|
|---|
| 737 | * @return True if the insertion was sucessful, false if the position
|
|---|
| 738 | * is out of bounds.
|
|---|
| 739 | *
|
|---|
| 740 | */
|
|---|
| 741 | bool wstr_linsert(wchar_t *str, wchar_t ch, size_t pos, size_t max_pos)
|
|---|
| 742 | {
|
|---|
| 743 | size_t len = wstr_length(str);
|
|---|
| 744 |
|
|---|
| 745 | if ((pos > len) || (pos + 1 > max_pos))
|
|---|
| 746 | return false;
|
|---|
| 747 |
|
|---|
| 748 | size_t i;
|
|---|
| 749 | for (i = len; i + 1 > pos; i--)
|
|---|
| 750 | str[i + 1] = str[i];
|
|---|
| 751 |
|
|---|
| 752 | str[pos] = ch;
|
|---|
| 753 |
|
|---|
| 754 | return true;
|
|---|
| 755 | }
|
|---|
| 756 |
|
|---|
| 757 | /** Remove a wide character from a wide string.
|
|---|
| 758 | *
|
|---|
| 759 | * Remove a wide character from a wide string at position
|
|---|
| 760 | * @a pos. The characters after the position are shifted.
|
|---|
| 761 | *
|
|---|
| 762 | * @param str String to remove from.
|
|---|
| 763 | * @param pos Character index to remove.
|
|---|
| 764 | *
|
|---|
| 765 | * @return True if the removal was sucessful, false if the position
|
|---|
| 766 | * is out of bounds.
|
|---|
| 767 | *
|
|---|
| 768 | */
|
|---|
| 769 | bool wstr_remove(wchar_t *str, size_t pos)
|
|---|
| 770 | {
|
|---|
| 771 | size_t len = wstr_length(str);
|
|---|
| 772 |
|
|---|
| 773 | if (pos >= len)
|
|---|
| 774 | return false;
|
|---|
| 775 |
|
|---|
| 776 | size_t i;
|
|---|
| 777 | for (i = pos + 1; i <= len; i++)
|
|---|
| 778 | str[i - 1] = str[i];
|
|---|
| 779 |
|
|---|
| 780 | return true;
|
|---|
| 781 | }
|
|---|
| 782 |
|
|---|
| 783 | /** Convert string to uint64_t (internal variant).
|
|---|
| 784 | *
|
|---|
| 785 | * @param nptr Pointer to string.
|
|---|
| 786 | * @param endptr Pointer to the first invalid character is stored here.
|
|---|
| 787 | * @param base Zero or number between 2 and 36 inclusive.
|
|---|
| 788 | * @param neg Indication of unary minus is stored here.
|
|---|
| 789 | * @apram result Result of the conversion.
|
|---|
| 790 | *
|
|---|
| 791 | * @return EOK if conversion was successful.
|
|---|
| 792 | *
|
|---|
| 793 | */
|
|---|
| 794 | static int str_uint(const char *nptr, char **endptr, unsigned int base,
|
|---|
| 795 | bool *neg, uint64_t *result)
|
|---|
| 796 | {
|
|---|
| 797 | ASSERT(endptr != NULL);
|
|---|
| 798 | ASSERT(neg != NULL);
|
|---|
| 799 | ASSERT(result != NULL);
|
|---|
| 800 |
|
|---|
| 801 | *neg = false;
|
|---|
| 802 | const char *str = nptr;
|
|---|
| 803 |
|
|---|
| 804 | /* Ignore leading whitespace */
|
|---|
| 805 | while (isspace(*str))
|
|---|
| 806 | str++;
|
|---|
| 807 |
|
|---|
| 808 | if (*str == '-') {
|
|---|
| 809 | *neg = true;
|
|---|
| 810 | str++;
|
|---|
| 811 | } else if (*str == '+')
|
|---|
| 812 | str++;
|
|---|
| 813 |
|
|---|
| 814 | if (base == 0) {
|
|---|
| 815 | /* Decode base if not specified */
|
|---|
| 816 | base = 10;
|
|---|
| 817 |
|
|---|
| 818 | if (*str == '0') {
|
|---|
| 819 | base = 8;
|
|---|
| 820 | str++;
|
|---|
| 821 |
|
|---|
| 822 | switch (*str) {
|
|---|
| 823 | case 'b':
|
|---|
| 824 | case 'B':
|
|---|
| 825 | base = 2;
|
|---|
| 826 | str++;
|
|---|
| 827 | break;
|
|---|
| 828 | case 'o':
|
|---|
| 829 | case 'O':
|
|---|
| 830 | base = 8;
|
|---|
| 831 | str++;
|
|---|
| 832 | break;
|
|---|
| 833 | case 'd':
|
|---|
| 834 | case 'D':
|
|---|
| 835 | case 't':
|
|---|
| 836 | case 'T':
|
|---|
| 837 | base = 10;
|
|---|
| 838 | str++;
|
|---|
| 839 | break;
|
|---|
| 840 | case 'x':
|
|---|
| 841 | case 'X':
|
|---|
| 842 | base = 16;
|
|---|
| 843 | str++;
|
|---|
| 844 | break;
|
|---|
| 845 | default:
|
|---|
| 846 | str--;
|
|---|
| 847 | }
|
|---|
| 848 | }
|
|---|
| 849 | } else {
|
|---|
| 850 | /* Check base range */
|
|---|
| 851 | if ((base < 2) || (base > 36)) {
|
|---|
| 852 | *endptr = (char *) str;
|
|---|
| 853 | return EINVAL;
|
|---|
| 854 | }
|
|---|
| 855 | }
|
|---|
| 856 |
|
|---|
| 857 | *result = 0;
|
|---|
| 858 | const char *startstr = str;
|
|---|
| 859 |
|
|---|
| 860 | while (*str != 0) {
|
|---|
| 861 | unsigned int digit;
|
|---|
| 862 |
|
|---|
| 863 | if ((*str >= 'a') && (*str <= 'z'))
|
|---|
| 864 | digit = *str - 'a' + 10;
|
|---|
| 865 | else if ((*str >= 'A') && (*str <= 'Z'))
|
|---|
| 866 | digit = *str - 'A' + 10;
|
|---|
| 867 | else if ((*str >= '0') && (*str <= '9'))
|
|---|
| 868 | digit = *str - '0';
|
|---|
| 869 | else
|
|---|
| 870 | break;
|
|---|
| 871 |
|
|---|
| 872 | if (digit >= base)
|
|---|
| 873 | break;
|
|---|
| 874 |
|
|---|
| 875 | uint64_t prev = *result;
|
|---|
| 876 | *result = (*result) * base + digit;
|
|---|
| 877 |
|
|---|
| 878 | if (*result < prev) {
|
|---|
| 879 | /* Overflow */
|
|---|
| 880 | *endptr = (char *) str;
|
|---|
| 881 | return EOVERFLOW;
|
|---|
| 882 | }
|
|---|
| 883 |
|
|---|
| 884 | str++;
|
|---|
| 885 | }
|
|---|
| 886 |
|
|---|
| 887 | if (str == startstr) {
|
|---|
| 888 | /*
|
|---|
| 889 | * No digits were decoded => first invalid character is
|
|---|
| 890 | * the first character of the string.
|
|---|
| 891 | */
|
|---|
| 892 | str = nptr;
|
|---|
| 893 | }
|
|---|
| 894 |
|
|---|
| 895 | *endptr = (char *) str;
|
|---|
| 896 |
|
|---|
| 897 | if (str == nptr)
|
|---|
| 898 | return EINVAL;
|
|---|
| 899 |
|
|---|
| 900 | return EOK;
|
|---|
| 901 | }
|
|---|
| 902 |
|
|---|
| 903 | /** Convert string to uint64_t.
|
|---|
| 904 | *
|
|---|
| 905 | * @param nptr Pointer to string.
|
|---|
| 906 | * @param endptr If not NULL, pointer to the first invalid character
|
|---|
| 907 | * is stored here.
|
|---|
| 908 | * @param base Zero or number between 2 and 36 inclusive.
|
|---|
| 909 | * @param strict Do not allow any trailing characters.
|
|---|
| 910 | * @param result Result of the conversion.
|
|---|
| 911 | *
|
|---|
| 912 | * @return EOK if conversion was successful.
|
|---|
| 913 | *
|
|---|
| 914 | */
|
|---|
| 915 | int str_uint64_t(const char *nptr, char **endptr, unsigned int base,
|
|---|
| 916 | bool strict, uint64_t *result)
|
|---|
| 917 | {
|
|---|
| 918 | ASSERT(result != NULL);
|
|---|
| 919 |
|
|---|
| 920 | bool neg;
|
|---|
| 921 | char *lendptr;
|
|---|
| 922 | int ret = str_uint(nptr, &lendptr, base, &neg, result);
|
|---|
| 923 |
|
|---|
| 924 | if (endptr != NULL)
|
|---|
| 925 | *endptr = (char *) lendptr;
|
|---|
| 926 |
|
|---|
| 927 | if (ret != EOK)
|
|---|
| 928 | return ret;
|
|---|
| 929 |
|
|---|
| 930 | /* Do not allow negative values */
|
|---|
| 931 | if (neg)
|
|---|
| 932 | return EINVAL;
|
|---|
| 933 |
|
|---|
| 934 | /* Check whether we are at the end of
|
|---|
| 935 | the string in strict mode */
|
|---|
| 936 | if ((strict) && (*lendptr != 0))
|
|---|
| 937 | return EINVAL;
|
|---|
| 938 |
|
|---|
| 939 | return EOK;
|
|---|
| 940 | }
|
|---|
| 941 |
|
|---|
| 942 | void order_suffix(const uint64_t val, uint64_t *rv, char *suffix)
|
|---|
| 943 | {
|
|---|
| 944 | if (val > UINT64_C(10000000000000000000)) {
|
|---|
| 945 | *rv = val / UINT64_C(1000000000000000000);
|
|---|
| 946 | *suffix = 'Z';
|
|---|
| 947 | } else if (val > UINT64_C(1000000000000000000)) {
|
|---|
| 948 | *rv = val / UINT64_C(1000000000000000);
|
|---|
| 949 | *suffix = 'E';
|
|---|
| 950 | } else if (val > UINT64_C(1000000000000000)) {
|
|---|
| 951 | *rv = val / UINT64_C(1000000000000);
|
|---|
| 952 | *suffix = 'T';
|
|---|
| 953 | } else if (val > UINT64_C(1000000000000)) {
|
|---|
| 954 | *rv = val / UINT64_C(1000000000);
|
|---|
| 955 | *suffix = 'G';
|
|---|
| 956 | } else if (val > UINT64_C(1000000000)) {
|
|---|
| 957 | *rv = val / UINT64_C(1000000);
|
|---|
| 958 | *suffix = 'M';
|
|---|
| 959 | } else if (val > UINT64_C(1000000)) {
|
|---|
| 960 | *rv = val / UINT64_C(1000);
|
|---|
| 961 | *suffix = 'k';
|
|---|
| 962 | } else {
|
|---|
| 963 | *rv = val;
|
|---|
| 964 | *suffix = ' ';
|
|---|
| 965 | }
|
|---|
| 966 | }
|
|---|
| 967 |
|
|---|
| 968 | void bin_order_suffix(const uint64_t val, uint64_t *rv, const char **suffix,
|
|---|
| 969 | bool fixed)
|
|---|
| 970 | {
|
|---|
| 971 | if (val > UINT64_C(1152921504606846976)) {
|
|---|
| 972 | *rv = val / UINT64_C(1125899906842624);
|
|---|
| 973 | *suffix = "EiB";
|
|---|
| 974 | } else if (val > UINT64_C(1125899906842624)) {
|
|---|
| 975 | *rv = val / UINT64_C(1099511627776);
|
|---|
| 976 | *suffix = "TiB";
|
|---|
| 977 | } else if (val > UINT64_C(1099511627776)) {
|
|---|
| 978 | *rv = val / UINT64_C(1073741824);
|
|---|
| 979 | *suffix = "GiB";
|
|---|
| 980 | } else if (val > UINT64_C(1073741824)) {
|
|---|
| 981 | *rv = val / UINT64_C(1048576);
|
|---|
| 982 | *suffix = "MiB";
|
|---|
| 983 | } else if (val > UINT64_C(1048576)) {
|
|---|
| 984 | *rv = val / UINT64_C(1024);
|
|---|
| 985 | *suffix = "KiB";
|
|---|
| 986 | } else {
|
|---|
| 987 | *rv = val;
|
|---|
| 988 | if (fixed)
|
|---|
| 989 | *suffix = "B ";
|
|---|
| 990 | else
|
|---|
| 991 | *suffix = "B";
|
|---|
| 992 | }
|
|---|
| 993 | }
|
|---|
| 994 |
|
|---|
| 995 | /** @}
|
|---|
| 996 | */
|
|---|