blob: d2d7bb87817dcb2d1128843b20435c8eea04ead1 [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
Aaron Durbinac4b00e2013-04-26 11:58:35 -05007/* Stringify a token */
8#ifndef STRINGIFY
9#define _STRINGIFY(x) #x
10#define STRINGIFY(x) _STRINGIFY(x)
11#endif
12
Stefan Reinauera91e0fe2008-08-01 11:39:35 +000013void *memcpy(void *dest, const void *src, size_t n);
14void *memmove(void *dest, const void *src, size_t n);
15void *memset(void *s, int c, size_t n);
16int memcmp(const void *s1, const void *s2, size_t n);
Gabe Black1025f3a2011-09-16 02:18:56 -070017void *memchr(const void *s, int c, size_t n);
Aaron Durbin5577a472016-11-08 09:42:13 -060018#if !defined(__ROMCC__)
Lee Leahy6d71a432017-03-07 15:24:16 -080019int snprintf(char *buf, size_t size, const char *fmt, ...);
Stefan Reinauer6bd571e2009-09-25 21:59:57 +000020#endif
Eric Biederman8ca8d762003-04-22 19:02:15 +000021
Stefan Reinauer14e22772010-04-27 06:56:47 +000022// simple string functions
Eric Biederman8ca8d762003-04-22 19:02:15 +000023
Stefan Reinauer14e22772010-04-27 06:56:47 +000024static inline size_t strnlen(const char *src, size_t max)
25{
Eric Biederman8ca8d762003-04-22 19:02:15 +000026 size_t i = 0;
Lee Leahybfdb8932017-03-07 15:17:04 -080027 while ((*src++) && (i < max))
Eric Biederman8ca8d762003-04-22 19:02:15 +000028 i++;
Eric Biederman8ca8d762003-04-22 19:02:15 +000029 return i;
30}
31
32static inline size_t strlen(const char *src)
33{
34 size_t i = 0;
Lee Leahybfdb8932017-03-07 15:17:04 -080035 while (*src++)
Eric Biederman8ca8d762003-04-22 19:02:15 +000036 i++;
Eric Biederman8ca8d762003-04-22 19:02:15 +000037 return i;
38}
39
Greg Watson16247822004-03-13 03:34:52 +000040static inline char *strchr(const char *s, int c)
41{
42 for (; *s; s++) {
43 if (*s == c)
44 return (char *) s;
Stefan Reinauer14e22772010-04-27 06:56:47 +000045 }
Greg Watson16247822004-03-13 03:34:52 +000046 return 0;
47}
Eric Biederman8ca8d762003-04-22 19:02:15 +000048
Stefan Reinauer35b6bbb2010-03-28 21:26:54 +000049#if !defined(__PRE_RAM__)
Greg Watson16247822004-03-13 03:34:52 +000050static inline char *strdup(const char *s)
Stefan Reinauer14e22772010-04-27 06:56:47 +000051{
Greg Watson16247822004-03-13 03:34:52 +000052 size_t sz = strlen(s) + 1;
53 char *d = malloc(sz);
54 memcpy(d, s, sz);
55 return d;
56}
Kyösti Mälkkic36af7b2014-11-18 12:41:16 +020057
58static 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 Reinauer951f5882009-07-19 00:18:15 +000067#endif
Greg Watson16247822004-03-13 03:34:52 +000068
Patrick Rudolph03c7b052018-03-29 10:05:34 +020069/**
70 * Find a character in a string.
71 *
72 * @param s The string.
73 * @param c The character.
74 * @return A pointer to the last occurrence of the character in the
75 * string, or NULL if the character was not encountered within the string.
76 */
77static inline char *strrchr(const char *s, int c)
78{
79 char *p = (char *)s + strlen(s);
80
81 for (; p >= s; p--) {
82 if (*p == c)
83 return p;
84 }
85
86 return NULL;
87}
88
Greg Watson16247822004-03-13 03:34:52 +000089static inline char *strncpy(char *to, const char *from, int count)
90{
91 register char *ret = to;
Lee Leahyb1260552017-03-07 16:01:09 -080092 register char data;
Greg Watson16247822004-03-13 03:34:52 +000093
94 while (count > 0) {
95 count--;
Lee Leahyb1260552017-03-07 16:01:09 -080096 data = *from++;
97 *to++ = data;
98 if (data == '\0')
Greg Watson16247822004-03-13 03:34:52 +000099 break;
100 }
101
102 while (count > 0) {
103 count--;
104 *to++ = '\0';
105 }
106 return ret;
107}
108
Stefan Reinauer2c3f2602012-12-19 11:22:07 -0800109static inline char *strcpy(char *dst, const char *src)
Uwe Hermannaac8f662010-09-29 09:54:16 +0000110{
Stefan Reinauer2c3f2602012-12-19 11:22:07 -0800111 char *ptr = dst;
112
Uwe Hermannaac8f662010-09-29 09:54:16 +0000113 while (*src)
114 *dst++ = *src++;
Stefan Reinauer2c3f2602012-12-19 11:22:07 -0800115 *dst = '\0';
116
117 return ptr;
Uwe Hermannaac8f662010-09-29 09:54:16 +0000118}
119
Greg Watson16247822004-03-13 03:34:52 +0000120static inline int strcmp(const char *s1, const char *s2)
Stefan Reinauer14e22772010-04-27 06:56:47 +0000121{
Greg Watson16247822004-03-13 03:34:52 +0000122 int r;
123
124 while ((r = (*s1 - *s2)) == 0 && *s1) {
125 s1++;
126 s2++;
127 }
128 return r;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000129}
Greg Watson16247822004-03-13 03:34:52 +0000130
Stefan Reinauer36c83402009-03-01 10:16:01 +0000131static inline int strncmp(const char *s1, const char *s2, int maxlen)
132{
133 int i;
134
135 for (i = 0; i < maxlen; i++) {
Hannah Williams03d4ae72015-12-01 09:30:27 -0800136 if ((s1[i] != s2[i]) || (s1[i] == '\0'))
Stefan Reinauer36c83402009-03-01 10:16:01 +0000137 return s1[i] - s2[i];
138 }
139
140 return 0;
141}
142
Greg Watson16247822004-03-13 03:34:52 +0000143static inline int isspace(int c)
144{
145 switch (c) {
146 case ' ': case '\f': case '\n':
147 case '\r': case '\t': case '\v':
148 return 1;
149 default:
150 return 0;
151 }
152}
153
154static inline int isdigit(int c)
155{
156 return (c >= '0' && c <= '9');
157}
158
159static inline int isxdigit(int c)
160{
161 return ((c >= '0' && c <= '9') ||
162 (c >= 'a' && c <= 'f') ||
163 (c >= 'A' && c <= 'F'));
164}
165
Greg Watsonf6d05f52004-03-17 17:02:28 +0000166static inline int isupper(int c)
167{
168 return (c >= 'A' && c <= 'Z');
169}
170
Greg Watson16247822004-03-13 03:34:52 +0000171static inline int islower(int c)
172{
173 return (c >= 'a' && c <= 'z');
174}
175
176static inline int toupper(int c)
177{
Elyes HAOUAS8ffd0502016-09-01 19:01:41 +0200178 if (islower(c))
179 c -= 'a'-'A';
180 return c;
Greg Watson16247822004-03-13 03:34:52 +0000181}
Greg Watsonf6d05f52004-03-17 17:02:28 +0000182
183static inline int tolower(int c)
184{
Elyes HAOUAS8ffd0502016-09-01 19:01:41 +0200185 if (isupper(c))
186 c -= 'A'-'a';
187 return c;
Greg Watsonf6d05f52004-03-17 17:02:28 +0000188}
Eric Biederman8ca8d762003-04-22 19:02:15 +0000189#endif /* STRING_H */