Changeset 0ffbed9 in mainline for uspace/lib/posix/string.c


Ignore:
Timestamp:
2011-06-19T17:49:29Z (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:
5974661
Parents:
f48b637 (diff), 32fb6944 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge libposix.

File:
1 edited

Legend:

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

    rf48b637 r0ffbed9  
    4343#include <errno.h>
    4444
    45 /* Defined for convenience. Returns pointer to the terminating nul character.
     45/**
     46 * Defined for convenience. Returns pointer to the terminating nul character.
     47 *
     48 * @param s
     49 * @return
    4650 */
    4751static char *strzero(const char *s)
    4852{
    49         while (*s != '\0')
    50                 s ++;
    51 
    52         return (char*) s;
    53 }
    54 
    55 /* Returns true if s2 is a prefix of s1.
     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
    5666 */
    5767static bool begins_with(const char *s1, const char *s2)
    5868{
    5969        while (*s1 == *s2 && *s2 != '\0') {
    60                 s1 ++;
    61                 s2 ++;
     70                s1++;
     71                s2++;
    6272        }
    6373       
     
    6676}
    6777
    68 /* The same as strpbrk, except it returns pointer to the nul terminator
     78/**
     79 * The same as strpbrk, except it returns pointer to the nul terminator
    6980 * if no occurence is found.
     81 *
     82 * @param s1
     83 * @param s2
     84 * @return
    7085 */
    7186static char *strpbrk_null(const char *s1, const char *s2)
    7287{
    7388        while (!posix_strchr(s2, *s1)) {
    74                 ++ s1;
     89                ++s1;
    7590        }
    7691       
     
    8297 * @param dest
    8398 * @param src
    84  * @return dest
     99 * @return
    85100 */
    86101char *posix_strcpy(char *dest, const char *src)
     
    95110 * @param src
    96111 * @param n
    97  * @return dest
     112 * @return
    98113 */
    99114char *posix_strncpy(char *dest, const char *src, size_t n)
     
    114129        assert(src != NULL);
    115130
    116         for (size_t i = 0; ; ++ i) {
     131        for (size_t i = 0; ; ++i) {
    117132                dest[i] = src[i];
    118133               
     
    139154        assert(src != NULL);
    140155
    141         for (size_t i = 0; i < n; ++ i) {
     156        for (size_t i = 0; i < n; ++i) {
    142157                dest[i] = src[i];
    143158       
     
    147162                if (src[i] == '\0') {
    148163                        char *result = &dest[i];
    149                         for (++ i; i < n; ++ i) {
     164                        for (++i; i < n; ++i) {
    150165                                dest[i] = '\0';
    151166                        }
     
    161176 * @param dest
    162177 * @param src
    163  * @return dest
     178 * @return
    164179 */
    165180char *posix_strcat(char *dest, const char *src)
     
    177192 * @param src
    178193 * @param n
    179  * @return dest
     194 * @return
    180195 */
    181196char *posix_strncat(char *dest, const char *src, size_t n)
     
    198213 * @return Pointer to the first byte after c in dest if found, NULL otherwise.
    199214 */
    200 void *posix_memccpy(void *restrict dest, const void *restrict src, int c, size_t n)
     215void *posix_memccpy(void *dest, const void *src, int c, size_t n)
    201216{
    202217        assert(dest != NULL);
     
    206221        const unsigned char* bsrc = src;
    207222       
    208         for (size_t i = 0; i < n; ++ i) {
     223        for (size_t i = 0; i < n; ++i) {
    209224                bdest[i] = bsrc[i];
    210225       
     
    257272 * @param n
    258273 * @return Difference of the first pair of inequal bytes,
    259  *          or 0 if areas have the same content
     274 *     or 0 if areas have the same content
    260275 */
    261276int posix_memcmp(const void *mem1, const void *mem2, size_t n)
     
    267282        const unsigned char *s2 = mem2;
    268283       
    269         for (size_t i = 0; i < n; ++ i) {
     284        for (size_t i = 0; i < n; ++i) {
    270285                if (s1[i] != s2[i]) {
    271286                        return s2[i] - s1[i];
     
    302317        assert(s2 != NULL);
    303318
    304         for (size_t i = 0; i < n; ++ i) {
     319        for (size_t i = 0; i < n; ++i) {
    305320                if (s1[i] != s2[i]) {
    306321                        return s2[i] - s1[i];
     
    327342        const unsigned char *s = mem;
    328343       
    329         for (size_t i = 0; i < n; ++ i) {
     344        for (size_t i = 0; i < n; ++i) {
    330345                if (s[i] == (unsigned char) c) {
    331346                        return (void *) &s[i];
     
    346361       
    347362        /* special handling for the case that zero is searched for */
    348         if (c == '\0')
     363        if (c == '\0') {
    349364                return strzero(s);
     365        }
    350366       
    351367        /* otherwise just loop through the string until found */
    352368        while (*s != (char) c) {
    353                 if (*s == '\0')
     369                if (*s == '\0') {
    354370                        return NULL;
    355 
    356                 s ++;
     371                }
     372
     373                s++;
    357374        }
    358375       
     
    374391        /* the same as in strchr, except it loops in reverse direction */
    375392        while (*ptr != (char) c) {
    376                 if (ptr == s)
     393                if (ptr == s) {
    377394                        return NULL;
    378 
    379                 ptr ++;
     395                }
     396
     397                ptr++;
    380398        }
    381399
     
    425443
    426444        const char *ptr;
    427         for (ptr = s1; *ptr != '\0'; ++ ptr) {
    428                 if (!posix_strchr(s2, *ptr))
     445        for (ptr = s1; *ptr != '\0'; ++ptr) {
     446                if (!posix_strchr(s2, *ptr)) {
    429447                        break;
     448                }
    430449        }
    431450        return ptr - s1;
     
    444463
    445464        /* special case - needle is an empty string */
    446         if (*s2 == '\0')
     465        if (*s2 == '\0') {
    447466                return (char *) s1;
     467        }
    448468
    449469        // TODO: use faster algorithm
    450470        /* check for prefix from every position - quadratic complexity */
    451471        while (*s1 != '\0') {
    452                 if (begins_with(s1, s2))
     472                if (begins_with(s1, s2)) {
    453473                        return (char *) s1;
     474                }
    454475               
    455                 s1 ++;
     476                s1++;
    456477        }
    457478       
     
    489510        size_t len = posix_strlen(s2);
    490511
    491         if (n > len)
     512        if (n > len) {
    492513                posix_strcpy(s1, s2);
     514        }
    493515
    494516        return len;
     
    503525{
    504526        /* uses function from libc, we just have to negate errno
    505            (POSIX uses positive errorcodes, HelenOS has negative) */
    506         return (char *) str_error (-errnum);
     527         * (POSIX uses positive errorcodes, HelenOS has negative)
     528         */
     529        return (char *) str_error(-errnum);
    507530}
    508531
     
    510533 *
    511534 * @param errnum Error code
    512  * @param buf    Buffer to store a human readable string to
    513  * @param bufsz  Size of buffer pointed to by buf
     535 * @param buf Buffer to store a human readable string to
     536 * @param bufsz Size of buffer pointed to by buf
    514537 * @return
    515538 */
     
    552575        assert(s != NULL);
    553576       
    554         for (size_t sz = 0; sz < n; ++ sz) {
     577        for (size_t sz = 0; sz < n; ++sz) {
    555578               
    556579                if (s[sz] == '\0') {
Note: See TracChangeset for help on using the changeset viewer.