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