Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/c/generic/str.c

    re535eeb r63f8966  
    471471 * null-terminated and containing only complete characters.
    472472 *
    473  * @param dest  Destination buffer.
     473 * @param dest   Destination buffer.
    474474 * @param count Size of the destination buffer (must be > 0).
    475475 * @param src   Source string.
     
    477477void str_cpy(char *dest, size_t size, const char *src)
    478478{
     479        wchar_t ch;
     480        size_t src_off;
     481        size_t dest_off;
     482
    479483        /* There must be space for a null terminator in the buffer. */
    480484        assert(size > 0);
    481485       
    482         size_t src_off = 0;
    483         size_t dest_off = 0;
    484        
    485         wchar_t ch;
     486        src_off = 0;
     487        dest_off = 0;
     488
    486489        while ((ch = str_decode(src, &src_off, STR_NO_LIMIT)) != 0) {
    487490                if (chr_encode(ch, dest, &dest_off, size - 1) != EOK)
    488491                        break;
    489492        }
    490        
     493
    491494        dest[dest_off] = '\0';
    492495}
     
    502505 * have to be null-terminated.
    503506 *
    504  * @param dest  Destination buffer.
     507 * @param dest   Destination buffer.
    505508 * @param count Size of the destination buffer (must be > 0).
    506509 * @param src   Source string.
    507  * @param n     Maximum number of bytes to read from @a src.
     510 * @param n     Maximum number of bytes to read from @a src.
    508511 */
    509512void str_ncpy(char *dest, size_t size, const char *src, size_t n)
    510513{
     514        wchar_t ch;
     515        size_t src_off;
     516        size_t dest_off;
     517
    511518        /* There must be space for a null terminator in the buffer. */
    512519        assert(size > 0);
    513520       
    514         size_t src_off = 0;
    515         size_t dest_off = 0;
    516        
    517         wchar_t ch;
     521        src_off = 0;
     522        dest_off = 0;
     523
    518524        while ((ch = str_decode(src, &src_off, n)) != 0) {
    519525                if (chr_encode(ch, dest, &dest_off, size - 1) != EOK)
    520526                        break;
    521527        }
    522        
     528
    523529        dest[dest_off] = '\0';
    524530}
     
    890896}
    891897
    892 /** Duplicate string.
    893  *
    894  * Allocate a new string and copy characters from the source
    895  * string into it. The duplicate string is allocated via sleeping
    896  * malloc(), thus this function can sleep in no memory conditions.
    897  *
    898  * The allocation cannot fail and the return value is always
    899  * a valid pointer. The duplicate string is always a well-formed
    900  * null-terminated UTF-8 string, but it can differ from the source
    901  * string on the byte level.
    902  *
    903  * @param src Source string.
    904  *
    905  * @return Duplicate string.
    906  *
    907  */
    908898char *str_dup(const char *src)
    909899{
    910         size_t size = str_size(src) + 1;
    911         char *dest = (char *) malloc(size);
     900        size_t size = str_size(src);
     901        void *dest = malloc(size + 1);
     902       
    912903        if (dest == NULL)
    913904                return (char *) NULL;
    914905       
    915         str_cpy(dest, size, src);
    916         return dest;
    917 }
    918 
    919 /** Duplicate string with size limit.
    920  *
    921  * Allocate a new string and copy up to @max_size bytes from the source
    922  * string into it. The duplicate string is allocated via sleeping
    923  * malloc(), thus this function can sleep in no memory conditions.
    924  * No more than @max_size + 1 bytes is allocated, but if the size
    925  * occupied by the source string is smaller than @max_size + 1,
    926  * less is allocated.
    927  *
    928  * The allocation cannot fail and the return value is always
    929  * a valid pointer. The duplicate string is always a well-formed
    930  * null-terminated UTF-8 string, but it can differ from the source
    931  * string on the byte level.
    932  *
    933  * @param src Source string.
    934  * @param n   Maximum number of bytes to duplicate.
    935  *
    936  * @return Duplicate string.
    937  *
    938  */
    939 char *str_ndup(const char *src, size_t n)
     906        return (char *) memcpy(dest, src, size + 1);
     907}
     908
     909char *str_ndup(const char *src, size_t max_size)
    940910{
    941911        size_t size = str_size(src);
    942         if (size > n)
    943                 size = n;
     912        if (size > max_size)
     913                size = max_size;
    944914       
    945915        char *dest = (char *) malloc(size + 1);
     916       
    946917        if (dest == NULL)
    947918                return (char *) NULL;
    948919       
    949         str_ncpy(dest, size + 1, src, size);
     920        memcpy(dest, src, size);
     921        dest[size] = 0;
    950922        return dest;
    951923}
     
    1007979}
    1008980
    1009 void order_suffix(const uint64_t val, uint64_t *rv, char *suffix)
    1010 {
    1011         if (val > 10000000000000000000ULL) {
    1012                 *rv = val / 1000000000000000000ULL;
    1013                 *suffix = 'Z';
    1014         } else if (val > 1000000000000000000ULL) {
    1015                 *rv = val / 1000000000000000ULL;
    1016                 *suffix = 'E';
    1017         } else if (val > 1000000000000000ULL) {
    1018                 *rv = val / 1000000000000ULL;
    1019                 *suffix = 'T';
    1020         } else if (val > 1000000000000ULL) {
    1021                 *rv = val / 1000000000ULL;
    1022                 *suffix = 'G';
    1023         } else if (val > 1000000000ULL) {
    1024                 *rv = val / 1000000ULL;
    1025                 *suffix = 'M';
    1026         } else if (val > 1000000ULL) {
    1027                 *rv = val / 1000ULL;
    1028                 *suffix = 'k';
    1029         } else {
    1030                 *rv = val;
    1031                 *suffix = ' ';
    1032         }
    1033 }
    1034 
    1035981/** @}
    1036982 */
Note: See TracChangeset for help on using the changeset viewer.