blob: b4d0268419bb041a21ebe90180871332475bf63a [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);
Stefan Reinauer35b6bbb2010-03-28 21:26:54 +000018#if !defined(__PRE_RAM__)
Vladimir Serbinenko4b5012a2013-11-26 22:07:47 +010019int 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;
27 while((*src++) && (i < max)) {
28 i++;
29 }
30 return i;
31}
32
33static inline size_t strlen(const char *src)
34{
35 size_t i = 0;
36 while(*src++) {
37 i++;
38 }
39 return i;
40}
41
Greg Watson16247822004-03-13 03:34:52 +000042static inline char *strchr(const char *s, int c)
43{
44 for (; *s; s++) {
45 if (*s == c)
46 return (char *) s;
Stefan Reinauer14e22772010-04-27 06:56:47 +000047 }
Greg Watson16247822004-03-13 03:34:52 +000048 return 0;
49}
Eric Biederman8ca8d762003-04-22 19:02:15 +000050
Stefan Reinauer35b6bbb2010-03-28 21:26:54 +000051#if !defined(__PRE_RAM__)
Greg Watson16247822004-03-13 03:34:52 +000052static inline char *strdup(const char *s)
Stefan Reinauer14e22772010-04-27 06:56:47 +000053{
Greg Watson16247822004-03-13 03:34:52 +000054 size_t sz = strlen(s) + 1;
55 char *d = malloc(sz);
56 memcpy(d, s, sz);
57 return d;
58}
Stefan Reinauer951f5882009-07-19 00:18:15 +000059#endif
Greg Watson16247822004-03-13 03:34:52 +000060
61static inline char *strncpy(char *to, const char *from, int count)
62{
63 register char *ret = to;
64
65 while (count > 0) {
66 count--;
67 if ((*to++ = *from++) == '\0')
68 break;
69 }
70
71 while (count > 0) {
72 count--;
73 *to++ = '\0';
74 }
75 return ret;
76}
77
Stefan Reinauer2c3f2602012-12-19 11:22:07 -080078static inline char *strcpy(char *dst, const char *src)
Uwe Hermannaac8f662010-09-29 09:54:16 +000079{
Stefan Reinauer2c3f2602012-12-19 11:22:07 -080080 char *ptr = dst;
81
Uwe Hermannaac8f662010-09-29 09:54:16 +000082 while (*src)
83 *dst++ = *src++;
Stefan Reinauer2c3f2602012-12-19 11:22:07 -080084 *dst = '\0';
85
86 return ptr;
Uwe Hermannaac8f662010-09-29 09:54:16 +000087}
88
Greg Watson16247822004-03-13 03:34:52 +000089static inline int strcmp(const char *s1, const char *s2)
Stefan Reinauer14e22772010-04-27 06:56:47 +000090{
Greg Watson16247822004-03-13 03:34:52 +000091 int r;
92
93 while ((r = (*s1 - *s2)) == 0 && *s1) {
94 s1++;
95 s2++;
96 }
97 return r;
Stefan Reinauer14e22772010-04-27 06:56:47 +000098}
Greg Watson16247822004-03-13 03:34:52 +000099
Stefan Reinauer36c83402009-03-01 10:16:01 +0000100static inline int strncmp(const char *s1, const char *s2, int maxlen)
101{
102 int i;
103
104 for (i = 0; i < maxlen; i++) {
105 if (s1[i] != s2[i])
106 return s1[i] - s2[i];
107 }
108
109 return 0;
110}
111
Greg Watson16247822004-03-13 03:34:52 +0000112static inline int isspace(int c)
113{
114 switch (c) {
115 case ' ': case '\f': case '\n':
116 case '\r': case '\t': case '\v':
117 return 1;
118 default:
119 return 0;
120 }
121}
122
123static inline int isdigit(int c)
124{
125 return (c >= '0' && c <= '9');
126}
127
128static inline int isxdigit(int c)
129{
130 return ((c >= '0' && c <= '9') ||
131 (c >= 'a' && c <= 'f') ||
132 (c >= 'A' && c <= 'F'));
133}
134
Greg Watsonf6d05f52004-03-17 17:02:28 +0000135static inline int isupper(int c)
136{
137 return (c >= 'A' && c <= 'Z');
138}
139
Greg Watson16247822004-03-13 03:34:52 +0000140static inline int islower(int c)
141{
142 return (c >= 'a' && c <= 'z');
143}
144
145static inline int toupper(int c)
146{
147 if (islower(c))
148 c -= 'a'-'A';
149 return c;
150}
Greg Watsonf6d05f52004-03-17 17:02:28 +0000151
152static inline int tolower(int c)
153{
154 if (isupper(c))
155 c -= 'A'-'a';
156 return c;
157}
Eric Biederman8ca8d762003-04-22 19:02:15 +0000158#endif /* STRING_H */