Changeset d066259 in mainline


Ignore:
Timestamp:
2019-02-05T17:42:58Z (5 years ago)
Author:
Jiří Zárevúcky <zarevucky.jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
08e103d4, bb97118, d80fa05
Parents:
cca2d93b
Message:

Synchronize str.c/str.h across boot/kernel/uspace

Files:
7 edited

Legend:

Unmodified
Added
Removed
  • boot/generic/include/errno.h

    rcca2d93b rd066259  
    4040#define EOVERFLOW  -16  /* The result does not fit its size. */
    4141
     42typedef int errno_t;
     43
    4244#endif
    4345
  • boot/generic/include/str.h

    rcca2d93b rd066259  
    11/*
    2  * Copyright (c) 2010 Martin Decky
     2 * Copyright (c) 2001-2004 Jakub Jermar
     3 * Copyright (c) 2005 Martin Decky
     4 * Copyright (c) 2011 Oleg Romanenko
    35 * All rights reserved.
    46 *
     
    3335#define BOOT_STR_H_
    3436
     37#include <errno.h>
    3538#include <stdbool.h>
    3639#include <stddef.h>
    3740
    38 /**< Common Unicode characters */
    39 #define U_SPECIAL  '?'
     41/* Common Unicode characters */
     42#define U_SPECIAL      '?'
    4043
    41 /**< No size limit constant */
     44/** No size limit constant */
    4245#define STR_NO_LIMIT  ((size_t) -1)
    4346
    44 extern wchar_t str_decode(const char *, size_t *, size_t);
    45 extern int chr_encode(wchar_t, char *, size_t *, size_t);
     47extern wchar_t str_decode(const char *str, size_t *offset, size_t sz);
     48extern errno_t chr_encode(wchar_t ch, char *str, size_t *offset, size_t sz);
    4649
    47 extern size_t str_size(const char *);
    48 extern size_t str_lsize(const char *, size_t);
    49 extern size_t str_length(const char *);
     50extern size_t str_size(const char *str);
     51extern size_t str_lsize(const char *str, size_t max_len);
     52extern size_t str_length(const char *str);
    5053
    51 extern bool ascii_check(wchar_t);
    52 extern bool chr_check(wchar_t);
     54extern bool ascii_check(wchar_t ch);
     55extern bool chr_check(wchar_t ch);
    5356
    54 extern int str_cmp(const char *, const char *);
    55 extern void str_cpy(char *, size_t, const char *);
     57extern int str_cmp(const char *s1, const char *s2);
     58extern void str_cpy(char *dest, size_t size, const char *src);
    5659
    5760#endif
  • boot/generic/src/str.c

    rcca2d93b rd066259  
    11/*
    22 * Copyright (c) 2001-2004 Jakub Jermar
     3 * Copyright (c) 2005 Martin Decky
     4 * Copyright (c) 2008 Jiri Svoboda
     5 * Copyright (c) 2011 Martin Sucha
     6 * Copyright (c) 2011 Oleg Romanenko
    37 * All rights reserved.
    48 *
     
    98102 */
    99103
     104#include <str.h>
     105
    100106#include <errno.h>
    101107#include <stdbool.h>
    102108#include <stddef.h>
    103109#include <stdint.h>
    104 #include <str.h>
    105110
    106111/** Check the condition if wchar_t is signed */
     
    208213 *         code was invalid.
    209214 */
    210 int chr_encode(const wchar_t ch, char *str, size_t *offset, size_t size)
     215errno_t chr_encode(const wchar_t ch, char *str, size_t *offset, size_t size)
    211216{
    212217        if (*offset >= size)
     
    392397                        return 1;
    393398
    394                 if ((c1 == 0) || (c2 == 0))
     399                if (c1 == 0 || c2 == 0)
    395400                        break;
    396401        }
  • kernel/generic/include/str.h

    rcca2d93b rd066259  
    11/*
    22 * Copyright (c) 2001-2004 Jakub Jermar
     3 * Copyright (c) 2005 Martin Decky
     4 * Copyright (c) 2011 Oleg Romanenko
    35 * All rights reserved.
    46 *
     
    3638#define KERN_STR_H_
    3739
     40#include <errno.h>
    3841#include <stdbool.h>
    3942#include <stddef.h>
    4043#include <stdint.h>
    41 #include <errno.h>
    4244
    43 /**< Common Unicode characters */
     45/* Common Unicode characters */
    4446#define U_SPECIAL      '?'
    4547
     
    6163#define U_CURSOR       0x2588
    6264
    63 /**< No size limit constant */
     65/** No size limit constant */
    6466#define STR_NO_LIMIT  ((size_t) -1)
    6567
    66 /**< Maximum size of a string containing cnt characters */
    67 #define STR_BOUNDS(cnt)  ((cnt) << 2)
     68/** Maximum size of a string containing @c length characters */
     69#define STR_BOUNDS(length)  ((length) << 2)
    6870
    6971extern wchar_t str_decode(const char *str, size_t *offset, size_t sz);
     
    9294extern void wstr_to_str(char *dest, size_t size, const wchar_t *src);
    9395
    94 extern char *str_dup(const char *src);
    95 extern char *str_ndup(const char *src, size_t n);
    96 
    9796extern char *str_chr(const char *str, wchar_t ch);
    9897
     
    10099extern bool wstr_remove(wchar_t *str, size_t pos);
    101100
    102 extern errno_t str_uint64_t(const char *, char **, unsigned int, bool, uint64_t *);
     101extern char *str_dup(const char *src);
     102extern char *str_ndup(const char *src, size_t n);
     103
     104extern errno_t str_uint64_t(const char *, char **, unsigned int, bool,
     105    uint64_t *);
    103106
    104107extern void order_suffix(const uint64_t, uint64_t *, char *);
  • kernel/generic/src/lib/str.c

    rcca2d93b rd066259  
    11/*
    22 * Copyright (c) 2001-2004 Jakub Jermar
     3 * Copyright (c) 2005 Martin Decky
     4 * Copyright (c) 2008 Jiri Svoboda
     5 * Copyright (c) 2011 Martin Sucha
     6 * Copyright (c) 2011 Oleg Romanenko
    37 * All rights reserved.
    48 *
     
    103107
    104108#include <str.h>
    105 #include <cpu.h>
    106 #include <arch/asm.h>
    107 #include <arch.h>
     109
     110#include <assert.h>
    108111#include <errno.h>
     112#include <stdbool.h>
     113#include <stddef.h>
     114#include <stdint.h>
     115#include <stdlib.h>
     116
    109117#include <align.h>
    110 #include <assert.h>
    111118#include <macros.h>
    112 #include <stdlib.h>
    113119
    114120/** Check the condition if wchar_t is signed */
     
    616622}
    617623
     624/** Convert wide string to string.
     625 *
     626 * Convert wide string @a src to string. The output is written to the buffer
     627 * specified by @a dest and @a size. @a size must be non-zero and the string
     628 * written will always be well-formed.
     629 *
     630 * @param dest  Destination buffer.
     631 * @param size  Size of the destination buffer.
     632 * @param src   Source wide string.
     633 */
     634void wstr_to_str(char *dest, size_t size, const wchar_t *src)
     635{
     636        wchar_t ch;
     637        size_t src_idx;
     638        size_t dest_off;
     639
     640        /* There must be space for a null terminator in the buffer. */
     641        assert(size > 0);
     642
     643        src_idx = 0;
     644        dest_off = 0;
     645
     646        while ((ch = src[src_idx++]) != 0) {
     647                if (chr_encode(ch, dest, &dest_off, size - 1) != EOK)
     648                        break;
     649        }
     650
     651        dest[dest_off] = '\0';
     652}
     653
     654/** Find first occurence of character in string.
     655 *
     656 * @param str String to search.
     657 * @param ch  Character to look for.
     658 *
     659 * @return Pointer to character in @a str or NULL if not found.
     660 */
     661char *str_chr(const char *str, wchar_t ch)
     662{
     663        wchar_t acc;
     664        size_t off = 0;
     665        size_t last = 0;
     666
     667        while ((acc = str_decode(str, &off, STR_NO_LIMIT)) != 0) {
     668                if (acc == ch)
     669                        return (char *) (str + last);
     670                last = off;
     671        }
     672
     673        return NULL;
     674}
     675
     676/** Insert a wide character into a wide string.
     677 *
     678 * Insert a wide character into a wide string at position
     679 * @a pos. The characters after the position are shifted.
     680 *
     681 * @param str     String to insert to.
     682 * @param ch      Character to insert to.
     683 * @param pos     Character index where to insert.
     684 * @param max_pos Characters in the buffer.
     685 *
     686 * @return True if the insertion was sucessful, false if the position
     687 *         is out of bounds.
     688 *
     689 */
     690bool wstr_linsert(wchar_t *str, wchar_t ch, size_t pos, size_t max_pos)
     691{
     692        size_t len = wstr_length(str);
     693
     694        if ((pos > len) || (pos + 1 > max_pos))
     695                return false;
     696
     697        size_t i;
     698        for (i = len; i + 1 > pos; i--)
     699                str[i + 1] = str[i];
     700
     701        str[pos] = ch;
     702
     703        return true;
     704}
     705
     706/** Remove a wide character from a wide string.
     707 *
     708 * Remove a wide character from a wide string at position
     709 * @a pos. The characters after the position are shifted.
     710 *
     711 * @param str String to remove from.
     712 * @param pos Character index to remove.
     713 *
     714 * @return True if the removal was sucessful, false if the position
     715 *         is out of bounds.
     716 *
     717 */
     718bool wstr_remove(wchar_t *str, size_t pos)
     719{
     720        size_t len = wstr_length(str);
     721
     722        if (pos >= len)
     723                return false;
     724
     725        size_t i;
     726        for (i = pos + 1; i <= len; i++)
     727                str[i - 1] = str[i];
     728
     729        return true;
     730}
     731
    618732/** Duplicate string.
    619733 *
     
    675789        str_ncpy(dest, size + 1, src, size);
    676790        return dest;
    677 }
    678 
    679 /** Convert wide string to string.
    680  *
    681  * Convert wide string @a src to string. The output is written to the buffer
    682  * specified by @a dest and @a size. @a size must be non-zero and the string
    683  * written will always be well-formed.
    684  *
    685  * @param dest  Destination buffer.
    686  * @param size  Size of the destination buffer.
    687  * @param src   Source wide string.
    688  */
    689 void wstr_to_str(char *dest, size_t size, const wchar_t *src)
    690 {
    691         wchar_t ch;
    692         size_t src_idx;
    693         size_t dest_off;
    694 
    695         /* There must be space for a null terminator in the buffer. */
    696         assert(size > 0);
    697 
    698         src_idx = 0;
    699         dest_off = 0;
    700 
    701         while ((ch = src[src_idx++]) != 0) {
    702                 if (chr_encode(ch, dest, &dest_off, size - 1) != EOK)
    703                         break;
    704         }
    705 
    706         dest[dest_off] = '\0';
    707 }
    708 
    709 /** Find first occurence of character in string.
    710  *
    711  * @param str String to search.
    712  * @param ch  Character to look for.
    713  *
    714  * @return Pointer to character in @a str or NULL if not found.
    715  *
    716  */
    717 char *str_chr(const char *str, wchar_t ch)
    718 {
    719         wchar_t acc;
    720         size_t off = 0;
    721         size_t last = 0;
    722 
    723         while ((acc = str_decode(str, &off, STR_NO_LIMIT)) != 0) {
    724                 if (acc == ch)
    725                         return (char *) (str + last);
    726                 last = off;
    727         }
    728 
    729         return NULL;
    730 }
    731 
    732 /** Insert a wide character into a wide string.
    733  *
    734  * Insert a wide character into a wide string at position
    735  * @a pos. The characters after the position are shifted.
    736  *
    737  * @param str     String to insert to.
    738  * @param ch      Character to insert to.
    739  * @param pos     Character index where to insert.
    740  * @param max_pos Characters in the buffer.
    741  *
    742  * @return True if the insertion was sucessful, false if the position
    743  *         is out of bounds.
    744  *
    745  */
    746 bool wstr_linsert(wchar_t *str, wchar_t ch, size_t pos, size_t max_pos)
    747 {
    748         size_t len = wstr_length(str);
    749 
    750         if ((pos > len) || (pos + 1 > max_pos))
    751                 return false;
    752 
    753         size_t i;
    754         for (i = len; i + 1 > pos; i--)
    755                 str[i + 1] = str[i];
    756 
    757         str[pos] = ch;
    758 
    759         return true;
    760 }
    761 
    762 /** Remove a wide character from a wide string.
    763  *
    764  * Remove a wide character from a wide string at position
    765  * @a pos. The characters after the position are shifted.
    766  *
    767  * @param str String to remove from.
    768  * @param pos Character index to remove.
    769  *
    770  * @return True if the removal was sucessful, false if the position
    771  *         is out of bounds.
    772  *
    773  */
    774 bool wstr_remove(wchar_t *str, size_t pos)
    775 {
    776         size_t len = wstr_length(str);
    777 
    778         if (pos >= len)
    779                 return false;
    780 
    781         size_t i;
    782         for (i = pos + 1; i <= len; i++)
    783                 str[i - 1] = str[i];
    784 
    785         return true;
    786791}
    787792
  • uspace/lib/c/generic/str.c

    rcca2d93b rd066259  
    11/*
     2 * Copyright (c) 2001-2004 Jakub Jermar
    23 * Copyright (c) 2005 Martin Decky
    34 * Copyright (c) 2008 Jiri Svoboda
     
    3334 * @{
    3435 */
    35 /** @file
     36
     37/**
     38 * @file
     39 * @brief String functions.
     40 *
     41 * Strings and characters use the Universal Character Set (UCS). The standard
     42 * strings, called just strings are encoded in UTF-8. Wide strings (encoded
     43 * in UTF-32) are supported to a limited degree. A single character is
     44 * represented as wchar_t.@n
     45 *
     46 * Overview of the terminology:@n
     47 *
     48 *  Term                  Meaning
     49 *  --------------------  ----------------------------------------------------
     50 *  byte                  8 bits stored in uint8_t (unsigned 8 bit integer)
     51 *
     52 *  character             UTF-32 encoded Unicode character, stored in wchar_t
     53 *                        (signed 32 bit integer), code points 0 .. 1114111
     54 *                        are valid
     55 *
     56 *  ASCII character       7 bit encoded ASCII character, stored in char
     57 *                        (usually signed 8 bit integer), code points 0 .. 127
     58 *                        are valid
     59 *
     60 *  string                UTF-8 encoded NULL-terminated Unicode string, char *
     61 *
     62 *  wide string           UTF-32 encoded NULL-terminated Unicode string,
     63 *                        wchar_t *
     64 *
     65 *  [wide] string size    number of BYTES in a [wide] string (excluding
     66 *                        the NULL-terminator), size_t
     67 *
     68 *  [wide] string length  number of CHARACTERS in a [wide] string (excluding
     69 *                        the NULL-terminator), size_t
     70 *
     71 *  [wide] string width   number of display cells on a monospace display taken
     72 *                        by a [wide] string, size_t
     73 *
     74 *
     75 * Overview of string metrics:@n
     76 *
     77 *  Metric  Abbrev.  Type     Meaning
     78 *  ------  ------   ------   -------------------------------------------------
     79 *  size    n        size_t   number of BYTES in a string (excluding the
     80 *                            NULL-terminator)
     81 *
     82 *  length  l        size_t   number of CHARACTERS in a string (excluding the
     83 *                            null terminator)
     84 *
     85 *  width  w         size_t   number of display cells on a monospace display
     86 *                            taken by a string
     87 *
     88 *
     89 * Function naming prefixes:@n
     90 *
     91 *  chr_    operate on characters
     92 *  ascii_  operate on ASCII characters
     93 *  str_    operate on strings
     94 *  wstr_   operate on wide strings
     95 *
     96 *  [w]str_[n|l|w]  operate on a prefix limited by size, length
     97 *                  or width
     98 *
     99 *
     100 * A specific character inside a [wide] string can be referred to by:@n
     101 *
     102 *  pointer (char *, wchar_t *)
     103 *  byte offset (size_t)
     104 *  character index (size_t)
     105 *
    36106 */
    37107
    38108#include <str.h>
     109
     110#include <assert.h>
     111#include <ctype.h>
     112#include <errno.h>
     113#include <stdbool.h>
    39114#include <stddef.h>
    40115#include <stdint.h>
    41116#include <stdlib.h>
    42 #include <assert.h>
    43 #include <ctype.h>
    44 #include <errno.h>
     117
    45118#include <align.h>
    46119#include <mem.h>
    47 #include <limits.h>
    48120
    49121/** Check the condition if wchar_t is signed */
     
    747819        /* There must be space for a null terminator in the buffer. */
    748820        assert(size > 0);
     821        assert(src != NULL);
    749822
    750823        size_t src_off = 0;
     
    13111384{
    13121385        size_t size = str_size(src) + 1;
    1313         char *dest = (char *) malloc(size);
    1314         if (dest == NULL)
    1315                 return (char *) NULL;
     1386        char *dest = malloc(size);
     1387        if (!dest)
     1388                return NULL;
    13161389
    13171390        str_cpy(dest, size, src);
     
    13451418                size = n;
    13461419
    1347         char *dest = (char *) malloc(size + 1);
    1348         if (dest == NULL)
    1349                 return (char *) NULL;
     1420        char *dest = malloc(size + 1);
     1421        if (!dest)
     1422                return NULL;
    13501423
    13511424        str_ncpy(dest, size + 1, src, size);
  • uspace/lib/c/include/str.h

    rcca2d93b rd066259  
    11/*
     2 * Copyright (c) 2001-2004 Jakub Jermar
    23 * Copyright (c) 2005 Martin Decky
    34 * Copyright (c) 2011 Oleg Romanenko
     
    4243
    4344#include <errno.h>
    44 #include <mem.h>
     45#include <stdbool.h>
    4546#include <stddef.h>
    4647#include <stdint.h>
    47 #include <stdbool.h>
    4848
    49 #define U_SPECIAL  '?'
     49#include <mem.h>
     50
     51/* Common Unicode characters */
     52#define U_SPECIAL      '?'
    5053
    5154/** No size limit constant */
     
    6366extern wchar_t str_decode(const char *str, size_t *offset, size_t sz);
    6467extern wchar_t str_decode_reverse(const char *str, size_t *offset, size_t sz);
    65 extern errno_t chr_encode(const wchar_t ch, char *str, size_t *offset, size_t sz);
     68extern errno_t chr_encode(wchar_t ch, char *str, size_t *offset, size_t sz);
    6669
    6770extern size_t str_size(const char *str);
     
    116119extern bool wstr_remove(wchar_t *str, size_t pos);
    117120
    118 extern char *str_dup(const char *);
    119 extern char *str_ndup(const char *, size_t max_size);
     121extern char *str_dup(const char *src);
     122extern char *str_ndup(const char *src, size_t n);
    120123
    121124extern char *str_tok(char *, const char *, char **);
Note: See TracChangeset for help on using the changeset viewer.