blob: 125c676128d358f5b4f286820f848a7be7ecfe75 [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}
Kyösti Mälkkic36af7b2014-11-18 12:41:16 +020059
60static inline char *strconcat(const char *s1, const char *s2)
61{
62 size_t sz_1 = strlen(s1);
63 size_t sz_2 = strlen(s2);
64 char *d = malloc(sz_1 + sz_2 + 1);
65 memcpy(d, s1, sz_1);
66 memcpy(d + sz_1, s2, sz_2 + 1);
67 return d;
68}
Stefan Reinauer951f5882009-07-19 00:18:15 +000069#endif
Greg Watson16247822004-03-13 03:34:52 +000070
71static inline char *strncpy(char *to, const char *from, int count)
72{
73 register char *ret = to;
74
75 while (count > 0) {
76 count--;
77 if ((*to++ = *from++) == '\0')
78 break;
79 }
80
81 while (count > 0) {
82 count--;
83 *to++ = '\0';
84 }
85 return ret;
86}
87
Stefan Reinauer2c3f2602012-12-19 11:22:07 -080088static inline char *strcpy(char *dst, const char *src)
Uwe Hermannaac8f662010-09-29 09:54:16 +000089{
Stefan Reinauer2c3f2602012-12-19 11:22:07 -080090 char *ptr = dst;
91
Uwe Hermannaac8f662010-09-29 09:54:16 +000092 while (*src)
93 *dst++ = *src++;
Stefan Reinauer2c3f2602012-12-19 11:22:07 -080094 *dst = '\0';
95
96 return ptr;
Uwe Hermannaac8f662010-09-29 09:54:16 +000097}
98
Greg Watson16247822004-03-13 03:34:52 +000099static inline int strcmp(const char *s1, const char *s2)
Stefan Reinauer14e22772010-04-27 06:56:47 +0000100{
Greg Watson16247822004-03-13 03:34:52 +0000101 int r;
102
103 while ((r = (*s1 - *s2)) == 0 && *s1) {
104 s1++;
105 s2++;
106 }
107 return r;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000108}
Greg Watson16247822004-03-13 03:34:52 +0000109
Stefan Reinauer36c83402009-03-01 10:16:01 +0000110static inline int strncmp(const char *s1, const char *s2, int maxlen)
111{
112 int i;
113
114 for (i = 0; i < maxlen; i++) {
Hannah Williams03d4ae72015-12-01 09:30:27 -0800115 if ((s1[i] != s2[i]) || (s1[i] == '\0'))
Stefan Reinauer36c83402009-03-01 10:16:01 +0000116 return s1[i] - s2[i];
117 }
118
119 return 0;
120}
121
Greg Watson16247822004-03-13 03:34:52 +0000122static inline int isspace(int c)
123{
124 switch (c) {
125 case ' ': case '\f': case '\n':
126 case '\r': case '\t': case '\v':
127 return 1;
128 default:
129 return 0;
130 }
131}
132
133static inline int isdigit(int c)
134{
135 return (c >= '0' && c <= '9');
136}
137
138static inline int isxdigit(int c)
139{
140 return ((c >= '0' && c <= '9') ||
141 (c >= 'a' && c <= 'f') ||
142 (c >= 'A' && c <= 'F'));
143}
144
Greg Watsonf6d05f52004-03-17 17:02:28 +0000145static inline int isupper(int c)
146{
147 return (c >= 'A' && c <= 'Z');
148}
149
Greg Watson16247822004-03-13 03:34:52 +0000150static inline int islower(int c)
151{
152 return (c >= 'a' && c <= 'z');
153}
154
155static inline int toupper(int c)
156{
157 if (islower(c))
158 c -= 'a'-'A';
159 return c;
160}
Greg Watsonf6d05f52004-03-17 17:02:28 +0000161
162static inline int tolower(int c)
163{
164 if (isupper(c))
165 c -= 'A'-'a';
166 return c;
167}
Eric Biederman8ca8d762003-04-22 19:02:15 +0000168#endif /* STRING_H */