[936351c1] | 1 | /*
|
---|
[d066259] | 2 | * Copyright (c) 2001-2004 Jakub Jermar
|
---|
[df4ed85] | 3 | * Copyright (c) 2005 Martin Decky
|
---|
[576845ec] | 4 | * Copyright (c) 2008 Jiri Svoboda
|
---|
[22cf42d9] | 5 | * Copyright (c) 2011 Martin Sucha
|
---|
[c4bbca8] | 6 | * Copyright (c) 2011 Oleg Romanenko
|
---|
[936351c1] | 7 | * All rights reserved.
|
---|
| 8 | *
|
---|
| 9 | * Redistribution and use in source and binary forms, with or without
|
---|
| 10 | * modification, are permitted provided that the following conditions
|
---|
| 11 | * are met:
|
---|
| 12 | *
|
---|
| 13 | * - Redistributions of source code must retain the above copyright
|
---|
| 14 | * notice, this list of conditions and the following disclaimer.
|
---|
| 15 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 16 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 17 | * documentation and/or other materials provided with the distribution.
|
---|
| 18 | * - The name of the author may not be used to endorse or promote products
|
---|
| 19 | * derived from this software without specific prior written permission.
|
---|
| 20 | *
|
---|
| 21 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 22 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 23 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 24 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 25 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 26 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 30 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 31 | */
|
---|
| 32 |
|
---|
[a46da63] | 33 | /** @addtogroup libc
|
---|
[b2951e2] | 34 | * @{
|
---|
| 35 | */
|
---|
[d066259] | 36 |
|
---|
| 37 | /**
|
---|
| 38 | * @file
|
---|
| 39 | * @brief String functions.
|
---|
| 40 | *
|
---|
| 41 | * Strings and characters use the Universal Character Set (UCS). The standard
|
---|
| 42 | * strings, called just strings are encoded in UTF-8. Wide strings (encoded
|
---|
| 43 | * in UTF-32) are supported to a limited degree. A single character is
|
---|
| 44 | * represented as wchar_t.@n
|
---|
| 45 | *
|
---|
| 46 | * Overview of the terminology:@n
|
---|
| 47 | *
|
---|
| 48 | * Term Meaning
|
---|
| 49 | * -------------------- ----------------------------------------------------
|
---|
| 50 | * byte 8 bits stored in uint8_t (unsigned 8 bit integer)
|
---|
| 51 | *
|
---|
| 52 | * character UTF-32 encoded Unicode character, stored in wchar_t
|
---|
| 53 | * (signed 32 bit integer), code points 0 .. 1114111
|
---|
| 54 | * are valid
|
---|
| 55 | *
|
---|
| 56 | * ASCII character 7 bit encoded ASCII character, stored in char
|
---|
| 57 | * (usually signed 8 bit integer), code points 0 .. 127
|
---|
| 58 | * are valid
|
---|
| 59 | *
|
---|
| 60 | * string UTF-8 encoded NULL-terminated Unicode string, char *
|
---|
| 61 | *
|
---|
| 62 | * wide string UTF-32 encoded NULL-terminated Unicode string,
|
---|
| 63 | * wchar_t *
|
---|
| 64 | *
|
---|
| 65 | * [wide] string size number of BYTES in a [wide] string (excluding
|
---|
| 66 | * the NULL-terminator), size_t
|
---|
| 67 | *
|
---|
| 68 | * [wide] string length number of CHARACTERS in a [wide] string (excluding
|
---|
| 69 | * the NULL-terminator), size_t
|
---|
| 70 | *
|
---|
| 71 | * [wide] string width number of display cells on a monospace display taken
|
---|
| 72 | * by a [wide] string, size_t
|
---|
| 73 | *
|
---|
| 74 | *
|
---|
| 75 | * Overview of string metrics:@n
|
---|
| 76 | *
|
---|
| 77 | * Metric Abbrev. Type Meaning
|
---|
| 78 | * ------ ------ ------ -------------------------------------------------
|
---|
| 79 | * size n size_t number of BYTES in a string (excluding the
|
---|
| 80 | * NULL-terminator)
|
---|
| 81 | *
|
---|
| 82 | * length l size_t number of CHARACTERS in a string (excluding the
|
---|
| 83 | * null terminator)
|
---|
| 84 | *
|
---|
| 85 | * width w size_t number of display cells on a monospace display
|
---|
| 86 | * taken by a string
|
---|
| 87 | *
|
---|
| 88 | *
|
---|
| 89 | * Function naming prefixes:@n
|
---|
| 90 | *
|
---|
| 91 | * chr_ operate on characters
|
---|
| 92 | * ascii_ operate on ASCII characters
|
---|
| 93 | * str_ operate on strings
|
---|
| 94 | * wstr_ operate on wide strings
|
---|
| 95 | *
|
---|
| 96 | * [w]str_[n|l|w] operate on a prefix limited by size, length
|
---|
| 97 | * or width
|
---|
| 98 | *
|
---|
| 99 | *
|
---|
| 100 | * A specific character inside a [wide] string can be referred to by:@n
|
---|
| 101 | *
|
---|
| 102 | * pointer (char *, wchar_t *)
|
---|
| 103 | * byte offset (size_t)
|
---|
| 104 | * character index (size_t)
|
---|
| 105 | *
|
---|
[b2951e2] | 106 | */
|
---|
| 107 |
|
---|
[19f857a] | 108 | #include <str.h>
|
---|
[d066259] | 109 |
|
---|
[38d150e] | 110 | #include <assert.h>
|
---|
[e64c4b2] | 111 | #include <ctype.h>
|
---|
[171f9a1] | 112 | #include <errno.h>
|
---|
[d066259] | 113 | #include <stdbool.h>
|
---|
| 114 | #include <stddef.h>
|
---|
| 115 | #include <stdint.h>
|
---|
| 116 | #include <stdlib.h>
|
---|
| 117 |
|
---|
[f2b8cdc] | 118 | #include <align.h>
|
---|
[095003a8] | 119 | #include <mem.h>
|
---|
[171f9a1] | 120 |
|
---|
[8e893ae] | 121 | /** Check the condition if wchar_t is signed */
|
---|
[002fd5f] | 122 | #ifdef __WCHAR_UNSIGNED__
|
---|
[1433ecda] | 123 | #define WCHAR_SIGNED_CHECK(cond) (true)
|
---|
[8e893ae] | 124 | #else
|
---|
[1433ecda] | 125 | #define WCHAR_SIGNED_CHECK(cond) (cond)
|
---|
[8e893ae] | 126 | #endif
|
---|
| 127 |
|
---|
[171f9a1] | 128 | /** Byte mask consisting of lowest @n bits (out of 8) */
|
---|
| 129 | #define LO_MASK_8(n) ((uint8_t) ((1 << (n)) - 1))
|
---|
| 130 |
|
---|
| 131 | /** Byte mask consisting of lowest @n bits (out of 32) */
|
---|
| 132 | #define LO_MASK_32(n) ((uint32_t) ((1 << (n)) - 1))
|
---|
| 133 |
|
---|
| 134 | /** Byte mask consisting of highest @n bits (out of 8) */
|
---|
| 135 | #define HI_MASK_8(n) (~LO_MASK_8(8 - (n)))
|
---|
| 136 |
|
---|
| 137 | /** Number of data bits in a UTF-8 continuation byte */
|
---|
| 138 | #define CONT_BITS 6
|
---|
| 139 |
|
---|
| 140 | /** Decode a single character from a string.
|
---|
| 141 | *
|
---|
| 142 | * Decode a single character from a string of size @a size. Decoding starts
|
---|
| 143 | * at @a offset and this offset is moved to the beginning of the next
|
---|
| 144 | * character. In case of decoding error, offset generally advances at least
|
---|
| 145 | * by one. However, offset is never moved beyond size.
|
---|
| 146 | *
|
---|
| 147 | * @param str String (not necessarily NULL-terminated).
|
---|
| 148 | * @param offset Byte offset in string where to start decoding.
|
---|
| 149 | * @param size Size of the string (in bytes).
|
---|
| 150 | *
|
---|
| 151 | * @return Value of decoded character, U_SPECIAL on decoding error or
|
---|
| 152 | * NULL if attempt to decode beyond @a size.
|
---|
| 153 | *
|
---|
| 154 | */
|
---|
| 155 | wchar_t str_decode(const char *str, size_t *offset, size_t size)
|
---|
| 156 | {
|
---|
| 157 | if (*offset + 1 > size)
|
---|
| 158 | return 0;
|
---|
[a35b458] | 159 |
|
---|
[171f9a1] | 160 | /* First byte read from string */
|
---|
| 161 | uint8_t b0 = (uint8_t) str[(*offset)++];
|
---|
[a35b458] | 162 |
|
---|
[171f9a1] | 163 | /* Determine code length */
|
---|
[a35b458] | 164 |
|
---|
[171f9a1] | 165 | unsigned int b0_bits; /* Data bits in first byte */
|
---|
| 166 | unsigned int cbytes; /* Number of continuation bytes */
|
---|
[a35b458] | 167 |
|
---|
[171f9a1] | 168 | if ((b0 & 0x80) == 0) {
|
---|
| 169 | /* 0xxxxxxx (Plain ASCII) */
|
---|
| 170 | b0_bits = 7;
|
---|
| 171 | cbytes = 0;
|
---|
| 172 | } else if ((b0 & 0xe0) == 0xc0) {
|
---|
| 173 | /* 110xxxxx 10xxxxxx */
|
---|
| 174 | b0_bits = 5;
|
---|
| 175 | cbytes = 1;
|
---|
| 176 | } else if ((b0 & 0xf0) == 0xe0) {
|
---|
| 177 | /* 1110xxxx 10xxxxxx 10xxxxxx */
|
---|
| 178 | b0_bits = 4;
|
---|
| 179 | cbytes = 2;
|
---|
| 180 | } else if ((b0 & 0xf8) == 0xf0) {
|
---|
| 181 | /* 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */
|
---|
| 182 | b0_bits = 3;
|
---|
| 183 | cbytes = 3;
|
---|
| 184 | } else {
|
---|
| 185 | /* 10xxxxxx -- unexpected continuation byte */
|
---|
| 186 | return U_SPECIAL;
|
---|
| 187 | }
|
---|
[a35b458] | 188 |
|
---|
[171f9a1] | 189 | if (*offset + cbytes > size)
|
---|
| 190 | return U_SPECIAL;
|
---|
[a35b458] | 191 |
|
---|
[171f9a1] | 192 | wchar_t ch = b0 & LO_MASK_8(b0_bits);
|
---|
[a35b458] | 193 |
|
---|
[171f9a1] | 194 | /* Decode continuation bytes */
|
---|
| 195 | while (cbytes > 0) {
|
---|
| 196 | uint8_t b = (uint8_t) str[(*offset)++];
|
---|
[a35b458] | 197 |
|
---|
[171f9a1] | 198 | /* Must be 10xxxxxx */
|
---|
| 199 | if ((b & 0xc0) != 0x80)
|
---|
| 200 | return U_SPECIAL;
|
---|
[a35b458] | 201 |
|
---|
[171f9a1] | 202 | /* Shift data bits to ch */
|
---|
| 203 | ch = (ch << CONT_BITS) | (wchar_t) (b & LO_MASK_8(CONT_BITS));
|
---|
| 204 | cbytes--;
|
---|
| 205 | }
|
---|
[a35b458] | 206 |
|
---|
[171f9a1] | 207 | return ch;
|
---|
| 208 | }
|
---|
| 209 |
|
---|
[568693b] | 210 | /** Decode a single character from a string to the left.
|
---|
| 211 | *
|
---|
| 212 | * Decode a single character from a string of size @a size. Decoding starts
|
---|
| 213 | * at @a offset and this offset is moved to the beginning of the previous
|
---|
| 214 | * character. In case of decoding error, offset generally decreases at least
|
---|
| 215 | * by one. However, offset is never moved before 0.
|
---|
| 216 | *
|
---|
| 217 | * @param str String (not necessarily NULL-terminated).
|
---|
| 218 | * @param offset Byte offset in string where to start decoding.
|
---|
| 219 | * @param size Size of the string (in bytes).
|
---|
| 220 | *
|
---|
| 221 | * @return Value of decoded character, U_SPECIAL on decoding error or
|
---|
| 222 | * NULL if attempt to decode beyond @a start of str.
|
---|
| 223 | *
|
---|
| 224 | */
|
---|
| 225 | wchar_t str_decode_reverse(const char *str, size_t *offset, size_t size)
|
---|
| 226 | {
|
---|
| 227 | if (*offset == 0)
|
---|
| 228 | return 0;
|
---|
[a35b458] | 229 |
|
---|
[568693b] | 230 | size_t processed = 0;
|
---|
| 231 | /* Continue while continuation bytes found */
|
---|
| 232 | while (*offset > 0 && processed < 4) {
|
---|
| 233 | uint8_t b = (uint8_t) str[--(*offset)];
|
---|
[a35b458] | 234 |
|
---|
[568693b] | 235 | if (processed == 0 && (b & 0x80) == 0) {
|
---|
| 236 | /* 0xxxxxxx (Plain ASCII) */
|
---|
| 237 | return b & 0x7f;
|
---|
[1433ecda] | 238 | } else if ((b & 0xe0) == 0xc0 || (b & 0xf0) == 0xe0 ||
|
---|
[568693b] | 239 | (b & 0xf8) == 0xf0) {
|
---|
| 240 | /* Start byte */
|
---|
| 241 | size_t start_offset = *offset;
|
---|
| 242 | return str_decode(str, &start_offset, size);
|
---|
[1433ecda] | 243 | } else if ((b & 0xc0) != 0x80) {
|
---|
[568693b] | 244 | /* Not a continuation byte */
|
---|
| 245 | return U_SPECIAL;
|
---|
| 246 | }
|
---|
| 247 | processed++;
|
---|
| 248 | }
|
---|
| 249 | /* Too many continuation bytes */
|
---|
| 250 | return U_SPECIAL;
|
---|
| 251 | }
|
---|
| 252 |
|
---|
[171f9a1] | 253 | /** Encode a single character to string representation.
|
---|
| 254 | *
|
---|
| 255 | * Encode a single character to string representation (i.e. UTF-8) and store
|
---|
| 256 | * it into a buffer at @a offset. Encoding starts at @a offset and this offset
|
---|
| 257 | * is moved to the position where the next character can be written to.
|
---|
| 258 | *
|
---|
| 259 | * @param ch Input character.
|
---|
| 260 | * @param str Output buffer.
|
---|
| 261 | * @param offset Byte offset where to start writing.
|
---|
| 262 | * @param size Size of the output buffer (in bytes).
|
---|
| 263 | *
|
---|
| 264 | * @return EOK if the character was encoded successfully, EOVERFLOW if there
|
---|
[d4a3ee5] | 265 | * was not enough space in the output buffer or EINVAL if the character
|
---|
| 266 | * code was invalid.
|
---|
[171f9a1] | 267 | */
|
---|
[b7fd2a0] | 268 | errno_t chr_encode(const wchar_t ch, char *str, size_t *offset, size_t size)
|
---|
[171f9a1] | 269 | {
|
---|
| 270 | if (*offset >= size)
|
---|
| 271 | return EOVERFLOW;
|
---|
[a35b458] | 272 |
|
---|
[171f9a1] | 273 | if (!chr_check(ch))
|
---|
| 274 | return EINVAL;
|
---|
[a35b458] | 275 |
|
---|
[7c3fb9b] | 276 | /*
|
---|
| 277 | * Unsigned version of ch (bit operations should only be done
|
---|
| 278 | * on unsigned types).
|
---|
| 279 | */
|
---|
[171f9a1] | 280 | uint32_t cc = (uint32_t) ch;
|
---|
[a35b458] | 281 |
|
---|
[171f9a1] | 282 | /* Determine how many continuation bytes are needed */
|
---|
[a35b458] | 283 |
|
---|
[171f9a1] | 284 | unsigned int b0_bits; /* Data bits in first byte */
|
---|
| 285 | unsigned int cbytes; /* Number of continuation bytes */
|
---|
[a35b458] | 286 |
|
---|
[171f9a1] | 287 | if ((cc & ~LO_MASK_32(7)) == 0) {
|
---|
| 288 | b0_bits = 7;
|
---|
| 289 | cbytes = 0;
|
---|
| 290 | } else if ((cc & ~LO_MASK_32(11)) == 0) {
|
---|
| 291 | b0_bits = 5;
|
---|
| 292 | cbytes = 1;
|
---|
| 293 | } else if ((cc & ~LO_MASK_32(16)) == 0) {
|
---|
| 294 | b0_bits = 4;
|
---|
| 295 | cbytes = 2;
|
---|
| 296 | } else if ((cc & ~LO_MASK_32(21)) == 0) {
|
---|
| 297 | b0_bits = 3;
|
---|
| 298 | cbytes = 3;
|
---|
| 299 | } else {
|
---|
| 300 | /* Codes longer than 21 bits are not supported */
|
---|
| 301 | return EINVAL;
|
---|
| 302 | }
|
---|
[a35b458] | 303 |
|
---|
[171f9a1] | 304 | /* Check for available space in buffer */
|
---|
| 305 | if (*offset + cbytes >= size)
|
---|
| 306 | return EOVERFLOW;
|
---|
[a35b458] | 307 |
|
---|
[171f9a1] | 308 | /* Encode continuation bytes */
|
---|
| 309 | unsigned int i;
|
---|
| 310 | for (i = cbytes; i > 0; i--) {
|
---|
| 311 | str[*offset + i] = 0x80 | (cc & LO_MASK_32(CONT_BITS));
|
---|
| 312 | cc = cc >> CONT_BITS;
|
---|
| 313 | }
|
---|
[a35b458] | 314 |
|
---|
[171f9a1] | 315 | /* Encode first byte */
|
---|
| 316 | str[*offset] = (cc & LO_MASK_32(b0_bits)) | HI_MASK_8(8 - b0_bits - 1);
|
---|
[a35b458] | 317 |
|
---|
[171f9a1] | 318 | /* Advance offset */
|
---|
| 319 | *offset += cbytes + 1;
|
---|
[a35b458] | 320 |
|
---|
[171f9a1] | 321 | return EOK;
|
---|
| 322 | }
|
---|
| 323 |
|
---|
[f2b8cdc] | 324 | /** Get size of string.
|
---|
| 325 | *
|
---|
| 326 | * Get the number of bytes which are used by the string @a str (excluding the
|
---|
| 327 | * NULL-terminator).
|
---|
| 328 | *
|
---|
| 329 | * @param str String to consider.
|
---|
| 330 | *
|
---|
| 331 | * @return Number of bytes used by the string
|
---|
| 332 | *
|
---|
| 333 | */
|
---|
| 334 | size_t str_size(const char *str)
|
---|
| 335 | {
|
---|
| 336 | size_t size = 0;
|
---|
[a35b458] | 337 |
|
---|
[f2b8cdc] | 338 | while (*str++ != 0)
|
---|
| 339 | size++;
|
---|
[a35b458] | 340 |
|
---|
[f2b8cdc] | 341 | return size;
|
---|
| 342 | }
|
---|
| 343 |
|
---|
| 344 | /** Get size of wide string.
|
---|
| 345 | *
|
---|
| 346 | * Get the number of bytes which are used by the wide string @a str (excluding the
|
---|
| 347 | * NULL-terminator).
|
---|
| 348 | *
|
---|
| 349 | * @param str Wide string to consider.
|
---|
| 350 | *
|
---|
| 351 | * @return Number of bytes used by the wide string
|
---|
| 352 | *
|
---|
| 353 | */
|
---|
| 354 | size_t wstr_size(const wchar_t *str)
|
---|
| 355 | {
|
---|
| 356 | return (wstr_length(str) * sizeof(wchar_t));
|
---|
| 357 | }
|
---|
| 358 |
|
---|
| 359 | /** Get size of string with length limit.
|
---|
| 360 | *
|
---|
| 361 | * Get the number of bytes which are used by up to @a max_len first
|
---|
| 362 | * characters in the string @a str. If @a max_len is greater than
|
---|
| 363 | * the length of @a str, the entire string is measured (excluding the
|
---|
| 364 | * NULL-terminator).
|
---|
| 365 | *
|
---|
| 366 | * @param str String to consider.
|
---|
| 367 | * @param max_len Maximum number of characters to measure.
|
---|
| 368 | *
|
---|
| 369 | * @return Number of bytes used by the characters.
|
---|
| 370 | *
|
---|
| 371 | */
|
---|
[d4a3ee5] | 372 | size_t str_lsize(const char *str, size_t max_len)
|
---|
[f2b8cdc] | 373 | {
|
---|
[d4a3ee5] | 374 | size_t len = 0;
|
---|
[f2b8cdc] | 375 | size_t offset = 0;
|
---|
[a35b458] | 376 |
|
---|
[f2b8cdc] | 377 | while (len < max_len) {
|
---|
| 378 | if (str_decode(str, &offset, STR_NO_LIMIT) == 0)
|
---|
| 379 | break;
|
---|
[a35b458] | 380 |
|
---|
[f2b8cdc] | 381 | len++;
|
---|
| 382 | }
|
---|
[a35b458] | 383 |
|
---|
[f2b8cdc] | 384 | return offset;
|
---|
| 385 | }
|
---|
| 386 |
|
---|
[560d79f] | 387 | /** Get size of string with size limit.
|
---|
| 388 | *
|
---|
| 389 | * Get the number of bytes which are used by the string @a str
|
---|
| 390 | * (excluding the NULL-terminator), but no more than @max_size bytes.
|
---|
| 391 | *
|
---|
| 392 | * @param str String to consider.
|
---|
| 393 | * @param max_size Maximum number of bytes to measure.
|
---|
| 394 | *
|
---|
| 395 | * @return Number of bytes used by the string
|
---|
| 396 | *
|
---|
| 397 | */
|
---|
| 398 | size_t str_nsize(const char *str, size_t max_size)
|
---|
| 399 | {
|
---|
| 400 | size_t size = 0;
|
---|
[a35b458] | 401 |
|
---|
[560d79f] | 402 | while ((*str++ != 0) && (size < max_size))
|
---|
| 403 | size++;
|
---|
[a35b458] | 404 |
|
---|
[560d79f] | 405 | return size;
|
---|
| 406 | }
|
---|
| 407 |
|
---|
| 408 | /** Get size of wide string with size limit.
|
---|
| 409 | *
|
---|
| 410 | * Get the number of bytes which are used by the wide string @a str
|
---|
| 411 | * (excluding the NULL-terminator), but no more than @max_size bytes.
|
---|
| 412 | *
|
---|
| 413 | * @param str Wide string to consider.
|
---|
| 414 | * @param max_size Maximum number of bytes to measure.
|
---|
| 415 | *
|
---|
| 416 | * @return Number of bytes used by the wide string
|
---|
| 417 | *
|
---|
| 418 | */
|
---|
| 419 | size_t wstr_nsize(const wchar_t *str, size_t max_size)
|
---|
| 420 | {
|
---|
| 421 | return (wstr_nlength(str, max_size) * sizeof(wchar_t));
|
---|
| 422 | }
|
---|
| 423 |
|
---|
[f2b8cdc] | 424 | /** Get size of wide string with length limit.
|
---|
| 425 | *
|
---|
| 426 | * Get the number of bytes which are used by up to @a max_len first
|
---|
| 427 | * wide characters in the wide string @a str. If @a max_len is greater than
|
---|
| 428 | * the length of @a str, the entire wide string is measured (excluding the
|
---|
| 429 | * NULL-terminator).
|
---|
| 430 | *
|
---|
| 431 | * @param str Wide string to consider.
|
---|
| 432 | * @param max_len Maximum number of wide characters to measure.
|
---|
| 433 | *
|
---|
| 434 | * @return Number of bytes used by the wide characters.
|
---|
| 435 | *
|
---|
| 436 | */
|
---|
[d4a3ee5] | 437 | size_t wstr_lsize(const wchar_t *str, size_t max_len)
|
---|
[f2b8cdc] | 438 | {
|
---|
| 439 | return (wstr_nlength(str, max_len * sizeof(wchar_t)) * sizeof(wchar_t));
|
---|
| 440 | }
|
---|
| 441 |
|
---|
| 442 | /** Get number of characters in a string.
|
---|
| 443 | *
|
---|
| 444 | * @param str NULL-terminated string.
|
---|
| 445 | *
|
---|
| 446 | * @return Number of characters in string.
|
---|
| 447 | *
|
---|
| 448 | */
|
---|
[d4a3ee5] | 449 | size_t str_length(const char *str)
|
---|
[f2b8cdc] | 450 | {
|
---|
[d4a3ee5] | 451 | size_t len = 0;
|
---|
[f2b8cdc] | 452 | size_t offset = 0;
|
---|
[a35b458] | 453 |
|
---|
[f2b8cdc] | 454 | while (str_decode(str, &offset, STR_NO_LIMIT) != 0)
|
---|
| 455 | len++;
|
---|
[a35b458] | 456 |
|
---|
[f2b8cdc] | 457 | return len;
|
---|
| 458 | }
|
---|
| 459 |
|
---|
| 460 | /** Get number of characters in a wide string.
|
---|
| 461 | *
|
---|
| 462 | * @param str NULL-terminated wide string.
|
---|
| 463 | *
|
---|
| 464 | * @return Number of characters in @a str.
|
---|
| 465 | *
|
---|
| 466 | */
|
---|
[d4a3ee5] | 467 | size_t wstr_length(const wchar_t *wstr)
|
---|
[f2b8cdc] | 468 | {
|
---|
[d4a3ee5] | 469 | size_t len = 0;
|
---|
[a35b458] | 470 |
|
---|
[f2b8cdc] | 471 | while (*wstr++ != 0)
|
---|
| 472 | len++;
|
---|
[a35b458] | 473 |
|
---|
[f2b8cdc] | 474 | return len;
|
---|
| 475 | }
|
---|
| 476 |
|
---|
| 477 | /** Get number of characters in a string with size limit.
|
---|
| 478 | *
|
---|
| 479 | * @param str NULL-terminated string.
|
---|
| 480 | * @param size Maximum number of bytes to consider.
|
---|
| 481 | *
|
---|
| 482 | * @return Number of characters in string.
|
---|
| 483 | *
|
---|
| 484 | */
|
---|
[d4a3ee5] | 485 | size_t str_nlength(const char *str, size_t size)
|
---|
[f2b8cdc] | 486 | {
|
---|
[d4a3ee5] | 487 | size_t len = 0;
|
---|
[f2b8cdc] | 488 | size_t offset = 0;
|
---|
[a35b458] | 489 |
|
---|
[f2b8cdc] | 490 | while (str_decode(str, &offset, size) != 0)
|
---|
| 491 | len++;
|
---|
[a35b458] | 492 |
|
---|
[f2b8cdc] | 493 | return len;
|
---|
| 494 | }
|
---|
| 495 |
|
---|
| 496 | /** Get number of characters in a string with size limit.
|
---|
| 497 | *
|
---|
| 498 | * @param str NULL-terminated string.
|
---|
| 499 | * @param size Maximum number of bytes to consider.
|
---|
| 500 | *
|
---|
| 501 | * @return Number of characters in string.
|
---|
| 502 | *
|
---|
| 503 | */
|
---|
[d4a3ee5] | 504 | size_t wstr_nlength(const wchar_t *str, size_t size)
|
---|
[f2b8cdc] | 505 | {
|
---|
[d4a3ee5] | 506 | size_t len = 0;
|
---|
| 507 | size_t limit = ALIGN_DOWN(size, sizeof(wchar_t));
|
---|
| 508 | size_t offset = 0;
|
---|
[a35b458] | 509 |
|
---|
[f2b8cdc] | 510 | while ((offset < limit) && (*str++ != 0)) {
|
---|
| 511 | len++;
|
---|
| 512 | offset += sizeof(wchar_t);
|
---|
| 513 | }
|
---|
[a35b458] | 514 |
|
---|
[f2b8cdc] | 515 | return len;
|
---|
| 516 | }
|
---|
| 517 |
|
---|
[be2a38ad] | 518 | /** Get character display width on a character cell display.
|
---|
| 519 | *
|
---|
| 520 | * @param ch Character
|
---|
| 521 | * @return Width of character in cells.
|
---|
| 522 | */
|
---|
| 523 | size_t chr_width(wchar_t ch)
|
---|
| 524 | {
|
---|
| 525 | return 1;
|
---|
| 526 | }
|
---|
| 527 |
|
---|
| 528 | /** Get string display width on a character cell display.
|
---|
| 529 | *
|
---|
| 530 | * @param str String
|
---|
| 531 | * @return Width of string in cells.
|
---|
| 532 | */
|
---|
| 533 | size_t str_width(const char *str)
|
---|
| 534 | {
|
---|
| 535 | size_t width = 0;
|
---|
| 536 | size_t offset = 0;
|
---|
| 537 | wchar_t ch;
|
---|
[a35b458] | 538 |
|
---|
[be2a38ad] | 539 | while ((ch = str_decode(str, &offset, STR_NO_LIMIT)) != 0)
|
---|
| 540 | width += chr_width(ch);
|
---|
[a35b458] | 541 |
|
---|
[be2a38ad] | 542 | return width;
|
---|
| 543 | }
|
---|
| 544 |
|
---|
[f2b8cdc] | 545 | /** Check whether character is plain ASCII.
|
---|
| 546 | *
|
---|
| 547 | * @return True if character is plain ASCII.
|
---|
| 548 | *
|
---|
| 549 | */
|
---|
| 550 | bool ascii_check(wchar_t ch)
|
---|
| 551 | {
|
---|
[8e893ae] | 552 | if (WCHAR_SIGNED_CHECK(ch >= 0) && (ch <= 127))
|
---|
[f2b8cdc] | 553 | return true;
|
---|
[a35b458] | 554 |
|
---|
[f2b8cdc] | 555 | return false;
|
---|
| 556 | }
|
---|
| 557 |
|
---|
[171f9a1] | 558 | /** Check whether character is valid
|
---|
| 559 | *
|
---|
| 560 | * @return True if character is a valid Unicode code point.
|
---|
| 561 | *
|
---|
| 562 | */
|
---|
[f2b8cdc] | 563 | bool chr_check(wchar_t ch)
|
---|
[171f9a1] | 564 | {
|
---|
[8e893ae] | 565 | if (WCHAR_SIGNED_CHECK(ch >= 0) && (ch <= 1114111))
|
---|
[171f9a1] | 566 | return true;
|
---|
[a35b458] | 567 |
|
---|
[171f9a1] | 568 | return false;
|
---|
| 569 | }
|
---|
[936351c1] | 570 |
|
---|
[f2b8cdc] | 571 | /** Compare two NULL terminated strings.
|
---|
| 572 | *
|
---|
| 573 | * Do a char-by-char comparison of two NULL-terminated strings.
|
---|
[4efeab5] | 574 | * The strings are considered equal iff their length is equal
|
---|
| 575 | * and both strings consist of the same sequence of characters.
|
---|
| 576 | *
|
---|
[1772e6d] | 577 | * A string S1 is less than another string S2 if it has a character with
|
---|
| 578 | * lower value at the first character position where the strings differ.
|
---|
| 579 | * If the strings differ in length, the shorter one is treated as if
|
---|
| 580 | * padded by characters with a value of zero.
|
---|
[f2b8cdc] | 581 | *
|
---|
| 582 | * @param s1 First string to compare.
|
---|
| 583 | * @param s2 Second string to compare.
|
---|
| 584 | *
|
---|
[1772e6d] | 585 | * @return 0 if the strings are equal, -1 if the first is less than the second,
|
---|
| 586 | * 1 if the second is less than the first.
|
---|
[f2b8cdc] | 587 | *
|
---|
| 588 | */
|
---|
| 589 | int str_cmp(const char *s1, const char *s2)
|
---|
| 590 | {
|
---|
| 591 | wchar_t c1 = 0;
|
---|
| 592 | wchar_t c2 = 0;
|
---|
[8227d63] | 593 |
|
---|
[f2b8cdc] | 594 | size_t off1 = 0;
|
---|
| 595 | size_t off2 = 0;
|
---|
| 596 |
|
---|
| 597 | while (true) {
|
---|
| 598 | c1 = str_decode(s1, &off1, STR_NO_LIMIT);
|
---|
| 599 | c2 = str_decode(s2, &off2, STR_NO_LIMIT);
|
---|
| 600 |
|
---|
| 601 | if (c1 < c2)
|
---|
| 602 | return -1;
|
---|
[8227d63] | 603 |
|
---|
[f2b8cdc] | 604 | if (c1 > c2)
|
---|
| 605 | return 1;
|
---|
| 606 |
|
---|
| 607 | if (c1 == 0 || c2 == 0)
|
---|
[8227d63] | 608 | break;
|
---|
[f2b8cdc] | 609 | }
|
---|
| 610 |
|
---|
| 611 | return 0;
|
---|
| 612 | }
|
---|
| 613 |
|
---|
| 614 | /** Compare two NULL terminated strings with length limit.
|
---|
| 615 | *
|
---|
| 616 | * Do a char-by-char comparison of two NULL-terminated strings.
|
---|
[4efeab5] | 617 | * The strings are considered equal iff
|
---|
| 618 | * min(str_length(s1), max_len) == min(str_length(s2), max_len)
|
---|
| 619 | * and both strings consist of the same sequence of characters,
|
---|
| 620 | * up to max_len characters.
|
---|
| 621 | *
|
---|
[1772e6d] | 622 | * A string S1 is less than another string S2 if it has a character with
|
---|
| 623 | * lower value at the first character position where the strings differ.
|
---|
| 624 | * If the strings differ in length, the shorter one is treated as if
|
---|
| 625 | * padded by characters with a value of zero. Only the first max_len
|
---|
| 626 | * characters are considered.
|
---|
[f2b8cdc] | 627 | *
|
---|
| 628 | * @param s1 First string to compare.
|
---|
| 629 | * @param s2 Second string to compare.
|
---|
| 630 | * @param max_len Maximum number of characters to consider.
|
---|
| 631 | *
|
---|
[1772e6d] | 632 | * @return 0 if the strings are equal, -1 if the first is less than the second,
|
---|
| 633 | * 1 if the second is less than the first.
|
---|
[f2b8cdc] | 634 | *
|
---|
| 635 | */
|
---|
[d4a3ee5] | 636 | int str_lcmp(const char *s1, const char *s2, size_t max_len)
|
---|
[f2b8cdc] | 637 | {
|
---|
| 638 | wchar_t c1 = 0;
|
---|
| 639 | wchar_t c2 = 0;
|
---|
[8227d63] | 640 |
|
---|
[f2b8cdc] | 641 | size_t off1 = 0;
|
---|
| 642 | size_t off2 = 0;
|
---|
[8227d63] | 643 |
|
---|
[d4a3ee5] | 644 | size_t len = 0;
|
---|
[f2b8cdc] | 645 |
|
---|
| 646 | while (true) {
|
---|
| 647 | if (len >= max_len)
|
---|
| 648 | break;
|
---|
| 649 |
|
---|
| 650 | c1 = str_decode(s1, &off1, STR_NO_LIMIT);
|
---|
| 651 | c2 = str_decode(s2, &off2, STR_NO_LIMIT);
|
---|
| 652 |
|
---|
[8227d63] | 653 | if (c1 < c2)
|
---|
| 654 | return -1;
|
---|
| 655 |
|
---|
| 656 | if (c1 > c2)
|
---|
| 657 | return 1;
|
---|
| 658 |
|
---|
| 659 | if (c1 == 0 || c2 == 0)
|
---|
| 660 | break;
|
---|
| 661 |
|
---|
| 662 | ++len;
|
---|
| 663 | }
|
---|
| 664 |
|
---|
| 665 | return 0;
|
---|
| 666 |
|
---|
| 667 | }
|
---|
| 668 |
|
---|
| 669 | /** Compare two NULL terminated strings in case-insensitive manner.
|
---|
| 670 | *
|
---|
| 671 | * Do a char-by-char comparison of two NULL-terminated strings.
|
---|
| 672 | * The strings are considered equal iff their length is equal
|
---|
| 673 | * and both strings consist of the same sequence of characters
|
---|
| 674 | * when converted to lower case.
|
---|
| 675 | *
|
---|
| 676 | * A string S1 is less than another string S2 if it has a character with
|
---|
| 677 | * lower value at the first character position where the strings differ.
|
---|
| 678 | * If the strings differ in length, the shorter one is treated as if
|
---|
| 679 | * padded by characters with a value of zero.
|
---|
| 680 | *
|
---|
| 681 | * @param s1 First string to compare.
|
---|
| 682 | * @param s2 Second string to compare.
|
---|
| 683 | *
|
---|
| 684 | * @return 0 if the strings are equal, -1 if the first is less than the second,
|
---|
| 685 | * 1 if the second is less than the first.
|
---|
| 686 | *
|
---|
| 687 | */
|
---|
| 688 | int str_casecmp(const char *s1, const char *s2)
|
---|
| 689 | {
|
---|
| 690 | wchar_t c1 = 0;
|
---|
| 691 | wchar_t c2 = 0;
|
---|
| 692 |
|
---|
| 693 | size_t off1 = 0;
|
---|
| 694 | size_t off2 = 0;
|
---|
| 695 |
|
---|
| 696 | while (true) {
|
---|
| 697 | c1 = tolower(str_decode(s1, &off1, STR_NO_LIMIT));
|
---|
| 698 | c2 = tolower(str_decode(s2, &off2, STR_NO_LIMIT));
|
---|
| 699 |
|
---|
| 700 | if (c1 < c2)
|
---|
| 701 | return -1;
|
---|
| 702 |
|
---|
| 703 | if (c1 > c2)
|
---|
| 704 | return 1;
|
---|
| 705 |
|
---|
| 706 | if (c1 == 0 || c2 == 0)
|
---|
| 707 | break;
|
---|
| 708 | }
|
---|
| 709 |
|
---|
| 710 | return 0;
|
---|
| 711 | }
|
---|
| 712 |
|
---|
| 713 | /** Compare two NULL terminated strings with length limit in case-insensitive
|
---|
| 714 | * manner.
|
---|
| 715 | *
|
---|
| 716 | * Do a char-by-char comparison of two NULL-terminated strings.
|
---|
| 717 | * The strings are considered equal iff
|
---|
| 718 | * min(str_length(s1), max_len) == min(str_length(s2), max_len)
|
---|
| 719 | * and both strings consist of the same sequence of characters,
|
---|
| 720 | * up to max_len characters.
|
---|
| 721 | *
|
---|
| 722 | * A string S1 is less than another string S2 if it has a character with
|
---|
| 723 | * lower value at the first character position where the strings differ.
|
---|
| 724 | * If the strings differ in length, the shorter one is treated as if
|
---|
| 725 | * padded by characters with a value of zero. Only the first max_len
|
---|
| 726 | * characters are considered.
|
---|
| 727 | *
|
---|
| 728 | * @param s1 First string to compare.
|
---|
| 729 | * @param s2 Second string to compare.
|
---|
| 730 | * @param max_len Maximum number of characters to consider.
|
---|
| 731 | *
|
---|
| 732 | * @return 0 if the strings are equal, -1 if the first is less than the second,
|
---|
| 733 | * 1 if the second is less than the first.
|
---|
| 734 | *
|
---|
| 735 | */
|
---|
| 736 | int str_lcasecmp(const char *s1, const char *s2, size_t max_len)
|
---|
| 737 | {
|
---|
| 738 | wchar_t c1 = 0;
|
---|
| 739 | wchar_t c2 = 0;
|
---|
[a35b458] | 740 |
|
---|
[8227d63] | 741 | size_t off1 = 0;
|
---|
| 742 | size_t off2 = 0;
|
---|
[a35b458] | 743 |
|
---|
[8227d63] | 744 | size_t len = 0;
|
---|
| 745 |
|
---|
| 746 | while (true) {
|
---|
| 747 | if (len >= max_len)
|
---|
| 748 | break;
|
---|
| 749 |
|
---|
| 750 | c1 = tolower(str_decode(s1, &off1, STR_NO_LIMIT));
|
---|
| 751 | c2 = tolower(str_decode(s2, &off2, STR_NO_LIMIT));
|
---|
| 752 |
|
---|
[f2b8cdc] | 753 | if (c1 < c2)
|
---|
| 754 | return -1;
|
---|
| 755 |
|
---|
| 756 | if (c1 > c2)
|
---|
| 757 | return 1;
|
---|
| 758 |
|
---|
| 759 | if (c1 == 0 || c2 == 0)
|
---|
| 760 | break;
|
---|
| 761 |
|
---|
[1b20da0] | 762 | ++len;
|
---|
[f2b8cdc] | 763 | }
|
---|
| 764 |
|
---|
| 765 | return 0;
|
---|
| 766 |
|
---|
| 767 | }
|
---|
| 768 |
|
---|
[dce39b4] | 769 | /** Test whether p is a prefix of s.
|
---|
| 770 | *
|
---|
| 771 | * Do a char-by-char comparison of two NULL-terminated strings
|
---|
| 772 | * and determine if p is a prefix of s.
|
---|
| 773 | *
|
---|
| 774 | * @param s The string in which to look
|
---|
| 775 | * @param p The string to check if it is a prefix of s
|
---|
| 776 | *
|
---|
| 777 | * @return true iff p is prefix of s else false
|
---|
| 778 | *
|
---|
| 779 | */
|
---|
| 780 | bool str_test_prefix(const char *s, const char *p)
|
---|
| 781 | {
|
---|
| 782 | wchar_t c1 = 0;
|
---|
| 783 | wchar_t c2 = 0;
|
---|
[a35b458] | 784 |
|
---|
[dce39b4] | 785 | size_t off1 = 0;
|
---|
| 786 | size_t off2 = 0;
|
---|
| 787 |
|
---|
| 788 | while (true) {
|
---|
| 789 | c1 = str_decode(s, &off1, STR_NO_LIMIT);
|
---|
| 790 | c2 = str_decode(p, &off2, STR_NO_LIMIT);
|
---|
[a35b458] | 791 |
|
---|
[dce39b4] | 792 | if (c2 == 0)
|
---|
| 793 | return true;
|
---|
| 794 |
|
---|
| 795 | if (c1 != c2)
|
---|
| 796 | return false;
|
---|
[a35b458] | 797 |
|
---|
[dce39b4] | 798 | if (c1 == 0)
|
---|
| 799 | break;
|
---|
| 800 | }
|
---|
| 801 |
|
---|
| 802 | return false;
|
---|
| 803 | }
|
---|
| 804 |
|
---|
[086cab0] | 805 | /** Get a string suffix.
|
---|
| 806 | *
|
---|
| 807 | * Return a string suffix defined by the prefix length.
|
---|
| 808 | *
|
---|
| 809 | * @param s The string to get the suffix from.
|
---|
| 810 | * @param prefix_length Number of prefix characters to ignore.
|
---|
| 811 | *
|
---|
| 812 | * @return String suffix.
|
---|
| 813 | *
|
---|
| 814 | */
|
---|
| 815 | const char *str_suffix(const char *s, size_t prefix_length)
|
---|
| 816 | {
|
---|
| 817 | size_t off = 0;
|
---|
| 818 | size_t i = 0;
|
---|
| 819 |
|
---|
| 820 | while (true) {
|
---|
| 821 | str_decode(s, &off, STR_NO_LIMIT);
|
---|
| 822 | i++;
|
---|
| 823 |
|
---|
| 824 | if (i >= prefix_length)
|
---|
| 825 | break;
|
---|
| 826 | }
|
---|
| 827 |
|
---|
| 828 | return s + off;
|
---|
| 829 | }
|
---|
| 830 |
|
---|
[6eb2e96] | 831 | /** Copy string.
|
---|
[f2b8cdc] | 832 | *
|
---|
[6eb2e96] | 833 | * Copy source string @a src to destination buffer @a dest.
|
---|
| 834 | * No more than @a size bytes are written. If the size of the output buffer
|
---|
| 835 | * is at least one byte, the output string will always be well-formed, i.e.
|
---|
| 836 | * null-terminated and containing only complete characters.
|
---|
[f2b8cdc] | 837 | *
|
---|
[abf09311] | 838 | * @param dest Destination buffer.
|
---|
[6700ee2] | 839 | * @param count Size of the destination buffer (must be > 0).
|
---|
[6eb2e96] | 840 | * @param src Source string.
|
---|
[8e893ae] | 841 | *
|
---|
[f2b8cdc] | 842 | */
|
---|
[6eb2e96] | 843 | void str_cpy(char *dest, size_t size, const char *src)
|
---|
[f2b8cdc] | 844 | {
|
---|
[6700ee2] | 845 | /* There must be space for a null terminator in the buffer. */
|
---|
| 846 | assert(size > 0);
|
---|
[d066259] | 847 | assert(src != NULL);
|
---|
[a35b458] | 848 |
|
---|
[abf09311] | 849 | size_t src_off = 0;
|
---|
| 850 | size_t dest_off = 0;
|
---|
[a35b458] | 851 |
|
---|
[abf09311] | 852 | wchar_t ch;
|
---|
[6eb2e96] | 853 | while ((ch = str_decode(src, &src_off, STR_NO_LIMIT)) != 0) {
|
---|
| 854 | if (chr_encode(ch, dest, &dest_off, size - 1) != EOK)
|
---|
| 855 | break;
|
---|
| 856 | }
|
---|
[a35b458] | 857 |
|
---|
[6eb2e96] | 858 | dest[dest_off] = '\0';
|
---|
| 859 | }
|
---|
| 860 |
|
---|
| 861 | /** Copy size-limited substring.
|
---|
| 862 | *
|
---|
[6700ee2] | 863 | * Copy prefix of string @a src of max. size @a size to destination buffer
|
---|
| 864 | * @a dest. No more than @a size bytes are written. The output string will
|
---|
| 865 | * always be well-formed, i.e. null-terminated and containing only complete
|
---|
| 866 | * characters.
|
---|
[6eb2e96] | 867 | *
|
---|
| 868 | * No more than @a n bytes are read from the input string, so it does not
|
---|
| 869 | * have to be null-terminated.
|
---|
| 870 | *
|
---|
[abf09311] | 871 | * @param dest Destination buffer.
|
---|
[6700ee2] | 872 | * @param count Size of the destination buffer (must be > 0).
|
---|
[6eb2e96] | 873 | * @param src Source string.
|
---|
[abf09311] | 874 | * @param n Maximum number of bytes to read from @a src.
|
---|
[8e893ae] | 875 | *
|
---|
[6eb2e96] | 876 | */
|
---|
| 877 | void str_ncpy(char *dest, size_t size, const char *src, size_t n)
|
---|
| 878 | {
|
---|
[6700ee2] | 879 | /* There must be space for a null terminator in the buffer. */
|
---|
| 880 | assert(size > 0);
|
---|
[a35b458] | 881 |
|
---|
[abf09311] | 882 | size_t src_off = 0;
|
---|
| 883 | size_t dest_off = 0;
|
---|
[a35b458] | 884 |
|
---|
[abf09311] | 885 | wchar_t ch;
|
---|
[6eb2e96] | 886 | while ((ch = str_decode(src, &src_off, n)) != 0) {
|
---|
| 887 | if (chr_encode(ch, dest, &dest_off, size - 1) != EOK)
|
---|
[f2b8cdc] | 888 | break;
|
---|
| 889 | }
|
---|
[a35b458] | 890 |
|
---|
[6eb2e96] | 891 | dest[dest_off] = '\0';
|
---|
[f2b8cdc] | 892 | }
|
---|
| 893 |
|
---|
[4482bc7] | 894 | /** Append one string to another.
|
---|
| 895 | *
|
---|
| 896 | * Append source string @a src to string in destination buffer @a dest.
|
---|
| 897 | * Size of the destination buffer is @a dest. If the size of the output buffer
|
---|
| 898 | * is at least one byte, the output string will always be well-formed, i.e.
|
---|
| 899 | * null-terminated and containing only complete characters.
|
---|
| 900 | *
|
---|
[0f06dbc] | 901 | * @param dest Destination buffer.
|
---|
[4482bc7] | 902 | * @param count Size of the destination buffer.
|
---|
| 903 | * @param src Source string.
|
---|
| 904 | */
|
---|
| 905 | void str_append(char *dest, size_t size, const char *src)
|
---|
| 906 | {
|
---|
| 907 | size_t dstr_size;
|
---|
| 908 |
|
---|
| 909 | dstr_size = str_size(dest);
|
---|
[3815efb] | 910 | if (dstr_size >= size)
|
---|
[a8bc7f8] | 911 | return;
|
---|
[a35b458] | 912 |
|
---|
[4482bc7] | 913 | str_cpy(dest + dstr_size, size - dstr_size, src);
|
---|
| 914 | }
|
---|
| 915 |
|
---|
[dcb74c0a] | 916 | /** Convert space-padded ASCII to string.
|
---|
| 917 | *
|
---|
| 918 | * Common legacy text encoding in hardware is 7-bit ASCII fitted into
|
---|
[c3d19ac] | 919 | * a fixed-width byte buffer (bit 7 always zero), right-padded with spaces
|
---|
[dcb74c0a] | 920 | * (ASCII 0x20). Convert space-padded ascii to string representation.
|
---|
| 921 | *
|
---|
| 922 | * If the text does not fit into the destination buffer, the function converts
|
---|
| 923 | * as many characters as possible and returns EOVERFLOW.
|
---|
| 924 | *
|
---|
| 925 | * If the text contains non-ASCII bytes (with bit 7 set), the whole string is
|
---|
| 926 | * converted anyway and invalid characters are replaced with question marks
|
---|
| 927 | * (U_SPECIAL) and the function returns EIO.
|
---|
| 928 | *
|
---|
| 929 | * Regardless of return value upon return @a dest will always be well-formed.
|
---|
| 930 | *
|
---|
| 931 | * @param dest Destination buffer
|
---|
| 932 | * @param size Size of destination buffer
|
---|
| 933 | * @param src Space-padded ASCII.
|
---|
| 934 | * @param n Size of the source buffer in bytes.
|
---|
| 935 | *
|
---|
| 936 | * @return EOK on success, EOVERFLOW if the text does not fit
|
---|
| 937 | * destination buffer, EIO if the text contains
|
---|
| 938 | * non-ASCII bytes.
|
---|
| 939 | */
|
---|
[b7fd2a0] | 940 | errno_t spascii_to_str(char *dest, size_t size, const uint8_t *src, size_t n)
|
---|
[dcb74c0a] | 941 | {
|
---|
| 942 | size_t sidx;
|
---|
| 943 | size_t didx;
|
---|
| 944 | size_t dlast;
|
---|
| 945 | uint8_t byte;
|
---|
[b7fd2a0] | 946 | errno_t rc;
|
---|
| 947 | errno_t result;
|
---|
[dcb74c0a] | 948 |
|
---|
| 949 | /* There must be space for a null terminator in the buffer. */
|
---|
| 950 | assert(size > 0);
|
---|
| 951 | result = EOK;
|
---|
| 952 |
|
---|
| 953 | didx = 0;
|
---|
| 954 | dlast = 0;
|
---|
| 955 | for (sidx = 0; sidx < n; ++sidx) {
|
---|
| 956 | byte = src[sidx];
|
---|
| 957 | if (!ascii_check(byte)) {
|
---|
| 958 | byte = U_SPECIAL;
|
---|
| 959 | result = EIO;
|
---|
| 960 | }
|
---|
| 961 |
|
---|
| 962 | rc = chr_encode(byte, dest, &didx, size - 1);
|
---|
| 963 | if (rc != EOK) {
|
---|
| 964 | assert(rc == EOVERFLOW);
|
---|
| 965 | dest[didx] = '\0';
|
---|
| 966 | return rc;
|
---|
| 967 | }
|
---|
| 968 |
|
---|
| 969 | /* Remember dest index after last non-empty character */
|
---|
| 970 | if (byte != 0x20)
|
---|
| 971 | dlast = didx;
|
---|
| 972 | }
|
---|
| 973 |
|
---|
| 974 | /* Terminate string after last non-empty character */
|
---|
| 975 | dest[dlast] = '\0';
|
---|
| 976 | return result;
|
---|
| 977 | }
|
---|
| 978 |
|
---|
[0f06dbc] | 979 | /** Convert wide string to string.
|
---|
[f2b8cdc] | 980 | *
|
---|
[0f06dbc] | 981 | * Convert wide string @a src to string. The output is written to the buffer
|
---|
| 982 | * specified by @a dest and @a size. @a size must be non-zero and the string
|
---|
| 983 | * written will always be well-formed.
|
---|
[f2b8cdc] | 984 | *
|
---|
[0f06dbc] | 985 | * @param dest Destination buffer.
|
---|
| 986 | * @param size Size of the destination buffer.
|
---|
| 987 | * @param src Source wide string.
|
---|
[f2b8cdc] | 988 | */
|
---|
[81e9cb3] | 989 | void wstr_to_str(char *dest, size_t size, const wchar_t *src)
|
---|
[f2b8cdc] | 990 | {
|
---|
| 991 | wchar_t ch;
|
---|
[0f06dbc] | 992 | size_t src_idx;
|
---|
| 993 | size_t dest_off;
|
---|
| 994 |
|
---|
| 995 | /* There must be space for a null terminator in the buffer. */
|
---|
| 996 | assert(size > 0);
|
---|
[a35b458] | 997 |
|
---|
[0f06dbc] | 998 | src_idx = 0;
|
---|
| 999 | dest_off = 0;
|
---|
| 1000 |
|
---|
[f2b8cdc] | 1001 | while ((ch = src[src_idx++]) != 0) {
|
---|
[81e9cb3] | 1002 | if (chr_encode(ch, dest, &dest_off, size - 1) != EOK)
|
---|
[f2b8cdc] | 1003 | break;
|
---|
| 1004 | }
|
---|
[0f06dbc] | 1005 |
|
---|
| 1006 | dest[dest_off] = '\0';
|
---|
[f2b8cdc] | 1007 | }
|
---|
| 1008 |
|
---|
[82374b2] | 1009 | /** Convert UTF16 string to string.
|
---|
| 1010 | *
|
---|
| 1011 | * Convert utf16 string @a src to string. The output is written to the buffer
|
---|
| 1012 | * specified by @a dest and @a size. @a size must be non-zero and the string
|
---|
| 1013 | * written will always be well-formed. Surrogate pairs also supported.
|
---|
| 1014 | *
|
---|
| 1015 | * @param dest Destination buffer.
|
---|
| 1016 | * @param size Size of the destination buffer.
|
---|
| 1017 | * @param src Source utf16 string.
|
---|
| 1018 | *
|
---|
[cde999a] | 1019 | * @return EOK, if success, an error code otherwise.
|
---|
[82374b2] | 1020 | */
|
---|
[b7fd2a0] | 1021 | errno_t utf16_to_str(char *dest, size_t size, const uint16_t *src)
|
---|
[82374b2] | 1022 | {
|
---|
[abb7491c] | 1023 | size_t idx = 0, dest_off = 0;
|
---|
[82374b2] | 1024 | wchar_t ch;
|
---|
[b7fd2a0] | 1025 | errno_t rc = EOK;
|
---|
[82374b2] | 1026 |
|
---|
| 1027 | /* There must be space for a null terminator in the buffer. */
|
---|
| 1028 | assert(size > 0);
|
---|
| 1029 |
|
---|
| 1030 | while (src[idx]) {
|
---|
| 1031 | if ((src[idx] & 0xfc00) == 0xd800) {
|
---|
[abb7491c] | 1032 | if (src[idx + 1] && (src[idx + 1] & 0xfc00) == 0xdc00) {
|
---|
[82374b2] | 1033 | ch = 0x10000;
|
---|
| 1034 | ch += (src[idx] & 0x03FF) << 10;
|
---|
[abb7491c] | 1035 | ch += (src[idx + 1] & 0x03FF);
|
---|
[82374b2] | 1036 | idx += 2;
|
---|
[1433ecda] | 1037 | } else
|
---|
[82374b2] | 1038 | break;
|
---|
| 1039 | } else {
|
---|
| 1040 | ch = src[idx];
|
---|
| 1041 | idx++;
|
---|
| 1042 | }
|
---|
[abb7491c] | 1043 | rc = chr_encode(ch, dest, &dest_off, size - 1);
|
---|
[82374b2] | 1044 | if (rc != EOK)
|
---|
| 1045 | break;
|
---|
| 1046 | }
|
---|
| 1047 | dest[dest_off] = '\0';
|
---|
| 1048 | return rc;
|
---|
| 1049 | }
|
---|
| 1050 |
|
---|
[b06414f] | 1051 | /** Convert string to UTF16 string.
|
---|
| 1052 | *
|
---|
| 1053 | * Convert string @a src to utf16 string. The output is written to the buffer
|
---|
| 1054 | * specified by @a dest and @a dlen. @a dlen must be non-zero and the string
|
---|
| 1055 | * written will always be well-formed. Surrogate pairs also supported.
|
---|
| 1056 | *
|
---|
| 1057 | * @param dest Destination buffer.
|
---|
| 1058 | * @param dlen Number of utf16 characters that fit in the destination buffer.
|
---|
| 1059 | * @param src Source string.
|
---|
| 1060 | *
|
---|
[cde999a] | 1061 | * @return EOK, if success, an error code otherwise.
|
---|
[b06414f] | 1062 | */
|
---|
[b7fd2a0] | 1063 | errno_t str_to_utf16(uint16_t *dest, size_t dlen, const char *src)
|
---|
[fc97128] | 1064 | {
|
---|
[b7fd2a0] | 1065 | errno_t rc = EOK;
|
---|
[abb7491c] | 1066 | size_t offset = 0;
|
---|
| 1067 | size_t idx = 0;
|
---|
[fc97128] | 1068 | wchar_t c;
|
---|
| 1069 |
|
---|
[b06414f] | 1070 | assert(dlen > 0);
|
---|
[a35b458] | 1071 |
|
---|
[fc97128] | 1072 | while ((c = str_decode(src, &offset, STR_NO_LIMIT)) != 0) {
|
---|
| 1073 | if (c > 0x10000) {
|
---|
[b06414f] | 1074 | if (idx + 2 >= dlen - 1) {
|
---|
[abb7491c] | 1075 | rc = EOVERFLOW;
|
---|
[fc97128] | 1076 | break;
|
---|
| 1077 | }
|
---|
| 1078 | c = (c - 0x10000);
|
---|
| 1079 | dest[idx] = 0xD800 | (c >> 10);
|
---|
[abb7491c] | 1080 | dest[idx + 1] = 0xDC00 | (c & 0x3FF);
|
---|
[fc97128] | 1081 | idx++;
|
---|
| 1082 | } else {
|
---|
[1433ecda] | 1083 | dest[idx] = c;
|
---|
[fc97128] | 1084 | }
|
---|
| 1085 |
|
---|
| 1086 | idx++;
|
---|
[b06414f] | 1087 | if (idx >= dlen - 1) {
|
---|
[abb7491c] | 1088 | rc = EOVERFLOW;
|
---|
[fc97128] | 1089 | break;
|
---|
| 1090 | }
|
---|
| 1091 | }
|
---|
| 1092 |
|
---|
| 1093 | dest[idx] = '\0';
|
---|
| 1094 | return rc;
|
---|
[f2b8cdc] | 1095 | }
|
---|
| 1096 |
|
---|
[b2906c0] | 1097 | /** Get size of UTF-16 string.
|
---|
| 1098 | *
|
---|
| 1099 | * Get the number of words which are used by the UTF-16 string @a ustr
|
---|
| 1100 | * (excluding the NULL-terminator).
|
---|
| 1101 | *
|
---|
| 1102 | * @param ustr UTF-16 string to consider.
|
---|
| 1103 | *
|
---|
| 1104 | * @return Number of words used by the UTF-16 string
|
---|
| 1105 | *
|
---|
| 1106 | */
|
---|
| 1107 | size_t utf16_wsize(const uint16_t *ustr)
|
---|
| 1108 | {
|
---|
| 1109 | size_t wsize = 0;
|
---|
| 1110 |
|
---|
| 1111 | while (*ustr++ != 0)
|
---|
| 1112 | wsize++;
|
---|
| 1113 |
|
---|
| 1114 | return wsize;
|
---|
| 1115 | }
|
---|
| 1116 |
|
---|
[b67c7d64] | 1117 | /** Convert wide string to new string.
|
---|
| 1118 | *
|
---|
| 1119 | * Convert wide string @a src to string. Space for the new string is allocated
|
---|
| 1120 | * on the heap.
|
---|
| 1121 | *
|
---|
| 1122 | * @param src Source wide string.
|
---|
| 1123 | * @return New string.
|
---|
| 1124 | */
|
---|
| 1125 | char *wstr_to_astr(const wchar_t *src)
|
---|
| 1126 | {
|
---|
| 1127 | char dbuf[STR_BOUNDS(1)];
|
---|
| 1128 | char *str;
|
---|
| 1129 | wchar_t ch;
|
---|
| 1130 |
|
---|
| 1131 | size_t src_idx;
|
---|
| 1132 | size_t dest_off;
|
---|
| 1133 | size_t dest_size;
|
---|
| 1134 |
|
---|
| 1135 | /* Compute size of encoded string. */
|
---|
| 1136 |
|
---|
| 1137 | src_idx = 0;
|
---|
| 1138 | dest_size = 0;
|
---|
| 1139 |
|
---|
| 1140 | while ((ch = src[src_idx++]) != 0) {
|
---|
| 1141 | dest_off = 0;
|
---|
| 1142 | if (chr_encode(ch, dbuf, &dest_off, STR_BOUNDS(1)) != EOK)
|
---|
| 1143 | break;
|
---|
| 1144 | dest_size += dest_off;
|
---|
| 1145 | }
|
---|
| 1146 |
|
---|
| 1147 | str = malloc(dest_size + 1);
|
---|
| 1148 | if (str == NULL)
|
---|
| 1149 | return NULL;
|
---|
| 1150 |
|
---|
| 1151 | /* Encode string. */
|
---|
| 1152 |
|
---|
| 1153 | src_idx = 0;
|
---|
| 1154 | dest_off = 0;
|
---|
| 1155 |
|
---|
| 1156 | while ((ch = src[src_idx++]) != 0) {
|
---|
| 1157 | if (chr_encode(ch, str, &dest_off, dest_size) != EOK)
|
---|
| 1158 | break;
|
---|
| 1159 | }
|
---|
| 1160 |
|
---|
| 1161 | str[dest_size] = '\0';
|
---|
| 1162 | return str;
|
---|
| 1163 | }
|
---|
| 1164 |
|
---|
[da2bd08] | 1165 | /** Convert string to wide string.
|
---|
| 1166 | *
|
---|
| 1167 | * Convert string @a src to wide string. The output is written to the
|
---|
[0f06dbc] | 1168 | * buffer specified by @a dest and @a dlen. @a dlen must be non-zero
|
---|
| 1169 | * and the wide string written will always be null-terminated.
|
---|
[da2bd08] | 1170 | *
|
---|
| 1171 | * @param dest Destination buffer.
|
---|
| 1172 | * @param dlen Length of destination buffer (number of wchars).
|
---|
| 1173 | * @param src Source string.
|
---|
| 1174 | */
|
---|
[81e9cb3] | 1175 | void str_to_wstr(wchar_t *dest, size_t dlen, const char *src)
|
---|
[da2bd08] | 1176 | {
|
---|
| 1177 | size_t offset;
|
---|
| 1178 | size_t di;
|
---|
| 1179 | wchar_t c;
|
---|
| 1180 |
|
---|
| 1181 | assert(dlen > 0);
|
---|
| 1182 |
|
---|
| 1183 | offset = 0;
|
---|
| 1184 | di = 0;
|
---|
| 1185 |
|
---|
| 1186 | do {
|
---|
[81e9cb3] | 1187 | if (di >= dlen - 1)
|
---|
[da2bd08] | 1188 | break;
|
---|
| 1189 |
|
---|
| 1190 | c = str_decode(src, &offset, STR_NO_LIMIT);
|
---|
| 1191 | dest[di++] = c;
|
---|
| 1192 | } while (c != '\0');
|
---|
| 1193 |
|
---|
| 1194 | dest[dlen - 1] = '\0';
|
---|
| 1195 | }
|
---|
| 1196 |
|
---|
[22cf42d9] | 1197 | /** Convert string to wide string.
|
---|
| 1198 | *
|
---|
| 1199 | * Convert string @a src to wide string. A new wide NULL-terminated
|
---|
| 1200 | * string will be allocated on the heap.
|
---|
| 1201 | *
|
---|
| 1202 | * @param src Source string.
|
---|
| 1203 | */
|
---|
| 1204 | wchar_t *str_to_awstr(const char *str)
|
---|
| 1205 | {
|
---|
| 1206 | size_t len = str_length(str);
|
---|
[a35b458] | 1207 |
|
---|
[1433ecda] | 1208 | wchar_t *wstr = calloc(len + 1, sizeof(wchar_t));
|
---|
[b48d046] | 1209 | if (wstr == NULL)
|
---|
| 1210 | return NULL;
|
---|
[a35b458] | 1211 |
|
---|
[b48d046] | 1212 | str_to_wstr(wstr, len + 1, str);
|
---|
[22cf42d9] | 1213 | return wstr;
|
---|
| 1214 | }
|
---|
| 1215 |
|
---|
[f2b8cdc] | 1216 | /** Find first occurence of character in string.
|
---|
| 1217 | *
|
---|
| 1218 | * @param str String to search.
|
---|
| 1219 | * @param ch Character to look for.
|
---|
| 1220 | *
|
---|
| 1221 | * @return Pointer to character in @a str or NULL if not found.
|
---|
| 1222 | */
|
---|
[dd2cfa7] | 1223 | char *str_chr(const char *str, wchar_t ch)
|
---|
[f2b8cdc] | 1224 | {
|
---|
| 1225 | wchar_t acc;
|
---|
| 1226 | size_t off = 0;
|
---|
[f2d2c7ba] | 1227 | size_t last = 0;
|
---|
[a35b458] | 1228 |
|
---|
[f2b8cdc] | 1229 | while ((acc = str_decode(str, &off, STR_NO_LIMIT)) != 0) {
|
---|
| 1230 | if (acc == ch)
|
---|
[dd2cfa7] | 1231 | return (char *) (str + last);
|
---|
[f2d2c7ba] | 1232 | last = off;
|
---|
[f2b8cdc] | 1233 | }
|
---|
[a35b458] | 1234 |
|
---|
[f2b8cdc] | 1235 | return NULL;
|
---|
| 1236 | }
|
---|
| 1237 |
|
---|
[da680b4b] | 1238 | /** Find first occurence of substring in string.
|
---|
| 1239 | *
|
---|
| 1240 | * @param hs Haystack (string)
|
---|
| 1241 | * @param n Needle (substring to look for)
|
---|
| 1242 | *
|
---|
| 1243 | * @return Pointer to character in @a hs or @c NULL if not found.
|
---|
| 1244 | */
|
---|
| 1245 | char *str_str(const char *hs, const char *n)
|
---|
| 1246 | {
|
---|
| 1247 | size_t off = 0;
|
---|
| 1248 |
|
---|
| 1249 | if (str_lcmp(hs, n, str_length(n)) == 0)
|
---|
| 1250 | return (char *)hs;
|
---|
| 1251 |
|
---|
| 1252 | while (str_decode(hs, &off, STR_NO_LIMIT) != 0) {
|
---|
| 1253 | if (str_lcmp(hs + off, n, str_length(n)) == 0)
|
---|
| 1254 | return (char *)(hs + off);
|
---|
| 1255 | }
|
---|
| 1256 |
|
---|
| 1257 | return NULL;
|
---|
| 1258 | }
|
---|
| 1259 |
|
---|
[1737bfb] | 1260 | /** Removes specified trailing characters from a string.
|
---|
| 1261 | *
|
---|
| 1262 | * @param str String to remove from.
|
---|
| 1263 | * @param ch Character to remove.
|
---|
| 1264 | */
|
---|
| 1265 | void str_rtrim(char *str, wchar_t ch)
|
---|
| 1266 | {
|
---|
| 1267 | size_t off = 0;
|
---|
| 1268 | size_t pos = 0;
|
---|
| 1269 | wchar_t c;
|
---|
| 1270 | bool update_last_chunk = true;
|
---|
| 1271 | char *last_chunk = NULL;
|
---|
| 1272 |
|
---|
| 1273 | while ((c = str_decode(str, &off, STR_NO_LIMIT))) {
|
---|
| 1274 | if (c != ch) {
|
---|
| 1275 | update_last_chunk = true;
|
---|
| 1276 | last_chunk = NULL;
|
---|
| 1277 | } else if (update_last_chunk) {
|
---|
| 1278 | update_last_chunk = false;
|
---|
| 1279 | last_chunk = (str + pos);
|
---|
| 1280 | }
|
---|
| 1281 | pos = off;
|
---|
| 1282 | }
|
---|
| 1283 |
|
---|
| 1284 | if (last_chunk)
|
---|
| 1285 | *last_chunk = '\0';
|
---|
| 1286 | }
|
---|
| 1287 |
|
---|
| 1288 | /** Removes specified leading characters from a string.
|
---|
| 1289 | *
|
---|
| 1290 | * @param str String to remove from.
|
---|
| 1291 | * @param ch Character to remove.
|
---|
| 1292 | */
|
---|
| 1293 | void str_ltrim(char *str, wchar_t ch)
|
---|
| 1294 | {
|
---|
| 1295 | wchar_t acc;
|
---|
| 1296 | size_t off = 0;
|
---|
| 1297 | size_t pos = 0;
|
---|
| 1298 | size_t str_sz = str_size(str);
|
---|
| 1299 |
|
---|
| 1300 | while ((acc = str_decode(str, &off, STR_NO_LIMIT)) != 0) {
|
---|
| 1301 | if (acc != ch)
|
---|
| 1302 | break;
|
---|
| 1303 | else
|
---|
| 1304 | pos = off;
|
---|
| 1305 | }
|
---|
| 1306 |
|
---|
| 1307 | if (pos > 0) {
|
---|
| 1308 | memmove(str, &str[pos], str_sz - pos);
|
---|
| 1309 | pos = str_sz - pos;
|
---|
[a18a8b9] | 1310 | str[pos] = '\0';
|
---|
[1737bfb] | 1311 | }
|
---|
| 1312 | }
|
---|
| 1313 |
|
---|
[7afb4a5] | 1314 | /** Find last occurence of character in string.
|
---|
| 1315 | *
|
---|
| 1316 | * @param str String to search.
|
---|
| 1317 | * @param ch Character to look for.
|
---|
| 1318 | *
|
---|
| 1319 | * @return Pointer to character in @a str or NULL if not found.
|
---|
| 1320 | */
|
---|
[dd2cfa7] | 1321 | char *str_rchr(const char *str, wchar_t ch)
|
---|
[7afb4a5] | 1322 | {
|
---|
| 1323 | wchar_t acc;
|
---|
| 1324 | size_t off = 0;
|
---|
[f2d2c7ba] | 1325 | size_t last = 0;
|
---|
[d4a3ee5] | 1326 | const char *res = NULL;
|
---|
[a35b458] | 1327 |
|
---|
[7afb4a5] | 1328 | while ((acc = str_decode(str, &off, STR_NO_LIMIT)) != 0) {
|
---|
| 1329 | if (acc == ch)
|
---|
[f2d2c7ba] | 1330 | res = (str + last);
|
---|
| 1331 | last = off;
|
---|
[7afb4a5] | 1332 | }
|
---|
[a35b458] | 1333 |
|
---|
[dd2cfa7] | 1334 | return (char *) res;
|
---|
[7afb4a5] | 1335 | }
|
---|
| 1336 |
|
---|
[f2b8cdc] | 1337 | /** Insert a wide character into a wide string.
|
---|
| 1338 | *
|
---|
| 1339 | * Insert a wide character into a wide string at position
|
---|
| 1340 | * @a pos. The characters after the position are shifted.
|
---|
| 1341 | *
|
---|
| 1342 | * @param str String to insert to.
|
---|
| 1343 | * @param ch Character to insert to.
|
---|
| 1344 | * @param pos Character index where to insert.
|
---|
[7c3fb9b] | 1345 | * @param max_pos Characters in the buffer.
|
---|
[f2b8cdc] | 1346 | *
|
---|
| 1347 | * @return True if the insertion was sucessful, false if the position
|
---|
| 1348 | * is out of bounds.
|
---|
| 1349 | *
|
---|
| 1350 | */
|
---|
[d4a3ee5] | 1351 | bool wstr_linsert(wchar_t *str, wchar_t ch, size_t pos, size_t max_pos)
|
---|
[f2b8cdc] | 1352 | {
|
---|
[d4a3ee5] | 1353 | size_t len = wstr_length(str);
|
---|
[a35b458] | 1354 |
|
---|
[f2b8cdc] | 1355 | if ((pos > len) || (pos + 1 > max_pos))
|
---|
| 1356 | return false;
|
---|
[a35b458] | 1357 |
|
---|
[d4a3ee5] | 1358 | size_t i;
|
---|
[f2b8cdc] | 1359 | for (i = len; i + 1 > pos; i--)
|
---|
| 1360 | str[i + 1] = str[i];
|
---|
[a35b458] | 1361 |
|
---|
[f2b8cdc] | 1362 | str[pos] = ch;
|
---|
[a35b458] | 1363 |
|
---|
[f2b8cdc] | 1364 | return true;
|
---|
| 1365 | }
|
---|
| 1366 |
|
---|
| 1367 | /** Remove a wide character from a wide string.
|
---|
| 1368 | *
|
---|
| 1369 | * Remove a wide character from a wide string at position
|
---|
| 1370 | * @a pos. The characters after the position are shifted.
|
---|
| 1371 | *
|
---|
| 1372 | * @param str String to remove from.
|
---|
| 1373 | * @param pos Character index to remove.
|
---|
| 1374 | *
|
---|
| 1375 | * @return True if the removal was sucessful, false if the position
|
---|
| 1376 | * is out of bounds.
|
---|
| 1377 | *
|
---|
| 1378 | */
|
---|
[d4a3ee5] | 1379 | bool wstr_remove(wchar_t *str, size_t pos)
|
---|
[f2b8cdc] | 1380 | {
|
---|
[d4a3ee5] | 1381 | size_t len = wstr_length(str);
|
---|
[a35b458] | 1382 |
|
---|
[f2b8cdc] | 1383 | if (pos >= len)
|
---|
| 1384 | return false;
|
---|
[a35b458] | 1385 |
|
---|
[d4a3ee5] | 1386 | size_t i;
|
---|
[f2b8cdc] | 1387 | for (i = pos + 1; i <= len; i++)
|
---|
| 1388 | str[i - 1] = str[i];
|
---|
[a35b458] | 1389 |
|
---|
[f2b8cdc] | 1390 | return true;
|
---|
| 1391 | }
|
---|
| 1392 |
|
---|
[abf09311] | 1393 | /** Duplicate string.
|
---|
| 1394 | *
|
---|
| 1395 | * Allocate a new string and copy characters from the source
|
---|
| 1396 | * string into it. The duplicate string is allocated via sleeping
|
---|
| 1397 | * malloc(), thus this function can sleep in no memory conditions.
|
---|
| 1398 | *
|
---|
| 1399 | * The allocation cannot fail and the return value is always
|
---|
| 1400 | * a valid pointer. The duplicate string is always a well-formed
|
---|
| 1401 | * null-terminated UTF-8 string, but it can differ from the source
|
---|
| 1402 | * string on the byte level.
|
---|
| 1403 | *
|
---|
| 1404 | * @param src Source string.
|
---|
| 1405 | *
|
---|
| 1406 | * @return Duplicate string.
|
---|
| 1407 | *
|
---|
| 1408 | */
|
---|
[fc6dd18] | 1409 | char *str_dup(const char *src)
|
---|
| 1410 | {
|
---|
[abf09311] | 1411 | size_t size = str_size(src) + 1;
|
---|
[d066259] | 1412 | char *dest = malloc(size);
|
---|
| 1413 | if (!dest)
|
---|
| 1414 | return NULL;
|
---|
[a35b458] | 1415 |
|
---|
[abf09311] | 1416 | str_cpy(dest, size, src);
|
---|
| 1417 | return dest;
|
---|
[fc6dd18] | 1418 | }
|
---|
| 1419 |
|
---|
[abf09311] | 1420 | /** Duplicate string with size limit.
|
---|
| 1421 | *
|
---|
| 1422 | * Allocate a new string and copy up to @max_size bytes from the source
|
---|
| 1423 | * string into it. The duplicate string is allocated via sleeping
|
---|
| 1424 | * malloc(), thus this function can sleep in no memory conditions.
|
---|
| 1425 | * No more than @max_size + 1 bytes is allocated, but if the size
|
---|
| 1426 | * occupied by the source string is smaller than @max_size + 1,
|
---|
| 1427 | * less is allocated.
|
---|
| 1428 | *
|
---|
| 1429 | * The allocation cannot fail and the return value is always
|
---|
| 1430 | * a valid pointer. The duplicate string is always a well-formed
|
---|
| 1431 | * null-terminated UTF-8 string, but it can differ from the source
|
---|
| 1432 | * string on the byte level.
|
---|
| 1433 | *
|
---|
| 1434 | * @param src Source string.
|
---|
| 1435 | * @param n Maximum number of bytes to duplicate.
|
---|
| 1436 | *
|
---|
| 1437 | * @return Duplicate string.
|
---|
| 1438 | *
|
---|
| 1439 | */
|
---|
| 1440 | char *str_ndup(const char *src, size_t n)
|
---|
[fc6dd18] | 1441 | {
|
---|
| 1442 | size_t size = str_size(src);
|
---|
[abf09311] | 1443 | if (size > n)
|
---|
| 1444 | size = n;
|
---|
[a35b458] | 1445 |
|
---|
[d066259] | 1446 | char *dest = malloc(size + 1);
|
---|
| 1447 | if (!dest)
|
---|
| 1448 | return NULL;
|
---|
[a35b458] | 1449 |
|
---|
[abf09311] | 1450 | str_ncpy(dest, size + 1, src, size);
|
---|
[fc6dd18] | 1451 | return dest;
|
---|
| 1452 | }
|
---|
| 1453 |
|
---|
[ee3f6f6] | 1454 | /** Split string by delimiters.
|
---|
| 1455 | *
|
---|
| 1456 | * @param s String to be tokenized. May not be NULL.
|
---|
| 1457 | * @param delim String with the delimiters.
|
---|
| 1458 | * @param next Variable which will receive the pointer to the
|
---|
| 1459 | * continuation of the string following the first
|
---|
| 1460 | * occurrence of any of the delimiter characters.
|
---|
| 1461 | * May be NULL.
|
---|
| 1462 | * @return Pointer to the prefix of @a s before the first
|
---|
| 1463 | * delimiter character. NULL if no such prefix
|
---|
| 1464 | * exists.
|
---|
| 1465 | */
|
---|
| 1466 | char *str_tok(char *s, const char *delim, char **next)
|
---|
[576845ec] | 1467 | {
|
---|
| 1468 | char *start, *end;
|
---|
[69df837f] | 1469 |
|
---|
[ee3f6f6] | 1470 | if (!s)
|
---|
| 1471 | return NULL;
|
---|
[a35b458] | 1472 |
|
---|
[ee3f6f6] | 1473 | size_t len = str_size(s);
|
---|
| 1474 | size_t cur;
|
---|
| 1475 | size_t tmp;
|
---|
| 1476 | wchar_t ch;
|
---|
[69df837f] | 1477 |
|
---|
[576845ec] | 1478 | /* Skip over leading delimiters. */
|
---|
[948222e4] | 1479 | tmp = 0;
|
---|
| 1480 | cur = 0;
|
---|
| 1481 | while ((ch = str_decode(s, &tmp, len)) && str_chr(delim, ch))
|
---|
[ee3f6f6] | 1482 | cur = tmp;
|
---|
| 1483 | start = &s[cur];
|
---|
[69df837f] | 1484 |
|
---|
[576845ec] | 1485 | /* Skip over token characters. */
|
---|
[948222e4] | 1486 | tmp = cur;
|
---|
| 1487 | while ((ch = str_decode(s, &tmp, len)) && !str_chr(delim, ch))
|
---|
[ee3f6f6] | 1488 | cur = tmp;
|
---|
| 1489 | end = &s[cur];
|
---|
| 1490 | if (next)
|
---|
| 1491 | *next = (ch ? &s[tmp] : &s[cur]);
|
---|
| 1492 |
|
---|
| 1493 | if (start == end)
|
---|
[576845ec] | 1494 | return NULL; /* No more tokens. */
|
---|
[69df837f] | 1495 |
|
---|
[576845ec] | 1496 | /* Overwrite delimiter with NULL terminator. */
|
---|
| 1497 | *end = '\0';
|
---|
| 1498 | return start;
|
---|
[69df837f] | 1499 | }
|
---|
| 1500 |
|
---|
[e535eeb] | 1501 | void order_suffix(const uint64_t val, uint64_t *rv, char *suffix)
|
---|
| 1502 | {
|
---|
[933cadf] | 1503 | if (val > UINT64_C(10000000000000000000)) {
|
---|
| 1504 | *rv = val / UINT64_C(1000000000000000000);
|
---|
[e535eeb] | 1505 | *suffix = 'Z';
|
---|
[933cadf] | 1506 | } else if (val > UINT64_C(1000000000000000000)) {
|
---|
| 1507 | *rv = val / UINT64_C(1000000000000000);
|
---|
[e535eeb] | 1508 | *suffix = 'E';
|
---|
[933cadf] | 1509 | } else if (val > UINT64_C(1000000000000000)) {
|
---|
| 1510 | *rv = val / UINT64_C(1000000000000);
|
---|
[e535eeb] | 1511 | *suffix = 'T';
|
---|
[933cadf] | 1512 | } else if (val > UINT64_C(1000000000000)) {
|
---|
| 1513 | *rv = val / UINT64_C(1000000000);
|
---|
[e535eeb] | 1514 | *suffix = 'G';
|
---|
[933cadf] | 1515 | } else if (val > UINT64_C(1000000000)) {
|
---|
| 1516 | *rv = val / UINT64_C(1000000);
|
---|
[e535eeb] | 1517 | *suffix = 'M';
|
---|
[933cadf] | 1518 | } else if (val > UINT64_C(1000000)) {
|
---|
| 1519 | *rv = val / UINT64_C(1000);
|
---|
[e535eeb] | 1520 | *suffix = 'k';
|
---|
| 1521 | } else {
|
---|
| 1522 | *rv = val;
|
---|
| 1523 | *suffix = ' ';
|
---|
| 1524 | }
|
---|
| 1525 | }
|
---|
| 1526 |
|
---|
[933cadf] | 1527 | void bin_order_suffix(const uint64_t val, uint64_t *rv, const char **suffix,
|
---|
| 1528 | bool fixed)
|
---|
| 1529 | {
|
---|
| 1530 | if (val > UINT64_C(1152921504606846976)) {
|
---|
| 1531 | *rv = val / UINT64_C(1125899906842624);
|
---|
| 1532 | *suffix = "EiB";
|
---|
| 1533 | } else if (val > UINT64_C(1125899906842624)) {
|
---|
| 1534 | *rv = val / UINT64_C(1099511627776);
|
---|
| 1535 | *suffix = "TiB";
|
---|
| 1536 | } else if (val > UINT64_C(1099511627776)) {
|
---|
| 1537 | *rv = val / UINT64_C(1073741824);
|
---|
| 1538 | *suffix = "GiB";
|
---|
| 1539 | } else if (val > UINT64_C(1073741824)) {
|
---|
| 1540 | *rv = val / UINT64_C(1048576);
|
---|
| 1541 | *suffix = "MiB";
|
---|
| 1542 | } else if (val > UINT64_C(1048576)) {
|
---|
| 1543 | *rv = val / UINT64_C(1024);
|
---|
| 1544 | *suffix = "KiB";
|
---|
| 1545 | } else {
|
---|
| 1546 | *rv = val;
|
---|
| 1547 | if (fixed)
|
---|
| 1548 | *suffix = "B ";
|
---|
| 1549 | else
|
---|
| 1550 | *suffix = "B";
|
---|
| 1551 | }
|
---|
| 1552 | }
|
---|
| 1553 |
|
---|
[a46da63] | 1554 | /** @}
|
---|
[b2951e2] | 1555 | */
|
---|