Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • kernel/generic/src/lib/str.c

    r1b20da0 ra35b458  
    151151        if (*offset + 1 > size)
    152152                return 0;
    153        
     153
    154154        /* First byte read from string */
    155155        uint8_t b0 = (uint8_t) str[(*offset)++];
    156        
     156
    157157        /* Determine code length */
    158        
     158
    159159        unsigned int b0_bits;  /* Data bits in first byte */
    160160        unsigned int cbytes;   /* Number of continuation bytes */
    161        
     161
    162162        if ((b0 & 0x80) == 0) {
    163163                /* 0xxxxxxx (Plain ASCII) */
     
    180180                return U_SPECIAL;
    181181        }
    182        
     182
    183183        if (*offset + cbytes > size)
    184184                return U_SPECIAL;
    185        
     185
    186186        wchar_t ch = b0 & LO_MASK_8(b0_bits);
    187        
     187
    188188        /* Decode continuation bytes */
    189189        while (cbytes > 0) {
    190190                uint8_t b = (uint8_t) str[(*offset)++];
    191                
     191
    192192                /* Must be 10xxxxxx */
    193193                if ((b & 0xc0) != 0x80)
    194194                        return U_SPECIAL;
    195                
     195
    196196                /* Shift data bits to ch */
    197197                ch = (ch << CONT_BITS) | (wchar_t) (b & LO_MASK_8(CONT_BITS));
    198198                cbytes--;
    199199        }
    200        
     200
    201201        return ch;
    202202}
     
    221221        if (*offset >= size)
    222222                return EOVERFLOW;
    223        
     223
    224224        if (!chr_check(ch))
    225225                return EINVAL;
    226        
     226
    227227        /* Unsigned version of ch (bit operations should only be done
    228228           on unsigned types). */
    229229        uint32_t cc = (uint32_t) ch;
    230        
     230
    231231        /* Determine how many continuation bytes are needed */
    232        
     232
    233233        unsigned int b0_bits;  /* Data bits in first byte */
    234234        unsigned int cbytes;   /* Number of continuation bytes */
    235        
     235
    236236        if ((cc & ~LO_MASK_32(7)) == 0) {
    237237                b0_bits = 7;
     
    250250                return EINVAL;
    251251        }
    252        
     252
    253253        /* Check for available space in buffer */
    254254        if (*offset + cbytes >= size)
    255255                return EOVERFLOW;
    256        
     256
    257257        /* Encode continuation bytes */
    258258        unsigned int i;
     
    261261                cc = cc >> CONT_BITS;
    262262        }
    263        
     263
    264264        /* Encode first byte */
    265265        str[*offset] = (cc & LO_MASK_32(b0_bits)) | HI_MASK_8(8 - b0_bits - 1);
    266        
     266
    267267        /* Advance offset */
    268268        *offset += cbytes + 1;
    269        
     269
    270270        return EOK;
    271271}
     
    284284{
    285285        size_t size = 0;
    286        
     286
    287287        while (*str++ != 0)
    288288                size++;
    289        
     289
    290290        return size;
    291291}
     
    323323        size_t len = 0;
    324324        size_t offset = 0;
    325        
     325
    326326        while (len < max_len) {
    327327                if (str_decode(str, &offset, STR_NO_LIMIT) == 0)
    328328                        break;
    329                
     329
    330330                len++;
    331331        }
    332        
     332
    333333        return offset;
    334334}
     
    363363        size_t len = 0;
    364364        size_t offset = 0;
    365        
     365
    366366        while (str_decode(str, &offset, STR_NO_LIMIT) != 0)
    367367                len++;
    368        
     368
    369369        return len;
    370370}
     
    380380{
    381381        size_t len = 0;
    382        
     382
    383383        while (*wstr++ != 0)
    384384                len++;
    385        
     385
    386386        return len;
    387387}
     
    399399        size_t len = 0;
    400400        size_t offset = 0;
    401        
     401
    402402        while (str_decode(str, &offset, size) != 0)
    403403                len++;
    404        
     404
    405405        return len;
    406406}
     
    419419        size_t limit = ALIGN_DOWN(size, sizeof(wchar_t));
    420420        size_t offset = 0;
    421        
     421
    422422        while ((offset < limit) && (*str++ != 0)) {
    423423                len++;
    424424                offset += sizeof(wchar_t);
    425425        }
    426        
     426
    427427        return len;
    428428}
     
    437437        if (WCHAR_SIGNED_CHECK(ch >= 0) && (ch <= 127))
    438438                return true;
    439        
     439
    440440        return false;
    441441}
     
    450450        if (WCHAR_SIGNED_CHECK(ch >= 0) && (ch <= 1114111))
    451451                return true;
    452        
     452
    453453        return false;
    454454}
     
    476476        wchar_t c1 = 0;
    477477        wchar_t c2 = 0;
    478        
     478
    479479        size_t off1 = 0;
    480480        size_t off2 = 0;
     
    486486                if (c1 < c2)
    487487                        return -1;
    488                
     488
    489489                if (c1 > c2)
    490490                        return 1;
     
    523523        wchar_t c1 = 0;
    524524        wchar_t c2 = 0;
    525        
     525
    526526        size_t off1 = 0;
    527527        size_t off2 = 0;
    528        
     528
    529529        size_t len = 0;
    530530
     
    569569        assert(size > 0);
    570570        assert(src != NULL);
    571        
     571
    572572        size_t src_off = 0;
    573573        size_t dest_off = 0;
    574        
     574
    575575        wchar_t ch;
    576576        while ((ch = str_decode(src, &src_off, STR_NO_LIMIT)) != 0) {
     
    578578                        break;
    579579        }
    580        
     580
    581581        dest[dest_off] = '\0';
    582582}
     
    602602        /* There must be space for a null terminator in the buffer. */
    603603        assert(size > 0);
    604        
     604
    605605        size_t src_off = 0;
    606606        size_t dest_off = 0;
    607        
     607
    608608        wchar_t ch;
    609609        while ((ch = str_decode(src, &src_off, n)) != 0) {
     
    611611                        break;
    612612        }
    613        
     613
    614614        dest[dest_off] = '\0';
    615615}
     
    636636        char *dest = malloc(size, 0);
    637637        assert(dest);
    638        
     638
    639639        str_cpy(dest, size, src);
    640640        return dest;
     
    666666        if (size > n)
    667667                size = n;
    668        
     668
    669669        char *dest = malloc(size + 1, 0);
    670670        assert(dest);
    671        
     671
    672672        str_ncpy(dest, size + 1, src, size);
    673673        return dest;
     
    695695        src_idx = 0;
    696696        dest_off = 0;
    697        
     697
    698698        while ((ch = src[src_idx++]) != 0) {
    699699                if (chr_encode(ch, dest, &dest_off, size - 1) != EOK)
     
    717717        size_t off = 0;
    718718        size_t last = 0;
    719        
     719
    720720        while ((acc = str_decode(str, &off, STR_NO_LIMIT)) != 0) {
    721721                if (acc == ch)
     
    723723                last = off;
    724724        }
    725        
     725
    726726        return NULL;
    727727}
     
    744744{
    745745        size_t len = wstr_length(str);
    746        
     746
    747747        if ((pos > len) || (pos + 1 > max_pos))
    748748                return false;
    749        
     749
    750750        size_t i;
    751751        for (i = len; i + 1 > pos; i--)
    752752                str[i + 1] = str[i];
    753        
     753
    754754        str[pos] = ch;
    755        
     755
    756756        return true;
    757757}
     
    772772{
    773773        size_t len = wstr_length(str);
    774        
     774
    775775        if (pos >= len)
    776776                return false;
    777        
     777
    778778        size_t i;
    779779        for (i = pos + 1; i <= len; i++)
    780780                str[i - 1] = str[i];
    781        
     781
    782782        return true;
    783783}
     
    800800        assert(neg != NULL);
    801801        assert(result != NULL);
    802        
     802
    803803        *neg = false;
    804804        const char *str = nptr;
    805        
     805
    806806        /* Ignore leading whitespace */
    807807        while (isspace(*str))
    808808                str++;
    809        
     809
    810810        if (*str == '-') {
    811811                *neg = true;
     
    813813        } else if (*str == '+')
    814814                str++;
    815        
     815
    816816        if (base == 0) {
    817817                /* Decode base if not specified */
    818818                base = 10;
    819                
     819
    820820                if (*str == '0') {
    821821                        base = 8;
    822822                        str++;
    823                        
     823
    824824                        switch (*str) {
    825825                        case 'b':
     
    856856                }
    857857        }
    858        
     858
    859859        *result = 0;
    860860        const char *startstr = str;
    861        
     861
    862862        while (*str != 0) {
    863863                unsigned int digit;
    864                
     864
    865865                if ((*str >= 'a') && (*str <= 'z'))
    866866                        digit = *str - 'a' + 10;
     
    871871                else
    872872                        break;
    873                
     873
    874874                if (digit >= base)
    875875                        break;
    876                
     876
    877877                uint64_t prev = *result;
    878878                *result = (*result) * base + digit;
    879                
     879
    880880                if (*result < prev) {
    881881                        /* Overflow */
     
    883883                        return EOVERFLOW;
    884884                }
    885                
     885
    886886                str++;
    887887        }
    888        
     888
    889889        if (str == startstr) {
    890890                /*
     
    894894                str = nptr;
    895895        }
    896        
     896
    897897        *endptr = (char *) str;
    898        
     898
    899899        if (str == nptr)
    900900                return EINVAL;
    901        
     901
    902902        return EOK;
    903903}
     
    919919{
    920920        assert(result != NULL);
    921        
     921
    922922        bool neg;
    923923        char *lendptr;
    924924        errno_t ret = str_uint(nptr, &lendptr, base, &neg, result);
    925        
     925
    926926        if (endptr != NULL)
    927927                *endptr = (char *) lendptr;
    928        
     928
    929929        if (ret != EOK)
    930930                return ret;
    931        
     931
    932932        /* Do not allow negative values */
    933933        if (neg)
    934934                return EINVAL;
    935        
     935
    936936        /* Check whether we are at the end of
    937937           the string in strict mode */
    938938        if ((strict) && (*lendptr != 0))
    939939                return EINVAL;
    940        
     940
    941941        return EOK;
    942942}
Note: See TracChangeset for help on using the changeset viewer.