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 | |
Aaron Durbin | ac4b00e | 2013-04-26 11:58:35 -0500 | [diff] [blame] | 7 | /* Stringify a token */ |
| 8 | #ifndef STRINGIFY |
| 9 | #define _STRINGIFY(x) #x |
| 10 | #define STRINGIFY(x) _STRINGIFY(x) |
| 11 | #endif |
| 12 | |
Stefan Reinauer | a91e0fe | 2008-08-01 11:39:35 +0000 | [diff] [blame] | 13 | void *memcpy(void *dest, const void *src, size_t n); |
| 14 | void *memmove(void *dest, const void *src, size_t n); |
| 15 | void *memset(void *s, int c, size_t n); |
| 16 | int memcmp(const void *s1, const void *s2, size_t n); |
Gabe Black | 1025f3a | 2011-09-16 02:18:56 -0700 | [diff] [blame] | 17 | void *memchr(const void *s, int c, size_t n); |
Aaron Durbin | 5577a47 | 2016-11-08 09:42:13 -0600 | [diff] [blame] | 18 | #if !defined(__ROMCC__) |
Lee Leahy | 6d71a43 | 2017-03-07 15:24:16 -0800 | [diff] [blame] | 19 | int snprintf(char *buf, size_t size, const char *fmt, ...); |
Stefan Reinauer | 6bd571e | 2009-09-25 21:59:57 +0000 | [diff] [blame] | 20 | #endif |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 21 | |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 22 | // simple string functions |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 23 | |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 24 | static inline size_t strnlen(const char *src, size_t max) |
| 25 | { |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 26 | size_t i = 0; |
Lee Leahy | bfdb893 | 2017-03-07 15:17:04 -0800 | [diff] [blame] | 27 | while ((*src++) && (i < max)) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 28 | i++; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 29 | return i; |
| 30 | } |
| 31 | |
| 32 | static inline size_t strlen(const char *src) |
| 33 | { |
| 34 | size_t i = 0; |
Lee Leahy | bfdb893 | 2017-03-07 15:17:04 -0800 | [diff] [blame] | 35 | while (*src++) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 36 | i++; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 37 | return i; |
| 38 | } |
| 39 | |
Greg Watson | 1624782 | 2004-03-13 03:34:52 +0000 | [diff] [blame] | 40 | static inline char *strchr(const char *s, int c) |
| 41 | { |
| 42 | for (; *s; s++) { |
| 43 | if (*s == c) |
| 44 | return (char *) s; |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 45 | } |
Greg Watson | 1624782 | 2004-03-13 03:34:52 +0000 | [diff] [blame] | 46 | return 0; |
| 47 | } |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 48 | |
Stefan Reinauer | 35b6bbb | 2010-03-28 21:26:54 +0000 | [diff] [blame] | 49 | #if !defined(__PRE_RAM__) |
Greg Watson | 1624782 | 2004-03-13 03:34:52 +0000 | [diff] [blame] | 50 | static inline char *strdup(const char *s) |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 51 | { |
Greg Watson | 1624782 | 2004-03-13 03:34:52 +0000 | [diff] [blame] | 52 | size_t sz = strlen(s) + 1; |
| 53 | char *d = malloc(sz); |
| 54 | memcpy(d, s, sz); |
| 55 | return d; |
| 56 | } |
Kyösti Mälkki | c36af7b | 2014-11-18 12:41:16 +0200 | [diff] [blame] | 57 | |
| 58 | static inline char *strconcat(const char *s1, const char *s2) |
| 59 | { |
| 60 | size_t sz_1 = strlen(s1); |
| 61 | size_t sz_2 = strlen(s2); |
| 62 | char *d = malloc(sz_1 + sz_2 + 1); |
| 63 | memcpy(d, s1, sz_1); |
| 64 | memcpy(d + sz_1, s2, sz_2 + 1); |
| 65 | return d; |
| 66 | } |
Stefan Reinauer | 951f588 | 2009-07-19 00:18:15 +0000 | [diff] [blame] | 67 | #endif |
Greg Watson | 1624782 | 2004-03-13 03:34:52 +0000 | [diff] [blame] | 68 | |
| 69 | static inline char *strncpy(char *to, const char *from, int count) |
| 70 | { |
| 71 | register char *ret = to; |
Lee Leahy | b126055 | 2017-03-07 16:01:09 -0800 | [diff] [blame] | 72 | register char data; |
Greg Watson | 1624782 | 2004-03-13 03:34:52 +0000 | [diff] [blame] | 73 | |
| 74 | while (count > 0) { |
| 75 | count--; |
Lee Leahy | b126055 | 2017-03-07 16:01:09 -0800 | [diff] [blame] | 76 | data = *from++; |
| 77 | *to++ = data; |
| 78 | if (data == '\0') |
Greg Watson | 1624782 | 2004-03-13 03:34:52 +0000 | [diff] [blame] | 79 | break; |
| 80 | } |
| 81 | |
| 82 | while (count > 0) { |
| 83 | count--; |
| 84 | *to++ = '\0'; |
| 85 | } |
| 86 | return ret; |
| 87 | } |
| 88 | |
Stefan Reinauer | 2c3f260 | 2012-12-19 11:22:07 -0800 | [diff] [blame] | 89 | static inline char *strcpy(char *dst, const char *src) |
Uwe Hermann | aac8f66 | 2010-09-29 09:54:16 +0000 | [diff] [blame] | 90 | { |
Stefan Reinauer | 2c3f260 | 2012-12-19 11:22:07 -0800 | [diff] [blame] | 91 | char *ptr = dst; |
| 92 | |
Uwe Hermann | aac8f66 | 2010-09-29 09:54:16 +0000 | [diff] [blame] | 93 | while (*src) |
| 94 | *dst++ = *src++; |
Stefan Reinauer | 2c3f260 | 2012-12-19 11:22:07 -0800 | [diff] [blame] | 95 | *dst = '\0'; |
| 96 | |
| 97 | return ptr; |
Uwe Hermann | aac8f66 | 2010-09-29 09:54:16 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Greg Watson | 1624782 | 2004-03-13 03:34:52 +0000 | [diff] [blame] | 100 | static inline int strcmp(const char *s1, const char *s2) |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 101 | { |
Greg Watson | 1624782 | 2004-03-13 03:34:52 +0000 | [diff] [blame] | 102 | int r; |
| 103 | |
| 104 | while ((r = (*s1 - *s2)) == 0 && *s1) { |
| 105 | s1++; |
| 106 | s2++; |
| 107 | } |
| 108 | return r; |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 109 | } |
Greg Watson | 1624782 | 2004-03-13 03:34:52 +0000 | [diff] [blame] | 110 | |
Stefan Reinauer | 36c8340 | 2009-03-01 10:16:01 +0000 | [diff] [blame] | 111 | static inline int strncmp(const char *s1, const char *s2, int maxlen) |
| 112 | { |
| 113 | int i; |
| 114 | |
| 115 | for (i = 0; i < maxlen; i++) { |
Hannah Williams | 03d4ae7 | 2015-12-01 09:30:27 -0800 | [diff] [blame] | 116 | if ((s1[i] != s2[i]) || (s1[i] == '\0')) |
Stefan Reinauer | 36c8340 | 2009-03-01 10:16:01 +0000 | [diff] [blame] | 117 | return s1[i] - s2[i]; |
| 118 | } |
| 119 | |
| 120 | return 0; |
| 121 | } |
| 122 | |
Greg Watson | 1624782 | 2004-03-13 03:34:52 +0000 | [diff] [blame] | 123 | static inline int isspace(int c) |
| 124 | { |
| 125 | switch (c) { |
| 126 | case ' ': case '\f': case '\n': |
| 127 | case '\r': case '\t': case '\v': |
| 128 | return 1; |
| 129 | default: |
| 130 | return 0; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | static inline int isdigit(int c) |
| 135 | { |
| 136 | return (c >= '0' && c <= '9'); |
| 137 | } |
| 138 | |
| 139 | static inline int isxdigit(int c) |
| 140 | { |
| 141 | return ((c >= '0' && c <= '9') || |
| 142 | (c >= 'a' && c <= 'f') || |
| 143 | (c >= 'A' && c <= 'F')); |
| 144 | } |
| 145 | |
Greg Watson | f6d05f5 | 2004-03-17 17:02:28 +0000 | [diff] [blame] | 146 | static inline int isupper(int c) |
| 147 | { |
| 148 | return (c >= 'A' && c <= 'Z'); |
| 149 | } |
| 150 | |
Greg Watson | 1624782 | 2004-03-13 03:34:52 +0000 | [diff] [blame] | 151 | static inline int islower(int c) |
| 152 | { |
| 153 | return (c >= 'a' && c <= 'z'); |
| 154 | } |
| 155 | |
| 156 | static inline int toupper(int c) |
| 157 | { |
Elyes HAOUAS | 8ffd050 | 2016-09-01 19:01:41 +0200 | [diff] [blame] | 158 | if (islower(c)) |
| 159 | c -= 'a'-'A'; |
| 160 | return c; |
Greg Watson | 1624782 | 2004-03-13 03:34:52 +0000 | [diff] [blame] | 161 | } |
Greg Watson | f6d05f5 | 2004-03-17 17:02:28 +0000 | [diff] [blame] | 162 | |
| 163 | static inline int tolower(int c) |
| 164 | { |
Elyes HAOUAS | 8ffd050 | 2016-09-01 19:01:41 +0200 | [diff] [blame] | 165 | if (isupper(c)) |
| 166 | c -= 'A'-'a'; |
| 167 | return c; |
Greg Watson | f6d05f5 | 2004-03-17 17:02:28 +0000 | [diff] [blame] | 168 | } |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 169 | #endif /* STRING_H */ |