Changeset 517cedc0 in mainline


Ignore:
Timestamp:
2011-07-01T19:30:59Z (13 years ago)
Author:
Jiří Zárevúcky <zarevucky.jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
b52ef5a
Parents:
65ed8f4
Message:

string.h: Add strchrnul().

Location:
uspace/lib/posix
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/posix/string.c

    r65ed8f4 r517cedc0  
    4444
    4545/**
    46  * Defined for convenience. Returns pointer to the terminating nul character.
    47  *
    48  * @param s
    49  * @return
    50  */
    51 static char *strzero(const char *s)
    52 {
    53         while (*s != '\0') {
    54                 s++;
    55         }
    56 
    57         return (char *) s;
    58 }
    59 
    60 /**
    6146 * Returns true if s2 is a prefix of s1.
    6247 *
     
    183168        assert(src != NULL);
    184169
    185         posix_strcpy(strzero(dest), src);
     170        posix_strcpy(posix_strchr(dest, '\0'), src);
    186171        return dest;
    187172}
     
    199184        assert(src != NULL);
    200185
    201         char *zeroptr = posix_strncpy(strzero(dest), src, n);
     186        char *zeroptr = posix_strncpy(posix_strchr(dest, '\0'), src, n);
    202187        /* strncpy doesn't append the nul terminator, so we do it here */
    203188        zeroptr[n] = '\0';
     
    360345        assert(s != NULL);
    361346       
    362         /* special handling for the case that zero is searched for */
    363         if (c == '\0') {
    364                 return strzero(s);
    365         }
    366        
    367         /* otherwise just loop through the string until found */
    368         while (*s != (char) c) {
    369                 if (*s == '\0') {
    370                         return NULL;
    371                 }
    372 
    373                 s++;
    374         }
    375        
    376         return (char *) s;
     347        char *res = gnu_strchrnul(s, c);
     348        return (*res == c) ? res : NULL;
    377349}
    378350
     
    387359        assert(s != NULL);
    388360       
    389         const char *ptr = strzero(s);
     361        const char *ptr = posix_strchr(s, '\0');
    390362       
    391363        /* the same as in strchr, except it loops in reverse direction */
     
    399371
    400372        return (char *) ptr;
     373}
     374
     375char *gnu_strchrnul(const char *s, int c)
     376{
     377        assert(s != NULL);
     378       
     379        while (*s != c && *s != '\0') {
     380                s++;
     381        }
     382       
     383        return (char *) s;
    401384}
    402385
     
    562545        assert(s != NULL);
    563546       
    564         return (size_t) (strzero(s) - s);
     547        return (size_t) (posix_strchr(s, '\0') - s);
    565548}
    566549
  • uspace/lib/posix/string.h

    r65ed8f4 r517cedc0  
    8585extern char *posix_strchr(const char *s, int c);
    8686extern char *posix_strrchr(const char *s, int c);
     87extern char *gnu_strchrnul(const char *s, int c);
    8788extern char *posix_strpbrk(const char *s1, const char *s2);
    8889extern size_t posix_strcspn(const char *s1, const char *s2);
     
    127128        #define strchr posix_strchr
    128129        #define strrchr posix_strrchr
     130        #define strchrnul gnu_strchrnul
    129131        #define strpbrk posix_strpbrk
    130132        #define strcspn posix_strcspn
Note: See TracChangeset for help on using the changeset viewer.