ec/google/chromeec: Update Tablet event call
[coreboot.git] / src / include / string.h
blobfc96393c48f6561e22d3097c0056112351c0477f
1 #ifndef STRING_H
2 #define STRING_H
4 #include <stddef.h>
5 #include <stdlib.h>
7 #if !defined(__ROMCC__)
8 #include <console/vtxprintf.h>
9 #endif
11 /* Stringify a token */
12 #ifndef STRINGIFY
13 #define _STRINGIFY(x) #x
14 #define STRINGIFY(x) _STRINGIFY(x)
15 #endif
17 void *memcpy(void *dest, const void *src, size_t n);
18 void *memmove(void *dest, const void *src, size_t n);
19 void *memset(void *s, int c, size_t n);
20 int memcmp(const void *s1, const void *s2, size_t n);
21 void *memchr(const void *s, int c, size_t n);
22 #if !defined(__ROMCC__)
23 int snprintf(char *buf, size_t size, const char *fmt, ...);
24 int vsnprintf(char *buf, size_t size, const char *fmt, va_list args);
25 #endif
27 // simple string functions
29 static inline size_t strnlen(const char *src, size_t max)
31 size_t i = 0;
32 while ((*src++) && (i < max))
33 i++;
34 return i;
37 static inline size_t strlen(const char *src)
39 size_t i = 0;
40 while (*src++)
41 i++;
42 return i;
45 static inline char *strchr(const char *s, int c)
47 for (; *s; s++) {
48 if (*s == c)
49 return (char *) s;
51 return 0;
54 #if !defined(__PRE_RAM__)
55 static inline char *strdup(const char *s)
57 size_t sz = strlen(s) + 1;
58 char *d = malloc(sz);
59 memcpy(d, s, sz);
60 return d;
63 static inline char *strconcat(const char *s1, const char *s2)
65 size_t sz_1 = strlen(s1);
66 size_t sz_2 = strlen(s2);
67 char *d = malloc(sz_1 + sz_2 + 1);
68 memcpy(d, s1, sz_1);
69 memcpy(d + sz_1, s2, sz_2 + 1);
70 return d;
72 #endif
74 /**
75 * Find a character in a string.
77 * @param s The string.
78 * @param c The character.
79 * @return A pointer to the last occurrence of the character in the
80 * string, or NULL if the character was not encountered within the string.
82 static inline char *strrchr(const char *s, int c)
84 char *p = (char *)s + strlen(s);
86 for (; p >= s; p--) {
87 if (*p == c)
88 return p;
91 return NULL;
94 static inline char *strncpy(char *to, const char *from, int count)
96 register char *ret = to;
97 register char data;
99 while (count > 0) {
100 count--;
101 data = *from++;
102 *to++ = data;
103 if (data == '\0')
104 break;
107 while (count > 0) {
108 count--;
109 *to++ = '\0';
111 return ret;
114 static inline char *strcpy(char *dst, const char *src)
116 char *ptr = dst;
118 while (*src)
119 *dst++ = *src++;
120 *dst = '\0';
122 return ptr;
125 static inline int strcmp(const char *s1, const char *s2)
127 int r;
129 while ((r = (*s1 - *s2)) == 0 && *s1) {
130 s1++;
131 s2++;
133 return r;
136 static inline int strncmp(const char *s1, const char *s2, int maxlen)
138 int i;
140 for (i = 0; i < maxlen; i++) {
141 if ((s1[i] != s2[i]) || (s1[i] == '\0'))
142 return s1[i] - s2[i];
145 return 0;
148 static inline int isspace(int c)
150 switch (c) {
151 case ' ': case '\f': case '\n':
152 case '\r': case '\t': case '\v':
153 return 1;
154 default:
155 return 0;
159 static inline int isdigit(int c)
161 return (c >= '0' && c <= '9');
164 static inline int isxdigit(int c)
166 return ((c >= '0' && c <= '9') ||
167 (c >= 'a' && c <= 'f') ||
168 (c >= 'A' && c <= 'F'));
171 static inline int isupper(int c)
173 return (c >= 'A' && c <= 'Z');
176 static inline int islower(int c)
178 return (c >= 'a' && c <= 'z');
181 static inline int toupper(int c)
183 if (islower(c))
184 c -= 'a'-'A';
185 return c;
188 static inline int tolower(int c)
190 if (isupper(c))
191 c -= 'A'-'a';
192 return c;
194 #endif /* STRING_H */