Ignore:
File:
1 edited

Legend:

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

    r1b20da0 ra35b458  
    8585        if (*offset + 1 > size)
    8686                return 0;
    87        
     87
    8888        /* First byte read from string */
    8989        uint8_t b0 = (uint8_t) str[(*offset)++];
    90        
     90
    9191        /* Determine code length */
    92        
     92
    9393        unsigned int b0_bits;  /* Data bits in first byte */
    9494        unsigned int cbytes;   /* Number of continuation bytes */
    95        
     95
    9696        if ((b0 & 0x80) == 0) {
    9797                /* 0xxxxxxx (Plain ASCII) */
     
    114114                return U_SPECIAL;
    115115        }
    116        
     116
    117117        if (*offset + cbytes > size)
    118118                return U_SPECIAL;
    119        
     119
    120120        wchar_t ch = b0 & LO_MASK_8(b0_bits);
    121        
     121
    122122        /* Decode continuation bytes */
    123123        while (cbytes > 0) {
    124124                uint8_t b = (uint8_t) str[(*offset)++];
    125                
     125
    126126                /* Must be 10xxxxxx */
    127127                if ((b & 0xc0) != 0x80)
    128128                        return U_SPECIAL;
    129                
     129
    130130                /* Shift data bits to ch */
    131131                ch = (ch << CONT_BITS) | (wchar_t) (b & LO_MASK_8(CONT_BITS));
    132132                cbytes--;
    133133        }
    134        
     134
    135135        return ch;
    136136}
     
    155155        if (*offset == 0)
    156156                return 0;
    157        
     157
    158158        size_t processed = 0;
    159159        /* Continue while continuation bytes found */
    160160        while (*offset > 0 && processed < 4) {
    161161                uint8_t b = (uint8_t) str[--(*offset)];
    162                
     162
    163163                if (processed == 0 && (b & 0x80) == 0) {
    164164                        /* 0xxxxxxx (Plain ASCII) */
     
    200200        if (*offset >= size)
    201201                return EOVERFLOW;
    202        
     202
    203203        if (!chr_check(ch))
    204204                return EINVAL;
    205        
     205
    206206        /* Unsigned version of ch (bit operations should only be done
    207207           on unsigned types). */
    208208        uint32_t cc = (uint32_t) ch;
    209        
     209
    210210        /* Determine how many continuation bytes are needed */
    211        
     211
    212212        unsigned int b0_bits;  /* Data bits in first byte */
    213213        unsigned int cbytes;   /* Number of continuation bytes */
    214        
     214
    215215        if ((cc & ~LO_MASK_32(7)) == 0) {
    216216                b0_bits = 7;
     
    229229                return EINVAL;
    230230        }
    231        
     231
    232232        /* Check for available space in buffer */
    233233        if (*offset + cbytes >= size)
    234234                return EOVERFLOW;
    235        
     235
    236236        /* Encode continuation bytes */
    237237        unsigned int i;
     
    240240                cc = cc >> CONT_BITS;
    241241        }
    242        
     242
    243243        /* Encode first byte */
    244244        str[*offset] = (cc & LO_MASK_32(b0_bits)) | HI_MASK_8(8 - b0_bits - 1);
    245        
     245
    246246        /* Advance offset */
    247247        *offset += cbytes + 1;
    248        
     248
    249249        return EOK;
    250250}
     
    263263{
    264264        size_t size = 0;
    265        
     265
    266266        while (*str++ != 0)
    267267                size++;
    268        
     268
    269269        return size;
    270270}
     
    302302        size_t len = 0;
    303303        size_t offset = 0;
    304        
     304
    305305        while (len < max_len) {
    306306                if (str_decode(str, &offset, STR_NO_LIMIT) == 0)
    307307                        break;
    308                
     308
    309309                len++;
    310310        }
    311        
     311
    312312        return offset;
    313313}
     
    327327{
    328328        size_t size = 0;
    329        
     329
    330330        while ((*str++ != 0) && (size < max_size))
    331331                size++;
    332        
     332
    333333        return size;
    334334}
     
    379379        size_t len = 0;
    380380        size_t offset = 0;
    381        
     381
    382382        while (str_decode(str, &offset, STR_NO_LIMIT) != 0)
    383383                len++;
    384        
     384
    385385        return len;
    386386}
     
    396396{
    397397        size_t len = 0;
    398        
     398
    399399        while (*wstr++ != 0)
    400400                len++;
    401        
     401
    402402        return len;
    403403}
     
    415415        size_t len = 0;
    416416        size_t offset = 0;
    417        
     417
    418418        while (str_decode(str, &offset, size) != 0)
    419419                len++;
    420        
     420
    421421        return len;
    422422}
     
    435435        size_t limit = ALIGN_DOWN(size, sizeof(wchar_t));
    436436        size_t offset = 0;
    437        
     437
    438438        while ((offset < limit) && (*str++ != 0)) {
    439439                len++;
    440440                offset += sizeof(wchar_t);
    441441        }
    442        
     442
    443443        return len;
    444444}
     
    464464        size_t offset = 0;
    465465        wchar_t ch;
    466        
     466
    467467        while ((ch = str_decode(str, &offset, STR_NO_LIMIT)) != 0)
    468468                width += chr_width(ch);
    469        
     469
    470470        return width;
    471471}
     
    480480        if (WCHAR_SIGNED_CHECK(ch >= 0) && (ch <= 127))
    481481                return true;
    482        
     482
    483483        return false;
    484484}
     
    493493        if (WCHAR_SIGNED_CHECK(ch >= 0) && (ch <= 1114111))
    494494                return true;
    495        
     495
    496496        return false;
    497497}
     
    666666        wchar_t c1 = 0;
    667667        wchar_t c2 = 0;
    668        
     668
    669669        size_t off1 = 0;
    670670        size_t off2 = 0;
    671        
     671
    672672        size_t len = 0;
    673673
     
    710710        wchar_t c1 = 0;
    711711        wchar_t c2 = 0;
    712        
     712
    713713        size_t off1 = 0;
    714714        size_t off2 = 0;
     
    717717                c1 = str_decode(s, &off1, STR_NO_LIMIT);
    718718                c2 = str_decode(p, &off2, STR_NO_LIMIT);
    719                
     719
    720720                if (c2 == 0)
    721721                        return true;
     
    723723                if (c1 != c2)
    724724                        return false;
    725                
     725
    726726                if (c1 == 0)
    727727                        break;
     
    747747        /* There must be space for a null terminator in the buffer. */
    748748        assert(size > 0);
    749        
     749
    750750        size_t src_off = 0;
    751751        size_t dest_off = 0;
    752        
     752
    753753        wchar_t ch;
    754754        while ((ch = str_decode(src, &src_off, STR_NO_LIMIT)) != 0) {
     
    756756                        break;
    757757        }
    758        
     758
    759759        dest[dest_off] = '\0';
    760760}
     
    780780        /* There must be space for a null terminator in the buffer. */
    781781        assert(size > 0);
    782        
     782
    783783        size_t src_off = 0;
    784784        size_t dest_off = 0;
    785        
     785
    786786        wchar_t ch;
    787787        while ((ch = str_decode(src, &src_off, n)) != 0) {
     
    789789                        break;
    790790        }
    791        
     791
    792792        dest[dest_off] = '\0';
    793793}
     
    811811        if (dstr_size >= size)
    812812                return;
    813        
     813
    814814        str_cpy(dest + dstr_size, size - dstr_size, src);
    815815}
     
    896896        /* There must be space for a null terminator in the buffer. */
    897897        assert(size > 0);
    898        
     898
    899899        src_idx = 0;
    900900        dest_off = 0;
     
    971971
    972972        assert(dlen > 0);
    973        
     973
    974974        while ((c = str_decode(src, &offset, STR_NO_LIMIT)) != 0) {
    975975                if (c > 0x10000) {
     
    11091109{
    11101110        size_t len = str_length(str);
    1111        
     1111
    11121112        wchar_t *wstr = calloc(len+1, sizeof(wchar_t));
    11131113        if (wstr == NULL)
    11141114                return NULL;
    1115        
     1115
    11161116        str_to_wstr(wstr, len + 1, str);
    11171117        return wstr;
     
    11301130        size_t off = 0;
    11311131        size_t last = 0;
    1132        
     1132
    11331133        while ((acc = str_decode(str, &off, STR_NO_LIMIT)) != 0) {
    11341134                if (acc == ch)
     
    11361136                last = off;
    11371137        }
    1138        
     1138
    11391139        return NULL;
    11401140}
     
    12071207        size_t last = 0;
    12081208        const char *res = NULL;
    1209        
     1209
    12101210        while ((acc = str_decode(str, &off, STR_NO_LIMIT)) != 0) {
    12111211                if (acc == ch)
     
    12131213                last = off;
    12141214        }
    1215        
     1215
    12161216        return (char *) res;
    12171217}
     
    12341234{
    12351235        size_t len = wstr_length(str);
    1236        
     1236
    12371237        if ((pos > len) || (pos + 1 > max_pos))
    12381238                return false;
    1239        
     1239
    12401240        size_t i;
    12411241        for (i = len; i + 1 > pos; i--)
    12421242                str[i + 1] = str[i];
    1243        
     1243
    12441244        str[pos] = ch;
    1245        
     1245
    12461246        return true;
    12471247}
     
    12621262{
    12631263        size_t len = wstr_length(str);
    1264        
     1264
    12651265        if (pos >= len)
    12661266                return false;
    1267        
     1267
    12681268        size_t i;
    12691269        for (i = pos + 1; i <= len; i++)
    12701270                str[i - 1] = str[i];
    1271        
     1271
    12721272        return true;
    12731273}
     
    12961296        if (dest == NULL)
    12971297                return (char *) NULL;
    1298        
     1298
    12991299        str_cpy(dest, size, src);
    13001300        return dest;
     
    13261326        if (size > n)
    13271327                size = n;
    1328        
     1328
    13291329        char *dest = (char *) malloc(size + 1);
    13301330        if (dest == NULL)
    13311331                return (char *) NULL;
    1332        
     1332
    13331333        str_ncpy(dest, size + 1, src, size);
    13341334        return dest;
     
    13531353        if (!s)
    13541354                return NULL;
    1355        
     1355
    13561356        size_t len = str_size(s);
    13571357        size_t cur;
     
    13981398        assert(neg != NULL);
    13991399        assert(result != NULL);
    1400        
     1400
    14011401        *neg = false;
    14021402        const char *str = nptr;
    1403        
     1403
    14041404        /* Ignore leading whitespace */
    14051405        while (isspace(*str))
    14061406                str++;
    1407        
     1407
    14081408        if (*str == '-') {
    14091409                *neg = true;
     
    14111411        } else if (*str == '+')
    14121412                str++;
    1413        
     1413
    14141414        if (base == 0) {
    14151415                /* Decode base if not specified */
    14161416                base = 10;
    1417                
     1417
    14181418                if (*str == '0') {
    14191419                        base = 8;
    14201420                        str++;
    1421                        
     1421
    14221422                        switch (*str) {
    14231423                        case 'b':
     
    14541454                }
    14551455        }
    1456        
     1456
    14571457        *result = 0;
    14581458        const char *startstr = str;
    1459        
     1459
    14601460        while (*str != 0) {
    14611461                unsigned int digit;
    1462                
     1462
    14631463                if ((*str >= 'a') && (*str <= 'z'))
    14641464                        digit = *str - 'a' + 10;
     
    14691469                else
    14701470                        break;
    1471                
     1471
    14721472                if (digit >= base)
    14731473                        break;
    1474                
     1474
    14751475                uint64_t prev = *result;
    14761476                *result = (*result) * base + digit;
    1477                
     1477
    14781478                if (*result < prev) {
    14791479                        /* Overflow */
     
    14811481                        return EOVERFLOW;
    14821482                }
    1483                
     1483
    14841484                str++;
    14851485        }
    1486        
     1486
    14871487        if (str == startstr) {
    14881488                /*
     
    14921492                str = nptr;
    14931493        }
    1494        
     1494
    14951495        *endptr = (char *) str;
    1496        
     1496
    14971497        if (str == nptr)
    14981498                return EINVAL;
    1499        
     1499
    15001500        return EOK;
    15011501}
     
    15171517{
    15181518        assert(result != NULL);
    1519        
     1519
    15201520        bool neg;
    15211521        char *lendptr;
    15221522        uint64_t res;
    15231523        errno_t ret = str_uint(nptr, &lendptr, base, &neg, &res);
    1524        
     1524
    15251525        if (endptr != NULL)
    15261526                *endptr = (char *) lendptr;
    1527        
     1527
    15281528        if (ret != EOK)
    15291529                return ret;
    1530        
     1530
    15311531        /* Do not allow negative values */
    15321532        if (neg)
    15331533                return EINVAL;
    1534        
     1534
    15351535        /* Check whether we are at the end of
    15361536           the string in strict mode */
    15371537        if ((strict) && (*lendptr != 0))
    15381538                return EINVAL;
    1539        
     1539
    15401540        /* Check for overflow */
    15411541        uint8_t _res = (uint8_t) res;
    15421542        if (_res != res)
    15431543                return EOVERFLOW;
    1544        
     1544
    15451545        *result = _res;
    1546        
     1546
    15471547        return EOK;
    15481548}
     
    15641564{
    15651565        assert(result != NULL);
    1566        
     1566
    15671567        bool neg;
    15681568        char *lendptr;
    15691569        uint64_t res;
    15701570        errno_t ret = str_uint(nptr, &lendptr, base, &neg, &res);
    1571        
     1571
    15721572        if (endptr != NULL)
    15731573                *endptr = (char *) lendptr;
    1574        
     1574
    15751575        if (ret != EOK)
    15761576                return ret;
    1577        
     1577
    15781578        /* Do not allow negative values */
    15791579        if (neg)
    15801580                return EINVAL;
    1581        
     1581
    15821582        /* Check whether we are at the end of
    15831583           the string in strict mode */
    15841584        if ((strict) && (*lendptr != 0))
    15851585                return EINVAL;
    1586        
     1586
    15871587        /* Check for overflow */
    15881588        uint16_t _res = (uint16_t) res;
    15891589        if (_res != res)
    15901590                return EOVERFLOW;
    1591        
     1591
    15921592        *result = _res;
    1593        
     1593
    15941594        return EOK;
    15951595}
     
    16111611{
    16121612        assert(result != NULL);
    1613        
     1613
    16141614        bool neg;
    16151615        char *lendptr;
    16161616        uint64_t res;
    16171617        errno_t ret = str_uint(nptr, &lendptr, base, &neg, &res);
    1618        
     1618
    16191619        if (endptr != NULL)
    16201620                *endptr = (char *) lendptr;
    1621        
     1621
    16221622        if (ret != EOK)
    16231623                return ret;
    1624        
     1624
    16251625        /* Do not allow negative values */
    16261626        if (neg)
    16271627                return EINVAL;
    1628        
     1628
    16291629        /* Check whether we are at the end of
    16301630           the string in strict mode */
    16311631        if ((strict) && (*lendptr != 0))
    16321632                return EINVAL;
    1633        
     1633
    16341634        /* Check for overflow */
    16351635        uint32_t _res = (uint32_t) res;
    16361636        if (_res != res)
    16371637                return EOVERFLOW;
    1638        
     1638
    16391639        *result = _res;
    1640        
     1640
    16411641        return EOK;
    16421642}
     
    16581658{
    16591659        assert(result != NULL);
    1660        
     1660
    16611661        bool neg;
    16621662        char *lendptr;
    16631663        errno_t ret = str_uint(nptr, &lendptr, base, &neg, result);
    1664        
     1664
    16651665        if (endptr != NULL)
    16661666                *endptr = (char *) lendptr;
    1667        
     1667
    16681668        if (ret != EOK)
    16691669                return ret;
    1670        
     1670
    16711671        /* Do not allow negative values */
    16721672        if (neg)
    16731673                return EINVAL;
    1674        
     1674
    16751675        /* Check whether we are at the end of
    16761676           the string in strict mode */
    16771677        if ((strict) && (*lendptr != 0))
    16781678                return EINVAL;
    1679        
     1679
    16801680        return EOK;
    16811681}
     
    16971697{
    16981698        assert(result != NULL);
    1699        
     1699
    17001700        bool neg;
    17011701        char *lendptr;
    17021702        uint64_t res;
    17031703        errno_t ret = str_uint(nptr, &lendptr, base, &neg, &res);
    1704        
     1704
    17051705        if (endptr != NULL)
    17061706                *endptr = (char *) lendptr;
    1707        
     1707
    17081708        if (ret != EOK)
    17091709                return ret;
    1710        
     1710
    17111711        /* Do not allow negative values */
    17121712        if (neg)
    17131713                return EINVAL;
    1714        
     1714
    17151715        /* Check whether we are at the end of
    17161716           the string in strict mode */
    17171717        if ((strict) && (*lendptr != 0))
    17181718                return EINVAL;
    1719        
     1719
    17201720        /* Check for overflow */
    17211721        size_t _res = (size_t) res;
    17221722        if (_res != res)
    17231723                return EOVERFLOW;
    1724        
     1724
    17251725        *result = _res;
    1726        
     1726
    17271727        return EOK;
    17281728}
Note: See TracChangeset for help on using the changeset viewer.