Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 1 | #ifndef STRING_H |
| 2 | #define STRING_H |
| 3 | |
| 4 | #include <stddef.h> |
Greg Watson | 1624782 | 2004-03-13 03:34:52 +0000 | [diff] [blame] | 5 | #include <stdlib.h> |
| 6 | |
Stefan Reinauer | a91e0fe | 2008-08-01 11:39:35 +0000 | [diff] [blame] | 7 | void *memcpy(void *dest, const void *src, size_t n); |
| 8 | void *memmove(void *dest, const void *src, size_t n); |
| 9 | void *memset(void *s, int c, size_t n); |
| 10 | int memcmp(const void *s1, const void *s2, size_t n); |
Stefan Reinauer | 35b6bbb | 2010-03-28 21:26:54 +0000 | [diff] [blame] | 11 | #if !defined(__PRE_RAM__) |
Stefan Reinauer | a91e0fe | 2008-08-01 11:39:35 +0000 | [diff] [blame] | 12 | int sprintf(char * buf, const char *fmt, ...); |
Stefan Reinauer | 6bd571e | 2009-09-25 21:59:57 +0000 | [diff] [blame] | 13 | #endif |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 14 | |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 15 | // simple string functions |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 16 | |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 17 | static inline size_t strnlen(const char *src, size_t max) |
| 18 | { |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 19 | size_t i = 0; |
| 20 | while((*src++) && (i < max)) { |
| 21 | i++; |
| 22 | } |
| 23 | return i; |
| 24 | } |
| 25 | |
| 26 | static inline size_t strlen(const char *src) |
| 27 | { |
| 28 | size_t i = 0; |
| 29 | while(*src++) { |
| 30 | i++; |
| 31 | } |
| 32 | return i; |
| 33 | } |
| 34 | |
Greg Watson | 1624782 | 2004-03-13 03:34:52 +0000 | [diff] [blame] | 35 | static inline char *strchr(const char *s, int c) |
| 36 | { |
| 37 | for (; *s; s++) { |
| 38 | if (*s == c) |
| 39 | return (char *) s; |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 40 | } |
Greg Watson | 1624782 | 2004-03-13 03:34:52 +0000 | [diff] [blame] | 41 | return 0; |
| 42 | } |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 43 | |
Stefan Reinauer | 35b6bbb | 2010-03-28 21:26:54 +0000 | [diff] [blame] | 44 | #if !defined(__PRE_RAM__) |
Greg Watson | 1624782 | 2004-03-13 03:34:52 +0000 | [diff] [blame] | 45 | static inline char *strdup(const char *s) |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 46 | { |
Greg Watson | 1624782 | 2004-03-13 03:34:52 +0000 | [diff] [blame] | 47 | size_t sz = strlen(s) + 1; |
| 48 | char *d = malloc(sz); |
| 49 | memcpy(d, s, sz); |
| 50 | return d; |
| 51 | } |
Stefan Reinauer | 951f588 | 2009-07-19 00:18:15 +0000 | [diff] [blame] | 52 | #endif |
Greg Watson | 1624782 | 2004-03-13 03:34:52 +0000 | [diff] [blame] | 53 | |
| 54 | static inline char *strncpy(char *to, const char *from, int count) |
| 55 | { |
| 56 | register char *ret = to; |
| 57 | |
| 58 | while (count > 0) { |
| 59 | count--; |
| 60 | if ((*to++ = *from++) == '\0') |
| 61 | break; |
| 62 | } |
| 63 | |
| 64 | while (count > 0) { |
| 65 | count--; |
| 66 | *to++ = '\0'; |
| 67 | } |
| 68 | return ret; |
| 69 | } |
| 70 | |
Uwe Hermann | aac8f66 | 2010-09-29 09:54:16 +0000 | [diff] [blame] | 71 | static inline void strcpy(char *dst, const char *src) |
| 72 | { |
| 73 | while (*src) |
| 74 | *dst++ = *src++; |
| 75 | } |
| 76 | |
Greg Watson | 1624782 | 2004-03-13 03:34:52 +0000 | [diff] [blame] | 77 | static inline int strcmp(const char *s1, const char *s2) |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 78 | { |
Greg Watson | 1624782 | 2004-03-13 03:34:52 +0000 | [diff] [blame] | 79 | int r; |
| 80 | |
| 81 | while ((r = (*s1 - *s2)) == 0 && *s1) { |
| 82 | s1++; |
| 83 | s2++; |
| 84 | } |
| 85 | return r; |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 86 | } |
Greg Watson | 1624782 | 2004-03-13 03:34:52 +0000 | [diff] [blame] | 87 | |
Stefan Reinauer | 36c8340 | 2009-03-01 10:16:01 +0000 | [diff] [blame] | 88 | static inline int strncmp(const char *s1, const char *s2, int maxlen) |
| 89 | { |
| 90 | int i; |
| 91 | |
| 92 | for (i = 0; i < maxlen; i++) { |
| 93 | if (s1[i] != s2[i]) |
| 94 | return s1[i] - s2[i]; |
| 95 | } |
| 96 | |
| 97 | return 0; |
| 98 | } |
| 99 | |
Greg Watson | 1624782 | 2004-03-13 03:34:52 +0000 | [diff] [blame] | 100 | static inline int isspace(int c) |
| 101 | { |
| 102 | switch (c) { |
| 103 | case ' ': case '\f': case '\n': |
| 104 | case '\r': case '\t': case '\v': |
| 105 | return 1; |
| 106 | default: |
| 107 | return 0; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | static inline int isdigit(int c) |
| 112 | { |
| 113 | return (c >= '0' && c <= '9'); |
| 114 | } |
| 115 | |
| 116 | static inline int isxdigit(int c) |
| 117 | { |
| 118 | return ((c >= '0' && c <= '9') || |
| 119 | (c >= 'a' && c <= 'f') || |
| 120 | (c >= 'A' && c <= 'F')); |
| 121 | } |
| 122 | |
Greg Watson | f6d05f5 | 2004-03-17 17:02:28 +0000 | [diff] [blame] | 123 | static inline int isupper(int c) |
| 124 | { |
| 125 | return (c >= 'A' && c <= 'Z'); |
| 126 | } |
| 127 | |
Greg Watson | 1624782 | 2004-03-13 03:34:52 +0000 | [diff] [blame] | 128 | static inline int islower(int c) |
| 129 | { |
| 130 | return (c >= 'a' && c <= 'z'); |
| 131 | } |
| 132 | |
| 133 | static inline int toupper(int c) |
| 134 | { |
| 135 | if (islower(c)) |
| 136 | c -= 'a'-'A'; |
| 137 | return c; |
| 138 | } |
Greg Watson | f6d05f5 | 2004-03-17 17:02:28 +0000 | [diff] [blame] | 139 | |
| 140 | static inline int tolower(int c) |
| 141 | { |
| 142 | if (isupper(c)) |
| 143 | c -= 'A'-'a'; |
| 144 | return c; |
| 145 | } |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 146 | #endif /* STRING_H */ |