Subversion Repositories HelenOS

Rev

Rev 4377 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4377 Rev 4692
Line 60... Line 60...
60
 *
60
 *
61
 *  [wide] string size    number of BYTES in a [wide] string (excluding
61
 *  [wide] string size    number of BYTES in a [wide] string (excluding
62
 *                        the NULL-terminator), size_t
62
 *                        the NULL-terminator), size_t
63
 *
63
 *
64
 *  [wide] string length  number of CHARACTERS in a [wide] string (excluding
64
 *  [wide] string length  number of CHARACTERS in a [wide] string (excluding
65
 *                        the NULL-terminator), count_t
65
 *                        the NULL-terminator), size_t
66
 *
66
 *
67
 *  [wide] string width   number of display cells on a monospace display taken
67
 *  [wide] string width   number of display cells on a monospace display taken
68
 *                        by a [wide] string, count_t
68
 *                        by a [wide] string, size_t
69
 *
69
 *
70
 *
70
 *
71
 * Overview of string metrics:@n
71
 * Overview of string metrics:@n
72
 *
72
 *
73
 *  Metric  Abbrev.  Type     Meaning
73
 *  Metric  Abbrev.  Type     Meaning
74
 *  ------  ------   ------   -------------------------------------------------
74
 *  ------  ------   ------   -------------------------------------------------
75
 *  size    n        size_t   number of BYTES in a string (excluding the
75
 *  size    n        size_t   number of BYTES in a string (excluding the
76
 *                            NULL-terminator)
76
 *                            NULL-terminator)
77
 *
77
 *
78
 *  length  l        count_t  number of CHARACTERS in a string (excluding the
78
 *  length  l        size_t   number of CHARACTERS in a string (excluding the
79
 *                            null terminator)
79
 *                            null terminator)
80
 *
80
 *
81
 *  width  w         count_t  number of display cells on a monospace display
81
 *  width  w         size_t   number of display cells on a monospace display
82
 *                            taken by a string
82
 *                            taken by a string
83
 *
83
 *
84
 *
84
 *
85
 * Function naming prefixes:@n
85
 * Function naming prefixes:@n
86
 *
86
 *
Line 95... Line 95...
95
 *
95
 *
96
 * A specific character inside a [wide] string can be referred to by:@n
96
 * A specific character inside a [wide] string can be referred to by:@n
97
 *
97
 *
98
 *  pointer (char *, wchar_t *)
98
 *  pointer (char *, wchar_t *)
99
 *  byte offset (size_t)
99
 *  byte offset (size_t)
100
 *  character index (count_t)
100
 *  character index (size_t)
101
 *
101
 *
102
 */
102
 */
103
 
103
 
104
#include <string.h>
104
#include <string.h>
105
#include <print.h>
105
#include <print.h>
Line 307... Line 307...
307
 * @param max_len Maximum number of characters to measure.
307
 * @param max_len Maximum number of characters to measure.
308
 *
308
 *
309
 * @return Number of bytes used by the characters.
309
 * @return Number of bytes used by the characters.
310
 *
310
 *
311
 */
311
 */
312
size_t str_lsize(const char *str, count_t max_len)
312
size_t str_lsize(const char *str, size_t max_len)
313
{
313
{
314
    count_t len = 0;
314
    size_t len = 0;
315
    size_t offset = 0;
315
    size_t offset = 0;
316
   
316
   
317
    while (len < max_len) {
317
    while (len < max_len) {
318
        if (str_decode(str, &offset, STR_NO_LIMIT) == 0)
318
        if (str_decode(str, &offset, STR_NO_LIMIT) == 0)
319
            break;
319
            break;
Line 335... Line 335...
335
 * @param max_len Maximum number of wide characters to measure.
335
 * @param max_len Maximum number of wide characters to measure.
336
 *
336
 *
337
 * @return Number of bytes used by the wide characters.
337
 * @return Number of bytes used by the wide characters.
338
 *
338
 *
339
 */
339
 */
340
size_t wstr_lsize(const wchar_t *str, count_t max_len)
340
size_t wstr_lsize(const wchar_t *str, size_t max_len)
341
{
341
{
342
    return (wstr_nlength(str, max_len * sizeof(wchar_t)) * sizeof(wchar_t));
342
    return (wstr_nlength(str, max_len * sizeof(wchar_t)) * sizeof(wchar_t));
343
}
343
}
344
 
344
 
345
/** Get number of characters in a string.
345
/** Get number of characters in a string.
Line 347... Line 347...
347
 * @param str NULL-terminated string.
347
 * @param str NULL-terminated string.
348
 *
348
 *
349
 * @return Number of characters in string.
349
 * @return Number of characters in string.
350
 *
350
 *
351
 */
351
 */
352
count_t str_length(const char *str)
352
size_t str_length(const char *str)
353
{
353
{
354
    count_t len = 0;
354
    size_t len = 0;
355
    size_t offset = 0;
355
    size_t offset = 0;
356
   
356
   
357
    while (str_decode(str, &offset, STR_NO_LIMIT) != 0)
357
    while (str_decode(str, &offset, STR_NO_LIMIT) != 0)
358
        len++;
358
        len++;
359
   
359
   
Line 365... Line 365...
365
 * @param str NULL-terminated wide string.
365
 * @param str NULL-terminated wide string.
366
 *
366
 *
367
 * @return Number of characters in @a str.
367
 * @return Number of characters in @a str.
368
 *
368
 *
369
 */
369
 */
370
count_t wstr_length(const wchar_t *wstr)
370
size_t wstr_length(const wchar_t *wstr)
371
{
371
{
372
    count_t len = 0;
372
    size_t len = 0;
373
   
373
   
374
    while (*wstr++ != 0)
374
    while (*wstr++ != 0)
375
        len++;
375
        len++;
376
   
376
   
377
    return len;
377
    return len;
Line 383... Line 383...
383
 * @param size Maximum number of bytes to consider.
383
 * @param size Maximum number of bytes to consider.
384
 *
384
 *
385
 * @return Number of characters in string.
385
 * @return Number of characters in string.
386
 *
386
 *
387
 */
387
 */
388
count_t str_nlength(const char *str, size_t size)
388
size_t str_nlength(const char *str, size_t size)
389
{
389
{
390
    count_t len = 0;
390
    size_t len = 0;
391
    size_t offset = 0;
391
    size_t offset = 0;
392
   
392
   
393
    while (str_decode(str, &offset, size) != 0)
393
    while (str_decode(str, &offset, size) != 0)
394
        len++;
394
        len++;
395
   
395
   
Line 402... Line 402...
402
 * @param size Maximum number of bytes to consider.
402
 * @param size Maximum number of bytes to consider.
403
 *
403
 *
404
 * @return Number of characters in string.
404
 * @return Number of characters in string.
405
 *
405
 *
406
 */
406
 */
407
count_t wstr_nlength(const wchar_t *str, size_t size)
407
size_t wstr_nlength(const wchar_t *str, size_t size)
408
{
408
{
409
    count_t len = 0;
409
    size_t len = 0;
410
    count_t limit = ALIGN_DOWN(size, sizeof(wchar_t));
410
    size_t limit = ALIGN_DOWN(size, sizeof(wchar_t));
411
    count_t offset = 0;
411
    size_t offset = 0;
412
   
412
   
413
    while ((offset < limit) && (*str++ != 0)) {
413
    while ((offset < limit) && (*str++ != 0)) {
414
        len++;
414
        len++;
415
        offset += sizeof(wchar_t);
415
        offset += sizeof(wchar_t);
416
    }
416
    }
Line 494... Line 494...
494
 *
494
 *
495
 * @return 0 if the strings are equal, -1 if first is smaller,
495
 * @return 0 if the strings are equal, -1 if first is smaller,
496
 *         1 if second smaller.
496
 *         1 if second smaller.
497
 *
497
 *
498
 */
498
 */
499
int str_lcmp(const char *s1, const char *s2, count_t max_len)
499
int str_lcmp(const char *s1, const char *s2, size_t max_len)
500
{
500
{
501
    wchar_t c1 = 0;
501
    wchar_t c1 = 0;
502
    wchar_t c2 = 0;
502
    wchar_t c2 = 0;
503
   
503
   
504
    size_t off1 = 0;
504
    size_t off1 = 0;
505
    size_t off2 = 0;
505
    size_t off2 = 0;
506
   
506
   
507
    count_t len = 0;
507
    size_t len = 0;
508
 
508
 
509
    while (true) {
509
    while (true) {
510
        if (len >= max_len)
510
        if (len >= max_len)
511
            break;
511
            break;
512
 
512
 
Line 613... Line 613...
613
    /* No space for the NULL-terminator in the buffer */
613
    /* No space for the NULL-terminator in the buffer */
614
    if (size == 0)
614
    if (size == 0)
615
        return;
615
        return;
616
   
616
   
617
    wchar_t ch;
617
    wchar_t ch;
618
    count_t src_idx = 0;
618
    size_t src_idx = 0;
619
    size_t dst_off = 0;
619
    size_t dst_off = 0;
620
   
620
   
621
    while ((ch = src[src_idx++]) != 0) {
621
    while ((ch = src[src_idx++]) != 0) {
622
        if (chr_encode(ch, dst, &dst_off, size) != EOK)
622
        if (chr_encode(ch, dst, &dst_off, size) != EOK)
623
            break;
623
            break;
Line 635... Line 635...
635
 * @param ch  Character to look for.
635
 * @param ch  Character to look for.
636
 *
636
 *
637
 * @return Pointer to character in @a str or NULL if not found.
637
 * @return Pointer to character in @a str or NULL if not found.
638
 *
638
 *
639
 */
639
 */
640
const char *str_chr(const char *str, wchar_t ch)
640
char *str_chr(const char *str, wchar_t ch)
641
{
641
{
642
    wchar_t acc;
642
    wchar_t acc;
643
    size_t off = 0;
643
    size_t off = 0;
644
    size_t last = 0;
644
    size_t last = 0;
645
   
645
   
646
    while ((acc = str_decode(str, &off, STR_NO_LIMIT)) != 0) {
646
    while ((acc = str_decode(str, &off, STR_NO_LIMIT)) != 0) {
647
        if (acc == ch)
647
        if (acc == ch)
648
            return (str + last);
648
            return (char *) (str + last);
649
        last = off;
649
        last = off;
650
    }
650
    }
651
   
651
   
652
    return NULL;
652
    return NULL;
653
}
653
}
Line 664... Line 664...
664
 *
664
 *
665
 * @return True if the insertion was sucessful, false if the position
665
 * @return True if the insertion was sucessful, false if the position
666
 *         is out of bounds.
666
 *         is out of bounds.
667
 *
667
 *
668
 */
668
 */
669
bool wstr_linsert(wchar_t *str, wchar_t ch, count_t pos, count_t max_pos)
669
bool wstr_linsert(wchar_t *str, wchar_t ch, size_t pos, size_t max_pos)
670
{
670
{
671
    count_t len = wstr_length(str);
671
    size_t len = wstr_length(str);
672
   
672
   
673
    if ((pos > len) || (pos + 1 > max_pos))
673
    if ((pos > len) || (pos + 1 > max_pos))
674
        return false;
674
        return false;
675
   
675
   
676
    count_t i;
676
    size_t i;
677
    for (i = len; i + 1 > pos; i--)
677
    for (i = len; i + 1 > pos; i--)
678
        str[i + 1] = str[i];
678
        str[i + 1] = str[i];
679
   
679
   
680
    str[pos] = ch;
680
    str[pos] = ch;
681
   
681
   
Line 692... Line 692...
692
 *
692
 *
693
 * @return True if the removal was sucessful, false if the position
693
 * @return True if the removal was sucessful, false if the position
694
 *         is out of bounds.
694
 *         is out of bounds.
695
 *
695
 *
696
 */
696
 */
697
bool wstr_remove(wchar_t *str, count_t pos)
697
bool wstr_remove(wchar_t *str, size_t pos)
698
{
698
{
699
    count_t len = wstr_length(str);
699
    size_t len = wstr_length(str);
700
   
700
   
701
    if (pos >= len)
701
    if (pos >= len)
702
        return false;
702
        return false;
703
   
703
   
704
    count_t i;
704
    size_t i;
705
    for (i = pos + 1; i <= len; i++)
705
    for (i = pos + 1; i <= len; i++)
706
        str[i - 1] = str[i];
706
        str[i - 1] = str[i];
707
   
707
   
708
    return true;
708
    return true;
709
}
709
}