Ignore:
File:
1 edited

Legend:

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

    r1558d85 rb49d872  
    4545#include <align.h>
    4646#include <mem.h>
     47#include <str.h>
    4748
    4849/** Check the condition if wchar_t is signed */
     
    518519        wchar_t c1 = 0;
    519520        wchar_t c2 = 0;
    520 
     521       
    521522        size_t off1 = 0;
    522523        size_t off2 = 0;
     
    528529                if (c1 < c2)
    529530                        return -1;
    530 
     531               
    531532                if (c1 > c2)
    532533                        return 1;
    533534
    534535                if (c1 == 0 || c2 == 0)
    535                         break;
     536                        break;         
    536537        }
    537538
     
    565566        wchar_t c1 = 0;
    566567        wchar_t c2 = 0;
    567 
     568       
    568569        size_t off1 = 0;
    569570        size_t off2 = 0;
    570 
     571       
    571572        size_t len = 0;
    572573
     
    577578                c1 = str_decode(s1, &off1, STR_NO_LIMIT);
    578579                c2 = str_decode(s2, &off2, STR_NO_LIMIT);
    579 
    580                 if (c1 < c2)
    581                         return -1;
    582 
    583                 if (c1 > c2)
    584                         return 1;
    585 
    586                 if (c1 == 0 || c2 == 0)
    587                         break;
    588 
    589                 ++len;
    590         }
    591 
    592         return 0;
    593 
    594 }
    595 
    596 /** Compare two NULL terminated strings in case-insensitive manner.
    597  *
    598  * Do a char-by-char comparison of two NULL-terminated strings.
    599  * The strings are considered equal iff their length is equal
    600  * and both strings consist of the same sequence of characters
    601  * when converted to lower case.
    602  *
    603  * A string S1 is less than another string S2 if it has a character with
    604  * lower value at the first character position where the strings differ.
    605  * If the strings differ in length, the shorter one is treated as if
    606  * padded by characters with a value of zero.
    607  *
    608  * @param s1 First string to compare.
    609  * @param s2 Second string to compare.
    610  *
    611  * @return 0 if the strings are equal, -1 if the first is less than the second,
    612  *         1 if the second is less than the first.
    613  *
    614  */
    615 int str_casecmp(const char *s1, const char *s2)
    616 {
    617         wchar_t c1 = 0;
    618         wchar_t c2 = 0;
    619 
    620         size_t off1 = 0;
    621         size_t off2 = 0;
    622 
    623         while (true) {
    624                 c1 = tolower(str_decode(s1, &off1, STR_NO_LIMIT));
    625                 c2 = tolower(str_decode(s2, &off2, STR_NO_LIMIT));
    626 
    627                 if (c1 < c2)
    628                         return -1;
    629 
    630                 if (c1 > c2)
    631                         return 1;
    632 
    633                 if (c1 == 0 || c2 == 0)
    634                         break;
    635         }
    636 
    637         return 0;
    638 }
    639 
    640 /** Compare two NULL terminated strings with length limit in case-insensitive
    641  * manner.
    642  *
    643  * Do a char-by-char comparison of two NULL-terminated strings.
    644  * The strings are considered equal iff
    645  * min(str_length(s1), max_len) == min(str_length(s2), max_len)
    646  * and both strings consist of the same sequence of characters,
    647  * up to max_len characters.
    648  *
    649  * A string S1 is less than another string S2 if it has a character with
    650  * lower value at the first character position where the strings differ.
    651  * If the strings differ in length, the shorter one is treated as if
    652  * padded by characters with a value of zero. Only the first max_len
    653  * characters are considered.
    654  *
    655  * @param s1      First string to compare.
    656  * @param s2      Second string to compare.
    657  * @param max_len Maximum number of characters to consider.
    658  *
    659  * @return 0 if the strings are equal, -1 if the first is less than the second,
    660  *         1 if the second is less than the first.
    661  *
    662  */
    663 int str_lcasecmp(const char *s1, const char *s2, size_t max_len)
    664 {
    665         wchar_t c1 = 0;
    666         wchar_t c2 = 0;
    667        
    668         size_t off1 = 0;
    669         size_t off2 = 0;
    670        
    671         size_t len = 0;
    672 
    673         while (true) {
    674                 if (len >= max_len)
    675                         break;
    676 
    677                 c1 = tolower(str_decode(s1, &off1, STR_NO_LIMIT));
    678                 c2 = tolower(str_decode(s2, &off2, STR_NO_LIMIT));
    679580
    680581                if (c1 < c2)
     
    14591360}
    14601361
    1461 /** Split string by delimiters.
    1462  *
    1463  * @param s             String to be tokenized. May not be NULL.
    1464  * @param delim         String with the delimiters.
    1465  * @param next          Variable which will receive the pointer to the
    1466  *                      continuation of the string following the first
    1467  *                      occurrence of any of the delimiter characters.
    1468  *                      May be NULL.
    1469  * @return              Pointer to the prefix of @a s before the first
    1470  *                      delimiter character. NULL if no such prefix
    1471  *                      exists.
    1472  */
    1473 char *str_tok(char *s, const char *delim, char **next)
     1362char *strtok(char *s, const char *delim)
     1363{
     1364        static char *next;
     1365
     1366        return strtok_r(s, delim, &next);
     1367}
     1368
     1369char *strtok_r(char *s, const char *delim, char **next)
    14741370{
    14751371        char *start, *end;
    14761372
    1477         if (!s)
    1478                 return NULL;
    1479        
    1480         size_t len = str_size(s);
    1481         size_t cur;
    1482         size_t tmp;
    1483         wchar_t ch;
     1373        if (s == NULL)
     1374                s = *next;
    14841375
    14851376        /* Skip over leading delimiters. */
    1486         for (tmp = cur = 0;
    1487             (ch = str_decode(s, &tmp, len)) && str_chr(delim, ch); /**/)
    1488                 cur = tmp;
    1489         start = &s[cur];
     1377        while (*s && (str_chr(delim, *s) != NULL)) ++s;
     1378        start = s;
    14901379
    14911380        /* Skip over token characters. */
    1492         for (tmp = cur;
    1493             (ch = str_decode(s, &tmp, len)) && !str_chr(delim, ch); /**/)
    1494                 cur = tmp;
    1495         end = &s[cur];
    1496         if (next)
    1497                 *next = (ch ? &s[tmp] : &s[cur]);
    1498 
    1499         if (start == end)
     1381        while (*s && (str_chr(delim, *s) == NULL)) ++s;
     1382        end = s;
     1383        *next = (*s ? s + 1 : s);
     1384
     1385        if (start == end) {
    15001386                return NULL;    /* No more tokens. */
     1387        }
    15011388
    15021389        /* Overwrite delimiter with NULL terminator. */
Note: See TracChangeset for help on using the changeset viewer.