blob: dc125e19b0e76c82b409a0cdf35e441bbb6a3109 [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__)
Stefan Reinauera91e0fe2008-08-01 11:39:35 +000019int sprintf(char * buf, const char *fmt, ...);
Vladimir Serbinenko4b5012a2013-11-26 22:07:47 +010020int snprintf(char * buf, size_t size, const char *fmt, ...);
Stefan Reinauer6bd571e2009-09-25 21:59:57 +000021#endif
Eric Biederman8ca8d762003-04-22 19:02:15 +000022
Stefan Reinauer14e22772010-04-27 06:56:47 +000023// simple string functions
Eric Biederman8ca8d762003-04-22 19:02:15 +000024
Stefan Reinauer14e22772010-04-27 06:56:47 +000025static inline size_t strnlen(const char *src, size_t max)
26{
Eric Biederman8ca8d762003-04-22 19:02:15 +000027 size_t i = 0;
28 while((*src++) && (i < max)) {
29 i++;
30 }
31 return i;
32}
33
34static inline size_t strlen(const char *src)
35{
36 size_t i = 0;
37 while(*src++) {
38 i++;
39 }
40 return i;
41}
42
Greg Watson16247822004-03-13 03:34:52 +000043static inline char *strchr(const char *s, int c)
44{
45 for (; *s; s++) {
46 if (*s == c)
47 return (char *) s;
Stefan Reinauer14e22772010-04-27 06:56:47 +000048 }
Greg Watson16247822004-03-13 03:34:52 +000049 return 0;
50}
Eric Biederman8ca8d762003-04-22 19:02:15 +000051
Stefan Reinauer35b6bbb2010-03-28 21:26:54 +000052#if !defined(__PRE_RAM__)
Greg Watson16247822004-03-13 03:34:52 +000053static inline char *strdup(const char *s)
Stefan Reinauer14e22772010-04-27 06:56:47 +000054{
Greg Watson16247822004-03-13 03:34:52 +000055 size_t sz = strlen(s) + 1;
56 char *d = malloc(sz);
57 memcpy(d, s, sz);
58 return d;
59}
Stefan Reinauer951f5882009-07-19 00:18:15 +000060#endif
Greg Watson16247822004-03-13 03:34:52 +000061
62static inline char *strncpy(char *to, const char *from, int count)
63{
64 register char *ret = to;
65
66 while (count > 0) {
67 count--;
68 if ((*to++ = *from++) == '\0')
69 break;
70 }
71
72 while (count > 0) {
73 count--;
74 *to++ = '\0';
75 }
76 return ret;
77}
78
Stefan Reinauer2c3f2602012-12-19 11:22:07 -080079static inline char *strcpy(char *dst, const char *src)
Uwe Hermannaac8f662010-09-29 09:54:16 +000080{
Stefan Reinauer2c3f2602012-12-19 11:22:07 -080081 char *ptr = dst;
82
Uwe Hermannaac8f662010-09-29 09:54:16 +000083 while (*src)
84 *dst++ = *src++;
Stefan Reinauer2c3f2602012-12-19 11:22:07 -080085 *dst = '\0';
86
87 return ptr;
Uwe Hermannaac8f662010-09-29 09:54:16 +000088}
89
Greg Watson16247822004-03-13 03:34:52 +000090static inline int strcmp(const char *s1, const char *s2)
Stefan Reinauer14e22772010-04-27 06:56:47 +000091{
Greg Watson16247822004-03-13 03:34:52 +000092 int r;
93
94 while ((r = (*s1 - *s2)) == 0 && *s1) {
95 s1++;
96 s2++;
97 }
98 return r;
Stefan Reinauer14e22772010-04-27 06:56:47 +000099}
Greg Watson16247822004-03-13 03:34:52 +0000100
Stefan Reinauer36c83402009-03-01 10:16:01 +0000101static inline int strncmp(const char *s1, const char *s2, int maxlen)
102{
103 int i;
104
105 for (i = 0; i < maxlen; i++) {
106 if (s1[i] != s2[i])
107 return s1[i] - s2[i];
108 }
109
110 return 0;
111}
112
Greg Watson16247822004-03-13 03:34:52 +0000113static inline int isspace(int c)
114{
115 switch (c) {
116 case ' ': case '\f': case '\n':
117 case '\r': case '\t': case '\v':
118 return 1;
119 default:
120 return 0;
121 }
122}
123
124static inline int isdigit(int c)
125{
126 return (c >= '0' && c <= '9');
127}
128
129static inline int isxdigit(int c)
130{
131 return ((c >= '0' && c <= '9') ||
132 (c >= 'a' && c <= 'f') ||
133 (c >= 'A' && c <= 'F'));
134}
135
Greg Watsonf6d05f52004-03-17 17:02:28 +0000136static inline int isupper(int c)
137{
138 return (c >= 'A' && c <= 'Z');
139}
140
Greg Watson16247822004-03-13 03:34:52 +0000141static inline int islower(int c)
142{
143 return (c >= 'a' && c <= 'z');
144}
145
146static inline int toupper(int c)
147{
148 if (islower(c))
149 c -= 'a'-'A';
150 return c;
151}
Greg Watsonf6d05f52004-03-17 17:02:28 +0000152
153static inline int tolower(int c)
154{
155 if (isupper(c))
156 c -= 'A'-'a';
157 return c;
158}
Eric Biederman8ca8d762003-04-22 19:02:15 +0000159#endif /* STRING_H */