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