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 |
Thomas Heijligen | 9204355 | 2019-01-29 12:48:01 +0100 | [diff] [blame] | 26 | char *strdup(const char *s); |
| 27 | char *strconcat(const char *s1, const char *s2); |
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 | // simple string functions |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 30 | |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 31 | static inline size_t strnlen(const char *src, size_t max) |
| 32 | { |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 33 | size_t i = 0; |
Lee Leahy | bfdb893 | 2017-03-07 15:17:04 -0800 | [diff] [blame] | 34 | while ((*src++) && (i < max)) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 35 | i++; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 36 | return i; |
| 37 | } |
| 38 | |
| 39 | static inline size_t strlen(const char *src) |
| 40 | { |
| 41 | size_t i = 0; |
Lee Leahy | bfdb893 | 2017-03-07 15:17:04 -0800 | [diff] [blame] | 42 | while (*src++) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 43 | i++; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 44 | return i; |
| 45 | } |
| 46 | |
Greg Watson | 1624782 | 2004-03-13 03:34:52 +0000 | [diff] [blame] | 47 | static inline char *strchr(const char *s, int c) |
| 48 | { |
| 49 | for (; *s; s++) { |
| 50 | if (*s == c) |
| 51 | return (char *) s; |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 52 | } |
Greg Watson | 1624782 | 2004-03-13 03:34:52 +0000 | [diff] [blame] | 53 | return 0; |
| 54 | } |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 55 | |
Patrick Rudolph | 03c7b05 | 2018-03-29 10:05:34 +0200 | [diff] [blame] | 56 | /** |
| 57 | * Find a character in a string. |
| 58 | * |
| 59 | * @param s The string. |
| 60 | * @param c The character. |
| 61 | * @return A pointer to the last occurrence of the character in the |
| 62 | * string, or NULL if the character was not encountered within the string. |
| 63 | */ |
| 64 | static inline char *strrchr(const char *s, int c) |
| 65 | { |
| 66 | char *p = (char *)s + strlen(s); |
| 67 | |
| 68 | for (; p >= s; p--) { |
| 69 | if (*p == c) |
| 70 | return p; |
| 71 | } |
| 72 | |
| 73 | return NULL; |
| 74 | } |
| 75 | |
Greg Watson | 1624782 | 2004-03-13 03:34:52 +0000 | [diff] [blame] | 76 | static inline char *strncpy(char *to, const char *from, int count) |
| 77 | { |
| 78 | register char *ret = to; |
Lee Leahy | b126055 | 2017-03-07 16:01:09 -0800 | [diff] [blame] | 79 | register char data; |
Greg Watson | 1624782 | 2004-03-13 03:34:52 +0000 | [diff] [blame] | 80 | |
| 81 | while (count > 0) { |
| 82 | count--; |
Lee Leahy | b126055 | 2017-03-07 16:01:09 -0800 | [diff] [blame] | 83 | data = *from++; |
| 84 | *to++ = data; |
| 85 | if (data == '\0') |
Greg Watson | 1624782 | 2004-03-13 03:34:52 +0000 | [diff] [blame] | 86 | break; |
| 87 | } |
| 88 | |
| 89 | while (count > 0) { |
| 90 | count--; |
| 91 | *to++ = '\0'; |
| 92 | } |
| 93 | return ret; |
| 94 | } |
| 95 | |
Stefan Reinauer | 2c3f260 | 2012-12-19 11:22:07 -0800 | [diff] [blame] | 96 | static inline char *strcpy(char *dst, const char *src) |
Uwe Hermann | aac8f66 | 2010-09-29 09:54:16 +0000 | [diff] [blame] | 97 | { |
Stefan Reinauer | 2c3f260 | 2012-12-19 11:22:07 -0800 | [diff] [blame] | 98 | char *ptr = dst; |
| 99 | |
Uwe Hermann | aac8f66 | 2010-09-29 09:54:16 +0000 | [diff] [blame] | 100 | while (*src) |
| 101 | *dst++ = *src++; |
Stefan Reinauer | 2c3f260 | 2012-12-19 11:22:07 -0800 | [diff] [blame] | 102 | *dst = '\0'; |
| 103 | |
| 104 | return ptr; |
Uwe Hermann | aac8f66 | 2010-09-29 09:54:16 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Greg Watson | 1624782 | 2004-03-13 03:34:52 +0000 | [diff] [blame] | 107 | static inline int strcmp(const char *s1, const char *s2) |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 108 | { |
Greg Watson | 1624782 | 2004-03-13 03:34:52 +0000 | [diff] [blame] | 109 | int r; |
| 110 | |
| 111 | while ((r = (*s1 - *s2)) == 0 && *s1) { |
| 112 | s1++; |
| 113 | s2++; |
| 114 | } |
| 115 | return r; |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 116 | } |
Greg Watson | 1624782 | 2004-03-13 03:34:52 +0000 | [diff] [blame] | 117 | |
Stefan Reinauer | 36c8340 | 2009-03-01 10:16:01 +0000 | [diff] [blame] | 118 | static inline int strncmp(const char *s1, const char *s2, int maxlen) |
| 119 | { |
| 120 | int i; |
| 121 | |
| 122 | for (i = 0; i < maxlen; i++) { |
Hannah Williams | 03d4ae7 | 2015-12-01 09:30:27 -0800 | [diff] [blame] | 123 | if ((s1[i] != s2[i]) || (s1[i] == '\0')) |
Stefan Reinauer | 36c8340 | 2009-03-01 10:16:01 +0000 | [diff] [blame] | 124 | return s1[i] - s2[i]; |
| 125 | } |
| 126 | |
| 127 | return 0; |
| 128 | } |
| 129 | |
Greg Watson | 1624782 | 2004-03-13 03:34:52 +0000 | [diff] [blame] | 130 | static inline int isspace(int c) |
| 131 | { |
| 132 | switch (c) { |
| 133 | case ' ': case '\f': case '\n': |
| 134 | case '\r': case '\t': case '\v': |
| 135 | return 1; |
| 136 | default: |
| 137 | return 0; |
| 138 | } |
| 139 | } |
| 140 | |
Nico Huber | f5b346c | 2017-11-01 13:21:40 +0100 | [diff] [blame] | 141 | static inline int isprint(int c) |
| 142 | { |
| 143 | return c >= ' ' && c <= '~'; |
| 144 | } |
| 145 | |
Greg Watson | 1624782 | 2004-03-13 03:34:52 +0000 | [diff] [blame] | 146 | static inline int isdigit(int c) |
| 147 | { |
| 148 | return (c >= '0' && c <= '9'); |
| 149 | } |
| 150 | |
| 151 | static inline int isxdigit(int c) |
| 152 | { |
| 153 | return ((c >= '0' && c <= '9') || |
| 154 | (c >= 'a' && c <= 'f') || |
| 155 | (c >= 'A' && c <= 'F')); |
| 156 | } |
| 157 | |
Greg Watson | f6d05f5 | 2004-03-17 17:02:28 +0000 | [diff] [blame] | 158 | static inline int isupper(int c) |
| 159 | { |
| 160 | return (c >= 'A' && c <= 'Z'); |
| 161 | } |
| 162 | |
Greg Watson | 1624782 | 2004-03-13 03:34:52 +0000 | [diff] [blame] | 163 | static inline int islower(int c) |
| 164 | { |
| 165 | return (c >= 'a' && c <= 'z'); |
| 166 | } |
| 167 | |
| 168 | static inline int toupper(int c) |
| 169 | { |
Elyes HAOUAS | 8ffd050 | 2016-09-01 19:01:41 +0200 | [diff] [blame] | 170 | if (islower(c)) |
| 171 | c -= 'a'-'A'; |
| 172 | return c; |
Greg Watson | 1624782 | 2004-03-13 03:34:52 +0000 | [diff] [blame] | 173 | } |
Greg Watson | f6d05f5 | 2004-03-17 17:02:28 +0000 | [diff] [blame] | 174 | |
| 175 | static inline int tolower(int c) |
| 176 | { |
Elyes HAOUAS | 8ffd050 | 2016-09-01 19:01:41 +0200 | [diff] [blame] | 177 | if (isupper(c)) |
| 178 | c -= 'A'-'a'; |
| 179 | return c; |
Greg Watson | f6d05f5 | 2004-03-17 17:02:28 +0000 | [diff] [blame] | 180 | } |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 181 | #endif /* STRING_H */ |