Subversion Repositories HelenOS

Rev

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

Rev 4377 Rev 4692
Line 136... Line 136...
136
 * @param str    Output buffer.
136
 * @param str    Output buffer.
137
 * @param offset Byte offset where to start writing.
137
 * @param offset Byte offset where to start writing.
138
 * @param size   Size of the output buffer (in bytes).
138
 * @param size   Size of the output buffer (in bytes).
139
 *
139
 *
140
 * @return EOK if the character was encoded successfully, EOVERFLOW if there
140
 * @return EOK if the character was encoded successfully, EOVERFLOW if there
141
 *     was not enough space in the output buffer or EINVAL if the character
141
 *         was not enough space in the output buffer or EINVAL if the character
142
 *     code was invalid.
142
 *         code was invalid.
143
 */
143
 */
144
int chr_encode(const wchar_t ch, char *str, size_t *offset, size_t size)
144
int chr_encode(const wchar_t ch, char *str, size_t *offset, size_t size)
145
{
145
{
146
    if (*offset >= size)
146
    if (*offset >= size)
147
        return EOVERFLOW;
147
        return EOVERFLOW;
Line 241... Line 241...
241
 * @param max_len Maximum number of characters to measure.
241
 * @param max_len Maximum number of characters to measure.
242
 *
242
 *
243
 * @return Number of bytes used by the characters.
243
 * @return Number of bytes used by the characters.
244
 *
244
 *
245
 */
245
 */
246
size_t str_lsize(const char *str, count_t max_len)
246
size_t str_lsize(const char *str, size_t max_len)
247
{
247
{
248
    count_t len = 0;
248
    size_t len = 0;
249
    size_t offset = 0;
249
    size_t offset = 0;
250
   
250
   
251
    while (len < max_len) {
251
    while (len < max_len) {
252
        if (str_decode(str, &offset, STR_NO_LIMIT) == 0)
252
        if (str_decode(str, &offset, STR_NO_LIMIT) == 0)
253
            break;
253
            break;
Line 269... Line 269...
269
 * @param max_len Maximum number of wide characters to measure.
269
 * @param max_len Maximum number of wide characters to measure.
270
 *
270
 *
271
 * @return Number of bytes used by the wide characters.
271
 * @return Number of bytes used by the wide characters.
272
 *
272
 *
273
 */
273
 */
274
size_t wstr_lsize(const wchar_t *str, count_t max_len)
274
size_t wstr_lsize(const wchar_t *str, size_t max_len)
275
{
275
{
276
    return (wstr_nlength(str, max_len * sizeof(wchar_t)) * sizeof(wchar_t));
276
    return (wstr_nlength(str, max_len * sizeof(wchar_t)) * sizeof(wchar_t));
277
}
277
}
278
 
278
 
279
/** Get number of characters in a string.
279
/** Get number of characters in a string.
Line 281... Line 281...
281
 * @param str NULL-terminated string.
281
 * @param str NULL-terminated string.
282
 *
282
 *
283
 * @return Number of characters in string.
283
 * @return Number of characters in string.
284
 *
284
 *
285
 */
285
 */
286
count_t str_length(const char *str)
286
size_t str_length(const char *str)
287
{
287
{
288
    count_t len = 0;
288
    size_t len = 0;
289
    size_t offset = 0;
289
    size_t offset = 0;
290
   
290
   
291
    while (str_decode(str, &offset, STR_NO_LIMIT) != 0)
291
    while (str_decode(str, &offset, STR_NO_LIMIT) != 0)
292
        len++;
292
        len++;
293
   
293
   
Line 299... Line 299...
299
 * @param str NULL-terminated wide string.
299
 * @param str NULL-terminated wide string.
300
 *
300
 *
301
 * @return Number of characters in @a str.
301
 * @return Number of characters in @a str.
302
 *
302
 *
303
 */
303
 */
304
count_t wstr_length(const wchar_t *wstr)
304
size_t wstr_length(const wchar_t *wstr)
305
{
305
{
306
    count_t len = 0;
306
    size_t len = 0;
307
   
307
   
308
    while (*wstr++ != 0)
308
    while (*wstr++ != 0)
309
        len++;
309
        len++;
310
   
310
   
311
    return len;
311
    return len;
Line 317... Line 317...
317
 * @param size Maximum number of bytes to consider.
317
 * @param size Maximum number of bytes to consider.
318
 *
318
 *
319
 * @return Number of characters in string.
319
 * @return Number of characters in string.
320
 *
320
 *
321
 */
321
 */
322
count_t str_nlength(const char *str, size_t size)
322
size_t str_nlength(const char *str, size_t size)
323
{
323
{
324
    count_t len = 0;
324
    size_t len = 0;
325
    size_t offset = 0;
325
    size_t offset = 0;
326
   
326
   
327
    while (str_decode(str, &offset, size) != 0)
327
    while (str_decode(str, &offset, size) != 0)
328
        len++;
328
        len++;
329
   
329
   
Line 336... Line 336...
336
 * @param size Maximum number of bytes to consider.
336
 * @param size Maximum number of bytes to consider.
337
 *
337
 *
338
 * @return Number of characters in string.
338
 * @return Number of characters in string.
339
 *
339
 *
340
 */
340
 */
341
count_t wstr_nlength(const wchar_t *str, size_t size)
341
size_t wstr_nlength(const wchar_t *str, size_t size)
342
{
342
{
343
    count_t len = 0;
343
    size_t len = 0;
344
    count_t limit = ALIGN_DOWN(size, sizeof(wchar_t));
344
    size_t limit = ALIGN_DOWN(size, sizeof(wchar_t));
345
    count_t offset = 0;
345
    size_t offset = 0;
346
   
346
   
347
    while ((offset < limit) && (*str++ != 0)) {
347
    while ((offset < limit) && (*str++ != 0)) {
348
        len++;
348
        len++;
349
        offset += sizeof(wchar_t);
349
        offset += sizeof(wchar_t);
350
    }
350
    }
Line 428... Line 428...
428
 *
428
 *
429
 * @return 0 if the strings are equal, -1 if first is smaller,
429
 * @return 0 if the strings are equal, -1 if first is smaller,
430
 *         1 if second smaller.
430
 *         1 if second smaller.
431
 *
431
 *
432
 */
432
 */
433
int str_lcmp(const char *s1, const char *s2, count_t max_len)
433
int str_lcmp(const char *s1, const char *s2, size_t max_len)
434
{
434
{
435
    wchar_t c1 = 0;
435
    wchar_t c1 = 0;
436
    wchar_t c2 = 0;
436
    wchar_t c2 = 0;
437
   
437
   
438
    size_t off1 = 0;
438
    size_t off1 = 0;
439
    size_t off2 = 0;
439
    size_t off2 = 0;
440
   
440
   
441
    count_t len = 0;
441
    size_t len = 0;
442
 
442
 
443
    while (true) {
443
    while (true) {
444
        if (len >= max_len)
444
        if (len >= max_len)
445
            break;
445
            break;
446
 
446
 
Line 566... Line 566...
566
    /* No space for the NULL-terminator in the buffer */
566
    /* No space for the NULL-terminator in the buffer */
567
    if (size == 0)
567
    if (size == 0)
568
        return;
568
        return;
569
   
569
   
570
    wchar_t ch;
570
    wchar_t ch;
571
    count_t src_idx = 0;
571
    size_t src_idx = 0;
572
    size_t dst_off = 0;
572
    size_t dst_off = 0;
573
   
573
   
574
    while ((ch = src[src_idx++]) != 0) {
574
    while ((ch = src[src_idx++]) != 0) {
575
        if (chr_encode(ch, dst, &dst_off, size) != EOK)
575
        if (chr_encode(ch, dst, &dst_off, size) != EOK)
576
            break;
576
            break;
Line 587... Line 587...
587
 * @param str String to search.
587
 * @param str String to search.
588
 * @param ch  Character to look for.
588
 * @param ch  Character to look for.
589
 *
589
 *
590
 * @return Pointer to character in @a str or NULL if not found.
590
 * @return Pointer to character in @a str or NULL if not found.
591
 */
591
 */
592
const char *str_chr(const char *str, wchar_t ch)
592
char *str_chr(const char *str, wchar_t ch)
593
{
593
{
594
    wchar_t acc;
594
    wchar_t acc;
595
    size_t off = 0;
595
    size_t off = 0;
596
    size_t last = 0;
596
    size_t last = 0;
597
   
597
   
598
    while ((acc = str_decode(str, &off, STR_NO_LIMIT)) != 0) {
598
    while ((acc = str_decode(str, &off, STR_NO_LIMIT)) != 0) {
599
        if (acc == ch)
599
        if (acc == ch)
600
            return (str + last);
600
            return (char *) (str + last);
601
        last = off;
601
        last = off;
602
    }
602
    }
603
   
603
   
604
    return NULL;
604
    return NULL;
605
}
605
}
Line 609... Line 609...
609
 * @param str String to search.
609
 * @param str String to search.
610
 * @param ch  Character to look for.
610
 * @param ch  Character to look for.
611
 *
611
 *
612
 * @return Pointer to character in @a str or NULL if not found.
612
 * @return Pointer to character in @a str or NULL if not found.
613
 */
613
 */
614
const char *str_rchr(const char *str, wchar_t ch)
614
char *str_rchr(const char *str, wchar_t ch)
615
{
615
{
616
    wchar_t acc;
616
    wchar_t acc;
617
    size_t off = 0;
617
    size_t off = 0;
618
    size_t last = 0;
618
    size_t last = 0;
619
    char *res = NULL;
619
    const char *res = NULL;
620
   
620
   
621
    while ((acc = str_decode(str, &off, STR_NO_LIMIT)) != 0) {
621
    while ((acc = str_decode(str, &off, STR_NO_LIMIT)) != 0) {
622
        if (acc == ch)
622
        if (acc == ch)
623
            res = (str + last);
623
            res = (str + last);
624
        last = off;
624
        last = off;
625
    }
625
    }
626
   
626
   
627
    return res;
627
    return (char *) res;
628
}
628
}
629
 
629
 
630
/** Insert a wide character into a wide string.
630
/** Insert a wide character into a wide string.
631
 *
631
 *
632
 * Insert a wide character into a wide string at position
632
 * Insert a wide character into a wide string at position
Line 639... Line 639...
639
 *
639
 *
640
 * @return True if the insertion was sucessful, false if the position
640
 * @return True if the insertion was sucessful, false if the position
641
 *         is out of bounds.
641
 *         is out of bounds.
642
 *
642
 *
643
 */
643
 */
644
bool wstr_linsert(wchar_t *str, wchar_t ch, count_t pos, count_t max_pos)
644
bool wstr_linsert(wchar_t *str, wchar_t ch, size_t pos, size_t max_pos)
645
{
645
{
646
    count_t len = wstr_length(str);
646
    size_t len = wstr_length(str);
647
   
647
   
648
    if ((pos > len) || (pos + 1 > max_pos))
648
    if ((pos > len) || (pos + 1 > max_pos))
649
        return false;
649
        return false;
650
   
650
   
651
    count_t i;
651
    size_t i;
652
    for (i = len; i + 1 > pos; i--)
652
    for (i = len; i + 1 > pos; i--)
653
        str[i + 1] = str[i];
653
        str[i + 1] = str[i];
654
   
654
   
655
    str[pos] = ch;
655
    str[pos] = ch;
656
   
656
   
Line 667... Line 667...
667
 *
667
 *
668
 * @return True if the removal was sucessful, false if the position
668
 * @return True if the removal was sucessful, false if the position
669
 *         is out of bounds.
669
 *         is out of bounds.
670
 *
670
 *
671
 */
671
 */
672
bool wstr_remove(wchar_t *str, count_t pos)
672
bool wstr_remove(wchar_t *str, size_t pos)
673
{
673
{
674
    count_t len = wstr_length(str);
674
    size_t len = wstr_length(str);
675
   
675
   
676
    if (pos >= len)
676
    if (pos >= len)
677
        return false;
677
        return false;
678
   
678
   
679
    count_t i;
679
    size_t i;
680
    for (i = pos + 1; i <= len; i++)
680
    for (i = pos + 1; i <= len; i++)
681
        str[i - 1] = str[i];
681
        str[i - 1] = str[i];
682
   
682
   
683
    return true;
683
    return true;
684
}
684
}