Ignore:
File:
1 edited

Legend:

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

    r4f4b4e7 ref6dd3f  
    4343#include <errno.h>
    4444
    45 /**
    46  * Defined for convenience. Returns pointer to the terminating nul character.
    47  *
    48  * @param s
    49  * @return
     45/* Defined for convenience. Returns pointer to the terminating nul character.
    5046 */
    5147static char *strzero(const char *s)
    5248{
    53         while (*s != '\0') {
    54                 s++;
    55         }
    56 
    57         return (char *) s;
    58 }
    59 
    60 /**
    61  * Returns true if s2 is a prefix of s1.
    62  *
    63  * @param s1
    64  * @param s2
    65  * @return
     49        while (*s != '\0')
     50                s ++;
     51
     52        return (char*) s;
     53}
     54
     55/* Returns true if s2 is a prefix of s1.
    6656 */
    6757static bool begins_with(const char *s1, const char *s2)
    6858{
    6959        while (*s1 == *s2 && *s2 != '\0') {
    70                 s1++;
    71                 s2++;
     60                s1 ++;
     61                s2 ++;
    7262        }
    7363       
     
    7666}
    7767
    78 /**
    79  * The same as strpbrk, except it returns pointer to the nul terminator
     68/* The same as strpbrk, except it returns pointer to the nul terminator
    8069 * if no occurence is found.
    81  *
    82  * @param s1
    83  * @param s2
    84  * @return
    8570 */
    8671static char *strpbrk_null(const char *s1, const char *s2)
    8772{
    8873        while (!posix_strchr(s2, *s1)) {
    89                 ++s1;
     74                ++ s1;
    9075        }
    9176       
     
    9782 * @param dest
    9883 * @param src
    99  * @return
     84 * @return dest
    10085 */
    10186char *posix_strcpy(char *dest, const char *src)
     
    11095 * @param src
    11196 * @param n
    112  * @return
     97 * @return dest
    11398 */
    11499char *posix_strncpy(char *dest, const char *src, size_t n)
     
    129114        assert(src != NULL);
    130115
    131         for (size_t i = 0; ; ++i) {
     116        for (size_t i = 0; ; ++ i) {
    132117                dest[i] = src[i];
    133118               
     
    154139        assert(src != NULL);
    155140
    156         for (size_t i = 0; i < n; ++i) {
     141        for (size_t i = 0; i < n; ++ i) {
    157142                dest[i] = src[i];
    158143       
     
    162147                if (src[i] == '\0') {
    163148                        char *result = &dest[i];
    164                         for (++i; i < n; ++i) {
     149                        for (++ i; i < n; ++ i) {
    165150                                dest[i] = '\0';
    166151                        }
     
    176161 * @param dest
    177162 * @param src
    178  * @return
     163 * @return dest
    179164 */
    180165char *posix_strcat(char *dest, const char *src)
     
    192177 * @param src
    193178 * @param n
    194  * @return
     179 * @return dest
    195180 */
    196181char *posix_strncat(char *dest, const char *src, size_t n)
     
    213198 * @return Pointer to the first byte after c in dest if found, NULL otherwise.
    214199 */
    215 void *posix_memccpy(void *dest, const void *src, int c, size_t n)
     200void *posix_memccpy(void *restrict dest, const void *restrict src, int c, size_t n)
    216201{
    217202        assert(dest != NULL);
     
    221206        const unsigned char* bsrc = src;
    222207       
    223         for (size_t i = 0; i < n; ++i) {
     208        for (size_t i = 0; i < n; ++ i) {
    224209                bdest[i] = bsrc[i];
    225210       
     
    272257 * @param n
    273258 * @return Difference of the first pair of inequal bytes,
    274  *     or 0 if areas have the same content
     259 *          or 0 if areas have the same content
    275260 */
    276261int posix_memcmp(const void *mem1, const void *mem2, size_t n)
     
    282267        const unsigned char *s2 = mem2;
    283268       
    284         for (size_t i = 0; i < n; ++i) {
     269        for (size_t i = 0; i < n; ++ i) {
    285270                if (s1[i] != s2[i]) {
    286271                        return s2[i] - s1[i];
     
    317302        assert(s2 != NULL);
    318303
    319         for (size_t i = 0; i < n; ++i) {
     304        for (size_t i = 0; i < n; ++ i) {
    320305                if (s1[i] != s2[i]) {
    321306                        return s2[i] - s1[i];
     
    342327        const unsigned char *s = mem;
    343328       
    344         for (size_t i = 0; i < n; ++i) {
     329        for (size_t i = 0; i < n; ++ i) {
    345330                if (s[i] == (unsigned char) c) {
    346331                        return (void *) &s[i];
     
    361346       
    362347        /* special handling for the case that zero is searched for */
    363         if (c == '\0') {
     348        if (c == '\0')
    364349                return strzero(s);
    365         }
    366350       
    367351        /* otherwise just loop through the string until found */
    368352        while (*s != (char) c) {
    369                 if (*s == '\0') {
     353                if (*s == '\0')
    370354                        return NULL;
    371                 }
    372 
    373                 s++;
     355
     356                s ++;
    374357        }
    375358       
     
    391374        /* the same as in strchr, except it loops in reverse direction */
    392375        while (*ptr != (char) c) {
    393                 if (ptr == s) {
     376                if (ptr == s)
    394377                        return NULL;
    395                 }
    396 
    397                 ptr++;
     378
     379                ptr ++;
    398380        }
    399381
     
    443425
    444426        const char *ptr;
    445         for (ptr = s1; *ptr != '\0'; ++ptr) {
    446                 if (!posix_strchr(s2, *ptr)) {
     427        for (ptr = s1; *ptr != '\0'; ++ ptr) {
     428                if (!posix_strchr(s2, *ptr))
    447429                        break;
    448                 }
    449430        }
    450431        return ptr - s1;
     
    463444
    464445        /* special case - needle is an empty string */
    465         if (*s2 == '\0') {
     446        if (*s2 == '\0')
    466447                return (char *) s1;
    467         }
    468448
    469449        // TODO: use faster algorithm
    470450        /* check for prefix from every position - quadratic complexity */
    471451        while (*s1 != '\0') {
    472                 if (begins_with(s1, s2)) {
     452                if (begins_with(s1, s2))
    473453                        return (char *) s1;
    474                 }
    475454               
    476                 s1++;
     455                s1 ++;
    477456        }
    478457       
     
    510489        size_t len = posix_strlen(s2);
    511490
    512         if (n > len) {
     491        if (n > len)
    513492                posix_strcpy(s1, s2);
    514         }
    515493
    516494        return len;
     
    525503{
    526504        /* uses function from libc, we just have to negate errno
    527          * (POSIX uses positive errorcodes, HelenOS has negative)
    528          */
    529         return (char *) str_error(-errnum);
     505           (POSIX uses positive errorcodes, HelenOS has negative) */
     506        return (char *) str_error (-errnum);
    530507}
    531508
     
    533510 *
    534511 * @param errnum Error code
    535  * @param buf Buffer to store a human readable string to
    536  * @param bufsz Size of buffer pointed to by buf
     512 * @param buf    Buffer to store a human readable string to
     513 * @param bufsz  Size of buffer pointed to by buf
    537514 * @return
    538515 */
     
    575552        assert(s != NULL);
    576553       
    577         for (size_t sz = 0; sz < n; ++sz) {
     554        for (size_t sz = 0; sz < n; ++ sz) {
    578555               
    579556                if (s[sz] == '\0') {
Note: See TracChangeset for help on using the changeset viewer.