Fork us on GitHub Follow us on Facebook Follow us on Twitter

Changeset dce39b4 in mainline


Ignore:
Timestamp:
2012-07-29T23:06:56Z (9 years ago)
Author:
Martin Sucha <sucha14@…>
Branches:
lfn, master
Children:
0aa300d
Parents:
99047a72
Message:

Add a function to check if a string has a given prefix.

Location:
uspace/lib/c
Files:
2 edited

Legend:

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

    r99047a72 rdce39b4  
    518518        return 0;
    519519
     520}
     521
     522/** Test whether p is a prefix of s.
     523 *
     524 * Do a char-by-char comparison of two NULL-terminated strings
     525 * and determine if p is a prefix of s.
     526 *
     527 * @param s The string in which to look
     528 * @param p The string to check if it is a prefix of s
     529 *
     530 * @return true iff p is prefix of s else false
     531 *
     532 */
     533bool str_test_prefix(const char *s, const char *p)
     534{
     535        wchar_t c1 = 0;
     536        wchar_t c2 = 0;
     537       
     538        size_t off1 = 0;
     539        size_t off2 = 0;
     540
     541        while (true) {
     542                c1 = str_decode(s, &off1, STR_NO_LIMIT);
     543                c2 = str_decode(p, &off2, STR_NO_LIMIT);
     544               
     545                if (c2 == 0)
     546                        return true;
     547
     548                if (c1 != c2)
     549                        return false;
     550               
     551                if (c1 == 0)
     552                        break;
     553        }
     554
     555        return false;
    520556}
    521557
  • uspace/lib/c/include/str.h

    r99047a72 rdce39b4  
    7979extern int str_lcmp(const char *s1, const char *s2, size_t max_len);
    8080
     81extern bool str_test_prefix(const char *s, const char *p);
     82
    8183extern void str_cpy(char *dest, size_t size, const char *src);
    8284extern void str_ncpy(char *dest, size_t size, const char *src, size_t n);
Note: See TracChangeset for help on using the changeset viewer.