console: Move poor-man's atoi() into string.h
[coreboot.git] / src / include / string.h
blobc56a760a047e8f15750949c318132dd365b2b46e
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
26 char *strdup(const char *s);
27 char *strconcat(const char *s1, const char *s2);
29 // simple string functions
31 static inline size_t strnlen(const char *src, size_t max)
33 size_t i = 0;
34 while ((*src++) && (i < max))
35 i++;
36 return i;
39 static inline size_t strlen(const char *src)
41 size_t i = 0;
42 while (*src++)
43 i++;
44 return i;
47 static inline char *strchr(const char *s, int c)
49 for (; *s; s++) {
50 if (*s == c)
51 return (char *) s;
53 return 0;
56 /**
57 * Find a character in a string.
59 * @param s The string.
60 * @param c The character.
61 * @return A pointer to the last occurrence of the character in the
62 * string, or NULL if the character was not encountered within the string.
64 static inline char *strrchr(const char *s, int c)
66 char *p = (char *)s + strlen(s);
68 for (; p >= s; p--) {
69 if (*p == c)
70 return p;
73 return NULL;
76 static inline char *strncpy(char *to, const char *from, int count)
78 register char *ret = to;
79 register char data;
81 while (count > 0) {
82 count--;
83 data = *from++;
84 *to++ = data;
85 if (data == '\0')
86 break;
89 while (count > 0) {
90 count--;
91 *to++ = '\0';
93 return ret;
96 static inline char *strcpy(char *dst, const char *src)
98 char *ptr = dst;
100 while (*src)
101 *dst++ = *src++;
102 *dst = '\0';
104 return ptr;
107 static inline int strcmp(const char *s1, const char *s2)
109 int r;
111 while ((r = (*s1 - *s2)) == 0 && *s1) {
112 s1++;
113 s2++;
115 return r;
118 static inline int strncmp(const char *s1, const char *s2, int maxlen)
120 int i;
122 for (i = 0; i < maxlen; i++) {
123 if ((s1[i] != s2[i]) || (s1[i] == '\0'))
124 return s1[i] - s2[i];
127 return 0;
130 static inline int isspace(int c)
132 switch (c) {
133 case ' ': case '\f': case '\n':
134 case '\r': case '\t': case '\v':
135 return 1;
136 default:
137 return 0;
141 static inline int isprint(int c)
143 return c >= ' ' && c <= '~';
146 static inline int isdigit(int c)
148 return (c >= '0' && c <= '9');
151 static inline int isxdigit(int c)
153 return ((c >= '0' && c <= '9') ||
154 (c >= 'a' && c <= 'f') ||
155 (c >= 'A' && c <= 'F'));
158 static inline int isupper(int c)
160 return (c >= 'A' && c <= 'Z');
163 static inline int islower(int c)
165 return (c >= 'a' && c <= 'z');
168 static inline int toupper(int c)
170 if (islower(c))
171 c -= 'a'-'A';
172 return c;
175 static inline int tolower(int c)
177 if (isupper(c))
178 c -= 'A'-'a';
179 return c;
183 * Parses an unsigned integer and moves the input pointer forward to the first
184 * character that's not a valid digit. s and *s must not be NULL. Result
185 * undefined if it overruns the return type size.
187 static inline unsigned int skip_atoi(char **s)
189 unsigned int i = 0;
191 while (isdigit(**s))
192 i = i*10 + *((*s)++) - '0';
193 return i;
196 #endif /* STRING_H */