Subversion Repositories HelenOS

Rev

Rev 4209 | Rev 4223 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
4011 svoboda 1
/*
2
 * Copyright (c) 2001-2004 Jakub Jermar
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
14
 * - The name of the author may not be used to endorse or promote products
15
 *   derived from this software without specific prior written permission.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
28
 
4014 decky 29
/** @addtogroup generic
4011 svoboda 30
 * @{
31
 */
32
 
33
/**
34
 * @file
4209 svoboda 35
 * @brief String functions.
36
 *
37
 * Strings and characters use the Universal Character Set (UCS). The standard
38
 * strings, called just strings are encoded in UTF-8. Wide strings (encoded
39
 * in UTF-32) are supported to a limited degree. A single character is
4212 decky 40
 * represented as wchar_t.@n
4209 svoboda 41
 *
4212 decky 42
 * Overview of the terminology:@n
4209 svoboda 43
 *
4212 decky 44
 *  Term                  Meaning
45
 *  --------------------  ----------------------------------------------------
46
 *  byte                  8 bits stored in uint8_t (unsigned 8 bit integer)
4209 svoboda 47
 *
4212 decky 48
 *  character             UTF-32 encoded Unicode character, stored in wchar_t
49
 *                        (signed 32 bit integer), code points 0 .. 1114111
50
 *                        are valid
4209 svoboda 51
 *
4212 decky 52
 *  ASCII character       7 bit encoded ASCII character, stored in char
53
 *                        (usually signed 8 bit integer), code points 0 .. 127
54
 *                        are valid
4209 svoboda 55
 *
4212 decky 56
 *  string                UTF-8 encoded NULL-terminated Unicode string, char *
57
 *
58
 *  wide string           UTF-32 encoded NULL-terminated Unicode string,
59
 *                        wchar_t *
60
 *
61
 *  [wide] string size    number of BYTES in a [wide] string (excluding
62
 *                        the NULL-terminator), size_t
63
 *
64
 *  [wide] string length  number of CHARACTERS in a [wide] string (excluding
65
 *                        the NULL-terminator), count_t
66
 *
67
 *  [wide] string width   number of display cells on a monospace display taken
68
 *                        by a [wide] string, count_t
69
 *
70
 *
71
 * Overview of string metrics:@n
72
 *
73
 *  Metric  Abbrev.  Type     Meaning
74
 *  ------  ------   ------   -------------------------------------------------
75
 *  size    n        size_t   number of BYTES in a string (excluding the
76
 *                            NULL-terminator)
77
 *
78
 *  length  l        count_t  number of CHARACTERS in a string (excluding the
79
 *                            null terminator)
80
 *
81
 *  width  w         count_t  number of display cells on a monospace display
82
 *                            taken by a string
83
 *
84
 *
85
 * Function naming prefixes:@n
86
 *
87
 *  chr_    operate on characters
88
 *  ascii_  operate on ASCII characters
89
 *  str_    operate on strings
90
 *  wstr_   operate on wide strings
91
 *
92
 *  [w]str_[n|l|w]  operate on a prefix limited by size, length
93
 *                  or width
94
 *
95
 *
96
 * A specific character inside a [wide] string can be referred to by:@n
97
 *
98
 *  pointer (char *, wchar_t *)
99
 *  byte offset (size_t)
100
 *  character index (count_t)
101
 *
4011 svoboda 102
 */
103
 
104
#include <string.h>
105
#include <print.h>
106
#include <cpu.h>
107
#include <arch/asm.h>
108
#include <arch.h>
4208 svoboda 109
#include <errno.h>
4212 decky 110
#include <align.h>
4011 svoboda 111
 
4179 decky 112
char invalch = '?';
113
 
4212 decky 114
/** Byte mask consisting of lowest @n bits (out of 8) */
115
#define LO_MASK_8(n)  ((uint8_t) ((1 << (n)) - 1))
4196 svoboda 116
 
4212 decky 117
/** Byte mask consisting of lowest @n bits (out of 32) */
118
#define LO_MASK_32(n)  ((uint32_t) ((1 << (n)) - 1))
4198 svoboda 119
 
4212 decky 120
/** Byte mask consisting of highest @n bits (out of 8) */
121
#define HI_MASK_8(n)  (~LO_MASK_8(8 - (n)))
4198 svoboda 122
 
4212 decky 123
/** Number of data bits in a UTF-8 continuation byte */
124
#define CONT_BITS  6
4196 svoboda 125
 
4212 decky 126
/** Decode a single character from a string.
4175 decky 127
 *
4212 decky 128
 * Decode a single character from a string of size @a size. Decoding starts
4200 svoboda 129
 * at @a offset and this offset is moved to the beginning of the next
130
 * character. In case of decoding error, offset generally advances at least
4212 decky 131
 * by one. However, offset is never moved beyond size.
4175 decky 132
 *
4212 decky 133
 * @param str    String (not necessarily NULL-terminated).
134
 * @param offset Byte offset in string where to start decoding.
135
 * @param size   Size of the string (in bytes).
4175 decky 136
 *
4212 decky 137
 * @return Value of decoded character, invalch on decoding error or
138
 *         NULL if attempt to decode beyond @a size.
139
 *
4175 decky 140
 */
4212 decky 141
wchar_t str_decode(const char *str, size_t *offset, size_t size)
4175 decky 142
{
4212 decky 143
    if (*offset + 1 > size)
144
        return 0;
145
 
146
    /* First byte read from string */
147
    uint8_t b0 = (uint8_t) str[(*offset)++];
148
 
149
    /* Determine code length */
150
 
151
    unsigned int b0_bits;  /* Data bits in first byte */
152
    unsigned int cbytes;   /* Number of continuation bytes */
153
 
4196 svoboda 154
    if ((b0 & 0x80) == 0) {
155
        /* 0xxxxxxx (Plain ASCII) */
156
        b0_bits = 7;
157
        cbytes = 0;
158
    } else if ((b0 & 0xe0) == 0xc0) {
159
        /* 110xxxxx 10xxxxxx */
160
        b0_bits = 5;
161
        cbytes = 1;
162
    } else if ((b0 & 0xf0) == 0xe0) {
163
        /* 1110xxxx 10xxxxxx 10xxxxxx */
164
        b0_bits = 4;
165
        cbytes = 2;
166
    } else if ((b0 & 0xf8) == 0xf0) {
167
        /* 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */
168
        b0_bits = 3;
169
        cbytes = 3;
170
    } else {
4212 decky 171
        /* 10xxxxxx -- unexpected continuation byte */
4196 svoboda 172
        return invalch;
4179 decky 173
    }
4212 decky 174
 
175
    if (*offset + cbytes > size)
4196 svoboda 176
        return invalch;
4212 decky 177
 
178
    wchar_t ch = b0 & LO_MASK_8(b0_bits);
179
 
180
    /* Decode continuation bytes */
4196 svoboda 181
    while (cbytes > 0) {
4212 decky 182
        uint8_t b = (uint8_t) str[(*offset)++];
183
 
184
        /* Must be 10xxxxxx */
185
        if ((b & 0xc0) != 0x80)
4179 decky 186
            return invalch;
4212 decky 187
 
188
        /* Shift data bits to ch */
4196 svoboda 189
        ch = (ch << CONT_BITS) | (wchar_t) (b & LO_MASK_8(CONT_BITS));
4212 decky 190
        cbytes--;
4179 decky 191
    }
4212 decky 192
 
4196 svoboda 193
    return ch;
4175 decky 194
}
195
 
4200 svoboda 196
/** Encode a single character to string representation.
4011 svoboda 197
 *
4200 svoboda 198
 * Encode a single character to string representation (i.e. UTF-8) and store
199
 * it into a buffer at @a offset. Encoding starts at @a offset and this offset
200
 * is moved to the position where the next character can be written to.
4011 svoboda 201
 *
4212 decky 202
 * @param ch     Input character.
203
 * @param str    Output buffer.
204
 * @param offset Byte offset where to start writing.
205
 * @param size   Size of the output buffer (in bytes).
4179 decky 206
 *
4208 svoboda 207
 * @return EOK if the character was encoded successfully, EOVERFLOW if there
208
 *     was not enough space in the output buffer or EINVAL if the character
209
 *     code was invalid.
4179 decky 210
 */
4212 decky 211
int chr_encode(const wchar_t ch, char *str, size_t *offset, size_t size)
4179 decky 212
{
4212 decky 213
    if (*offset >= size)
4208 svoboda 214
        return EOVERFLOW;
4212 decky 215
 
216
    if (!chr_check(ch))
4208 svoboda 217
        return EINVAL;
4212 decky 218
 
219
    /* Unsigned version of ch (bit operations should only be done
220
       on unsigned types). */
221
    uint32_t cc = (uint32_t) ch;
222
 
223
    /* Determine how many continuation bytes are needed */
224
 
225
    unsigned int b0_bits;  /* Data bits in first byte */
226
    unsigned int cbytes;   /* Number of continuation bytes */
227
 
4198 svoboda 228
    if ((cc & ~LO_MASK_32(7)) == 0) {
229
        b0_bits = 7;
230
        cbytes = 0;
231
    } else if ((cc & ~LO_MASK_32(11)) == 0) {
232
        b0_bits = 5;
233
        cbytes = 1;
234
    } else if ((cc & ~LO_MASK_32(16)) == 0) {
235
        b0_bits = 4;
236
        cbytes = 2;
237
    } else if ((cc & ~LO_MASK_32(21)) == 0) {
238
        b0_bits = 3;
239
        cbytes = 3;
240
    } else {
4212 decky 241
        /* Codes longer than 21 bits are not supported */
4208 svoboda 242
        return EINVAL;
4179 decky 243
    }
4212 decky 244
 
245
    /* Check for available space in buffer */
246
    if (*offset + cbytes >= size)
4208 svoboda 247
        return EOVERFLOW;
4212 decky 248
 
249
    /* Encode continuation bytes */
250
    unsigned int i;
251
    for (i = cbytes; i > 0; i--) {
4200 svoboda 252
        str[*offset + i] = 0x80 | (cc & LO_MASK_32(CONT_BITS));
4198 svoboda 253
        cc = cc >> CONT_BITS;
4179 decky 254
    }
4212 decky 255
 
256
    /* Encode first byte */
4200 svoboda 257
    str[*offset] = (cc & LO_MASK_32(b0_bits)) | HI_MASK_8(8 - b0_bits - 1);
4179 decky 258
 
4212 decky 259
    /* Advance offset */
260
    *offset += cbytes + 1;
261
 
4208 svoboda 262
    return EOK;
4179 decky 263
}
264
 
4212 decky 265
/** Get size of string.
4209 svoboda 266
 *
4212 decky 267
 * Get the number of bytes which are used by the string @a str (excluding the
268
 * NULL-terminator).
269
 *
270
 * @param str String to consider.
271
 *
272
 * @return Number of bytes used by the string
273
 *
4209 svoboda 274
 */
4212 decky 275
size_t str_size(const char *str)
4209 svoboda 276
{
4212 decky 277
    size_t size = 0;
278
 
279
    while (*str++ != 0)
280
        size++;
281
 
282
    return size;
4209 svoboda 283
}
284
 
4212 decky 285
/** Get size of wide string.
4179 decky 286
 *
4212 decky 287
 * Get the number of bytes which are used by the wide string @a str (excluding the
288
 * NULL-terminator).
289
 *
290
 * @param str Wide string to consider.
291
 *
292
 * @return Number of bytes used by the wide string
293
 *
294
 */
295
size_t wstr_size(const wchar_t *str)
296
{
297
    return (wstr_length(str) * sizeof(wchar_t));
298
}
299
 
300
/** Get size of string with length limit.
301
 *
4205 svoboda 302
 * Get the number of bytes which are used by up to @a max_len first
303
 * characters in the string @a str. If @a max_len is greater than
4212 decky 304
 * the length of @a str, the entire string is measured (excluding the
305
 * NULL-terminator).
4179 decky 306
 *
4212 decky 307
 * @param str     String to consider.
308
 * @param max_len Maximum number of characters to measure.
4179 decky 309
 *
4212 decky 310
 * @return Number of bytes used by the characters.
311
 *
4179 decky 312
 */
4205 svoboda 313
size_t str_lsize(const char *str, count_t max_len)
4179 decky 314
{
4205 svoboda 315
    count_t len = 0;
4212 decky 316
    size_t offset = 0;
317
 
318
    while (len < max_len) {
319
        if (str_decode(str, &offset, STR_NO_LIMIT) == 0)
4199 svoboda 320
            break;
4212 decky 321
 
4205 svoboda 322
        len++;
4179 decky 323
    }
4212 decky 324
 
325
    return offset;
4179 decky 326
}
327
 
4212 decky 328
/** Get size of wide string with length limit.
4209 svoboda 329
 *
4212 decky 330
 * Get the number of bytes which are used by up to @a max_len first
331
 * wide characters in the wide string @a str. If @a max_len is greater than
332
 * the length of @a str, the entire wide string is measured (excluding the
333
 * NULL-terminator).
4209 svoboda 334
 *
4212 decky 335
 * @param str     Wide string to consider.
336
 * @param max_len Maximum number of wide characters to measure.
4209 svoboda 337
 *
4212 decky 338
 * @return Number of bytes used by the wide characters.
339
 *
4209 svoboda 340
 */
4212 decky 341
size_t wstr_lsize(const wchar_t *str, count_t max_len)
4209 svoboda 342
{
4212 decky 343
    return (wstr_nlength(str, max_len * sizeof(wchar_t)) * sizeof(wchar_t));
4209 svoboda 344
}
345
 
4212 decky 346
/** Get number of characters in a string.
4209 svoboda 347
 *
4212 decky 348
 * @param str NULL-terminated string.
4209 svoboda 349
 *
4212 decky 350
 * @return Number of characters in string.
4209 svoboda 351
 *
352
 */
4212 decky 353
count_t str_length(const char *str)
4209 svoboda 354
{
4212 decky 355
    count_t len = 0;
356
    size_t offset = 0;
357
 
358
    while (str_decode(str, &offset, STR_NO_LIMIT) != 0)
359
        len++;
360
 
361
    return len;
4209 svoboda 362
}
363
 
4212 decky 364
/** Get number of characters in a wide string.
4179 decky 365
 *
4212 decky 366
 * @param str NULL-terminated wide string.
4179 decky 367
 *
4212 decky 368
 * @return Number of characters in @a str.
369
 *
4179 decky 370
 */
4212 decky 371
count_t wstr_length(const wchar_t *wstr)
4179 decky 372
{
4212 decky 373
    count_t len = 0;
4179 decky 374
 
4212 decky 375
    while (*wstr++ != 0)
376
        len++;
377
 
378
    return len;
4179 decky 379
}
380
 
4212 decky 381
/** Get number of characters in a string with size limit.
4179 decky 382
 *
4212 decky 383
 * @param str  NULL-terminated string.
384
 * @param size Maximum number of bytes to consider.
385
 *
386
 * @return Number of characters in string.
387
 *
4179 decky 388
 */
4212 decky 389
count_t str_nlength(const char *str, size_t size)
4179 decky 390
{
4212 decky 391
    count_t len = 0;
392
    size_t offset = 0;
4179 decky 393
 
4212 decky 394
    while (str_decode(str, &offset, size) != 0)
395
        len++;
396
 
397
    return len;
4179 decky 398
}
399
 
4212 decky 400
/** Get number of characters in a string with size limit.
4179 decky 401
 *
4212 decky 402
 * @param str  NULL-terminated string.
403
 * @param size Maximum number of bytes to consider.
4179 decky 404
 *
4205 svoboda 405
 * @return Number of characters in string.
4212 decky 406
 *
4179 decky 407
 */
4212 decky 408
count_t wstr_nlength(const wchar_t *str, size_t size)
4179 decky 409
{
4205 svoboda 410
    count_t len = 0;
4212 decky 411
    count_t limit = ALIGN_DOWN(size, sizeof(wchar_t));
412
    count_t offset = 0;
413
 
414
    while ((offset < limit) && (*str++ != 0)) {
4205 svoboda 415
        len++;
4212 decky 416
        offset += sizeof(wchar_t);
4179 decky 417
    }
4212 decky 418
 
4205 svoboda 419
    return len;
4011 svoboda 420
}
421
 
4212 decky 422
/** Check whether character is plain ASCII.
4179 decky 423
 *
4212 decky 424
 * @return True if character is plain ASCII.
425
 *
4179 decky 426
 */
4212 decky 427
bool ascii_check(const wchar_t ch)
4179 decky 428
{
4212 decky 429
    if ((ch >= 0) && (ch <= 127))
430
        return true;
431
 
432
    return false;
433
}
4205 svoboda 434
 
4212 decky 435
/** Check whether character is valid
436
 *
437
 * @return True if character is a valid Unicode code point.
438
 *
439
 */
440
bool chr_check(const wchar_t ch)
441
{
442
    if ((ch >= 0) && (ch <= 1114111))
443
        return true;
444
 
445
    return false;
4179 decky 446
}
447
 
4212 decky 448
/** Compare two NULL terminated strings.
4011 svoboda 449
 *
4212 decky 450
 * Do a char-by-char comparison of two NULL-terminated strings.
4011 svoboda 451
 * The strings are considered equal iff they consist of the same
452
 * characters on the minimum of their lengths.
453
 *
4212 decky 454
 * @param s1 First string to compare.
455
 * @param s2 Second string to compare.
4011 svoboda 456
 *
4212 decky 457
 * @return 0 if the strings are equal, -1 if first is smaller,
458
 *         1 if second smaller.
4011 svoboda 459
 *
460
 */
4212 decky 461
int str_cmp(const char *s1, const char *s2)
4011 svoboda 462
{
4212 decky 463
    wchar_t c1;
464
    wchar_t c2;
465
 
466
    size_t off1 = 0;
467
    size_t off2 = 0;
468
 
469
    while ((c1 = str_decode(s1, &off1, STR_NO_LIMIT) != 0)
470
        && (c2 = str_decode(s2, &off2, STR_NO_LIMIT) != 0)) {
471
 
472
        if (off1 != off2)
473
            break;
474
 
475
        if (c1 < c2)
4011 svoboda 476
            return -1;
4212 decky 477
 
478
        if (c1 > c2)
4011 svoboda 479
            return 1;
480
    }
4212 decky 481
 
482
    if ((off1 == off2) && (c1 == c2))
4011 svoboda 483
        return 0;
4014 decky 484
 
4212 decky 485
    if ((c1 == 0) || (off1 < off2))
4011 svoboda 486
        return -1;
4014 decky 487
 
4011 svoboda 488
    return 1;
489
}
490
 
4212 decky 491
/** Compare two NULL terminated strings with length limit.
4011 svoboda 492
 *
4212 decky 493
 * Do a char-by-char comparison of two NULL-terminated strings.
4011 svoboda 494
 * The strings are considered equal iff they consist of the same
4212 decky 495
 * characters on the minimum of their lengths and the length limit.
4011 svoboda 496
 *
4212 decky 497
 * @param s1      First string to compare.
498
 * @param s2      Second string to compare.
499
 * @param max_len Maximum number of characters to consider.
4011 svoboda 500
 *
4212 decky 501
 * @return 0 if the strings are equal, -1 if first is smaller,
502
 *         1 if second smaller.
503
 *
4011 svoboda 504
 */
4212 decky 505
int str_lcmp(const char *s1, const char *s2, count_t max_len)
4011 svoboda 506
{
4212 decky 507
    wchar_t c1 = 0;
508
    wchar_t c2 = 0;
4011 svoboda 509
 
4212 decky 510
    size_t off1 = 0;
511
    size_t off2 = 0;
512
 
513
    count_t len = 0;
514
 
515
    while ((len < max_len)
516
        && ((c1 = str_decode(s1, &off1, STR_NO_LIMIT)) != 0)
517
        && ((c2 = str_decode(s2, &off2, STR_NO_LIMIT)) != 0)) {
518
 
519
        if (off1 != off2)
520
            break;
521
 
522
        if (c1 < c2)
4011 svoboda 523
            return -1;
4014 decky 524
 
4212 decky 525
        if (c1 > c2)
4011 svoboda 526
            return 1;
4212 decky 527
 
528
        len++;
4011 svoboda 529
    }
4014 decky 530
 
4212 decky 531
    if ((off1 == off2) && (len == max_len) && (c1 == c2))
4011 svoboda 532
        return 0;
4014 decky 533
 
4212 decky 534
    if ((c1 == 0) || (off1 < off2))
4011 svoboda 535
        return -1;
4014 decky 536
 
4011 svoboda 537
    return 1;
538
}
539
 
4212 decky 540
/** Copy NULL-terminated string.
541
 *
542
 * Copy source string @a src to destination buffer @a dst.
543
 * No more than @a size bytes are written. NULL-terminator is always
544
 * written after the last succesfully copied character (i.e. if the
545
 * destination buffer is has at least 1 byte, it will be always
546
 * NULL-terminated).
547
 *
548
 * @param src   Source string.
549
 * @param dst   Destination buffer.
550
 * @param count Size of the destination buffer.
551
 *
552
 */
553
void str_ncpy(char *dst, const char *src, size_t size)
554
{
555
    /* No space for the NULL-terminator in the buffer */
556
    if (size == 0)
557
        return;
558
 
559
    wchar_t ch;
560
    size_t str_off = 0;
561
    size_t dst_off = 0;
562
 
563
    while ((ch = str_decode(src, &str_off, STR_NO_LIMIT) != 0)) {
564
        if (chr_encode(ch, dst, &dst_off, size) != EOK)
565
            break;
566
    }
567
 
568
    if (dst_off >= size)
569
        dst[size - 1] = 0;
570
    else
571
        dst[dst_off] = 0;
572
}
4011 svoboda 573
 
4212 decky 574
/** Copy NULL-terminated wide string to string
4011 svoboda 575
 *
4212 decky 576
 * Copy source wide string @a src to destination buffer @a dst.
577
 * No more than @a size bytes are written. NULL-terminator is always
578
 * written after the last succesfully copied character (i.e. if the
579
 * destination buffer is has at least 1 byte, it will be always
580
 * NULL-terminated).
4011 svoboda 581
 *
4212 decky 582
 * @param src   Source wide string.
583
 * @param dst   Destination buffer.
584
 * @param count Size of the destination buffer.
585
 *
4011 svoboda 586
 */
4212 decky 587
void wstr_nstr(char *dst, const wchar_t *src, size_t size)
4011 svoboda 588
{
4212 decky 589
    /* No space for the NULL-terminator in the buffer */
590
    if (size == 0)
591
        return;
4014 decky 592
 
4212 decky 593
    wchar_t ch;
594
    count_t src_idx = 0;
595
    size_t dst_off = 0;
596
 
597
    while ((ch = src[src_idx++]) != 0) {
598
        if (chr_encode(ch, dst, &dst_off, size) != EOK)
599
            break;
4011 svoboda 600
    }
4014 decky 601
 
4212 decky 602
    if (dst_off >= size)
603
        dst[size - 1] = 0;
604
    else
605
        dst[dst_off] = 0;
4011 svoboda 606
}
607
 
4012 svoboda 608
/** Find first occurence of character in string.
609
 *
4212 decky 610
 * @param str String to search.
611
 * @param ch  Character to look for.
4012 svoboda 612
 *
4212 decky 613
 * @return Pointer to character in @a str or NULL if not found.
614
 *
4012 svoboda 615
 */
4212 decky 616
const char *str_chr(const char *str, wchar_t ch)
4012 svoboda 617
{
4212 decky 618
    wchar_t acc;
619
    size_t off = 0;
620
 
621
    while ((acc = str_decode(str, &off, STR_NO_LIMIT) != 0)) {
622
        if (acc == ch)
623
            return (str + off);
4012 svoboda 624
    }
4014 decky 625
 
4012 svoboda 626
    return NULL;
627
}
628
 
4212 decky 629
/** Insert a wide character into a wide string.
630
 *
631
 * Insert a wide character into a wide string at position
632
 * @a pos. The characters after the position are shifted.
633
 *
634
 * @param str     String to insert to.
635
 * @param ch      Character to insert to.
636
 * @param pos     Character index where to insert.
637
 @ @param max_pos Characters in the buffer.
638
 *
639
 * @return True if the insertion was sucessful, false if the position
640
 *         is out of bounds.
641
 *
642
 */
643
bool wstr_linsert(wchar_t *str, wchar_t ch, count_t pos, count_t max_pos)
644
{
645
    count_t len = wstr_length(str);
646
 
647
    if ((pos > len) || (pos + 1 > max_pos))
648
        return false;
649
 
650
    count_t i;
651
    for (i = len; i + 1 > pos; i--)
652
        str[i + 1] = str[i];
653
 
654
    str[pos] = ch;
655
 
656
    return true;
657
}
658
 
659
/** Remove a wide character from a wide string.
660
 *
661
 * Remove a wide character from a wide string at position
662
 * @a pos. The characters after the position are shifted.
663
 *
664
 * @param str String to remove from.
665
 * @param pos Character index to remove.
666
 *
667
 * @return True if the removal was sucessful, false if the position
668
 *         is out of bounds.
669
 *
670
 */
671
bool wstr_remove(wchar_t *str, count_t pos)
672
{
673
    count_t len = wstr_length(str);
674
 
675
    if (pos >= len)
676
        return false;
677
 
678
    count_t i;
679
    for (i = pos + 1; i <= len; i++)
680
        str[i - 1] = str[i];
681
 
682
    return true;
683
}
684
 
4011 svoboda 685
/** @}
686
 */