blob: 4a2f5e9ee6678c58c502ceb413e135f83f721849 [file] [log] [blame]
Eric Biederman8ca8d762003-04-22 19:02:15 +00001#ifndef STRING_H
2#define STRING_H
3
4#include <stddef.h>
Greg Watson16247822004-03-13 03:34:52 +00005#include <stdlib.h>
6
David Hendricks6053a9c2018-01-28 18:01:10 -08007#if !defined(__ROMCC__)
8#include <console/vtxprintf.h>
9#endif
10
Aaron Durbinac4b00e2013-04-26 11:58:35 -050011/* Stringify a token */
12#ifndef STRINGIFY
13#define _STRINGIFY(x) #x
14#define STRINGIFY(x) _STRINGIFY(x)
15#endif
16
Stefan Reinauera91e0fe2008-08-01 11:39:35 +000017void *memcpy(void *dest, const void *src, size_t n);
18void *memmove(void *dest, const void *src, size_t n);
19void *memset(void *s, int c, size_t n);
20int memcmp(const void *s1, const void *s2, size_t n);
Gabe Black1025f3a2011-09-16 02:18:56 -070021void *memchr(const void *s, int c, size_t n);
Aaron Durbin5577a472016-11-08 09:42:13 -060022#if !defined(__ROMCC__)
Lee Leahy6d71a432017-03-07 15:24:16 -080023int snprintf(char *buf, size_t size, const char *fmt, ...);
David Hendricks6053a9c2018-01-28 18:01:10 -080024int vsnprintf(char *buf, size_t size, const char *fmt, va_list args);
Stefan Reinauer6bd571e2009-09-25 21:59:57 +000025#endif
Thomas Heijligen92043552019-01-29 12:48:01 +010026char *strdup(const char *s);
27char *strconcat(const char *s1, const char *s2);
Eric Biederman8ca8d762003-04-22 19:02:15 +000028
Stefan Reinauer14e22772010-04-27 06:56:47 +000029// simple string functions
Eric Biederman8ca8d762003-04-22 19:02:15 +000030
Stefan Reinauer14e22772010-04-27 06:56:47 +000031static inline size_t strnlen(const char *src, size_t max)
32{
Eric Biederman8ca8d762003-04-22 19:02:15 +000033 size_t i = 0;
Lee Leahybfdb8932017-03-07 15:17:04 -080034 while ((*src++) && (i < max))
Eric Biederman8ca8d762003-04-22 19:02:15 +000035 i++;
Eric Biederman8ca8d762003-04-22 19:02:15 +000036 return i;
37}
38
39static inline size_t strlen(const char *src)
40{
41 size_t i = 0;
Lee Leahybfdb8932017-03-07 15:17:04 -080042 while (*src++)
Eric Biederman8ca8d762003-04-22 19:02:15 +000043 i++;
Eric Biederman8ca8d762003-04-22 19:02:15 +000044 return i;
45}
46
Greg Watson16247822004-03-13 03:34:52 +000047static inline char *strchr(const char *s, int c)
48{
49 for (; *s; s++) {
50 if (*s == c)
51 return (char *) s;
Stefan Reinauer14e22772010-04-27 06:56:47 +000052 }
Greg Watson16247822004-03-13 03:34:52 +000053 return 0;
54}
Eric Biederman8ca8d762003-04-22 19:02:15 +000055
Patrick Rudolph03c7b052018-03-29 10:05:34 +020056/**
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 */
64static 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 Watson16247822004-03-13 03:34:52 +000076static inline char *strncpy(char *to, const char *from, int count)
77{
78 register char *ret = to;
Lee Leahyb1260552017-03-07 16:01:09 -080079 register char data;
Greg Watson16247822004-03-13 03:34:52 +000080
81 while (count > 0) {
82 count--;
Lee Leahyb1260552017-03-07 16:01:09 -080083 data = *from++;
84 *to++ = data;
85 if (data == '\0')
Greg Watson16247822004-03-13 03:34:52 +000086 break;
87 }
88
89 while (count > 0) {
90 count--;
91 *to++ = '\0';
92 }
93 return ret;
94}
95
Stefan Reinauer2c3f2602012-12-19 11:22:07 -080096static inline char *strcpy(char *dst, const char *src)
Uwe Hermannaac8f662010-09-29 09:54:16 +000097{
Stefan Reinauer2c3f2602012-12-19 11:22:07 -080098 char *ptr = dst;
99
Uwe Hermannaac8f662010-09-29 09:54:16 +0000100 while (*src)
101 *dst++ = *src++;
Stefan Reinauer2c3f2602012-12-19 11:22:07 -0800102 *dst = '\0';
103
104 return ptr;
Uwe Hermannaac8f662010-09-29 09:54:16 +0000105}
106
Greg Watson16247822004-03-13 03:34:52 +0000107static inline int strcmp(const char *s1, const char *s2)
Stefan Reinauer14e22772010-04-27 06:56:47 +0000108{
Greg Watson16247822004-03-13 03:34:52 +0000109 int r;
110
111 while ((r = (*s1 - *s2)) == 0 && *s1) {
112 s1++;
113 s2++;
114 }
115 return r;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000116}
Greg Watson16247822004-03-13 03:34:52 +0000117
Stefan Reinauer36c83402009-03-01 10:16:01 +0000118static inline int strncmp(const char *s1, const char *s2, int maxlen)
119{
120 int i;
121
122 for (i = 0; i < maxlen; i++) {
Hannah Williams03d4ae72015-12-01 09:30:27 -0800123 if ((s1[i] != s2[i]) || (s1[i] == '\0'))
Stefan Reinauer36c83402009-03-01 10:16:01 +0000124 return s1[i] - s2[i];
125 }
126
127 return 0;
128}
129
Greg Watson16247822004-03-13 03:34:52 +0000130static 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 Huberf5b346c2017-11-01 13:21:40 +0100141static inline int isprint(int c)
142{
143 return c >= ' ' && c <= '~';
144}
145
Greg Watson16247822004-03-13 03:34:52 +0000146static inline int isdigit(int c)
147{
148 return (c >= '0' && c <= '9');
149}
150
151static 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 Watsonf6d05f52004-03-17 17:02:28 +0000158static inline int isupper(int c)
159{
160 return (c >= 'A' && c <= 'Z');
161}
162
Greg Watson16247822004-03-13 03:34:52 +0000163static inline int islower(int c)
164{
165 return (c >= 'a' && c <= 'z');
166}
167
168static inline int toupper(int c)
169{
Elyes HAOUAS8ffd0502016-09-01 19:01:41 +0200170 if (islower(c))
171 c -= 'a'-'A';
172 return c;
Greg Watson16247822004-03-13 03:34:52 +0000173}
Greg Watsonf6d05f52004-03-17 17:02:28 +0000174
175static inline int tolower(int c)
176{
Elyes HAOUAS8ffd0502016-09-01 19:01:41 +0200177 if (isupper(c))
178 c -= 'A'-'a';
179 return c;
Greg Watsonf6d05f52004-03-17 17:02:28 +0000180}
Eric Biederman8ca8d762003-04-22 19:02:15 +0000181#endif /* STRING_H */