Changeset 16da5f8e in mainline for kernel/generic/src/lib/func.c


Ignore:
Timestamp:
2009-03-02T21:18:15Z (16 years ago)
Author:
Jiri Svoboda <jirik.svoboda@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
20f1597
Parents:
8dc72b64
Message:

String functions should be declared in string.h (and implemented in string.c) in the kernel.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • kernel/generic/src/lib/func.c

    r8dc72b64 r16da5f8e  
    7878}
    7979
    80 /** Return number of characters in a string.
    81  *
    82  * @param str NULL terminated string.
    83  *
    84  * @return Number of characters in str.
    85  */
    86 size_t strlen(const char *str)
    87 {
    88         int i;
    89        
    90         for (i = 0; str[i]; i++)
    91                 ;
    92        
    93         return i;
    94 }
    95 
    96 /** Compare two NULL terminated strings
    97  *
    98  * Do a char-by-char comparison of two NULL terminated strings.
    99  * The strings are considered equal iff they consist of the same
    100  * characters on the minimum of their lengths.
    101  *
    102  * @param src First string to compare.
    103  * @param dst Second string to compare.
    104  *
    105  * @return 0 if the strings are equal, -1 if first is smaller, 1 if second smaller.
    106  *
    107  */
    108 int strcmp(const char *src, const char *dst)
    109 {
    110         for (; *src && *dst; src++, dst++) {
    111                 if (*src < *dst)
    112                         return -1;
    113                 if (*src > *dst)
    114                         return 1;
    115         }
    116         if (*src == *dst)
    117                 return 0;
    118         if (!*src)
    119                 return -1;
    120         return 1;
    121 }
    122 
    123 
    124 /** Compare two NULL terminated strings
    125  *
    126  * Do a char-by-char comparison of two NULL terminated strings.
    127  * The strings are considered equal iff they consist of the same
    128  * characters on the minimum of their lengths and specified maximal
    129  * length.
    130  *
    131  * @param src First string to compare.
    132  * @param dst Second string to compare.
    133  * @param len Maximal length for comparison.
    134  *
    135  * @return 0 if the strings are equal, -1 if first is smaller, 1 if second smaller.
    136  *
    137  */
    138 int strncmp(const char *src, const char *dst, size_t len)
    139 {
    140         unsigned int i;
    141        
    142         for (i = 0; (*src) && (*dst) && (i < len); src++, dst++, i++) {
    143                 if (*src < *dst)
    144                         return -1;
    145                 if (*src > *dst)
    146                         return 1;
    147         }
    148         if (i == len || *src == *dst)
    149                 return 0;
    150         if (!*src)
    151                 return -1;
    152         return 1;
    153 }
    154 
    155 
    156 
    157 /** Copy NULL terminated string.
    158  *
    159  * Copy at most 'len' characters from string 'src' to 'dest'.
    160  * If 'src' is shorter than 'len', '\0' is inserted behind the
    161  * last copied character.
    162  *
    163  * @param src Source string.
    164  * @param dest Destination buffer.
    165  * @param len Size of destination buffer.
    166  */
    167 void strncpy(char *dest, const char *src, size_t len)
    168 {
    169         unsigned int i;
    170         for (i = 0; i < len; i++) {
    171                 if (!(dest[i] = src[i]))
    172                         return;
    173         }
    174         dest[i-1] = '\0';
    175 }
    176 
    17780/** Convert ascii representation to unative_t
    17881 *
Note: See TracChangeset for help on using the changeset viewer.