intel/car/cache_as_ram_ht.inc: Prepare for dynamic CONFIG_RAMTOP
[coreboot.git] / src / include / string.h
blob125c676128d358f5b4f286820f848a7be7ecfe75
1 #ifndef STRING_H
2 #define STRING_H
4 #include <stddef.h>
5 #include <stdlib.h>
7 /* Stringify a token */
8 #ifndef STRINGIFY
9 #define _STRINGIFY(x) #x
10 #define STRINGIFY(x) _STRINGIFY(x)
11 #endif
13 void *memcpy(void *dest, const void *src, size_t n);
14 void *memmove(void *dest, const void *src, size_t n);
15 void *memset(void *s, int c, size_t n);
16 int memcmp(const void *s1, const void *s2, size_t n);
17 void *memchr(const void *s, int c, size_t n);
18 #if !defined(__PRE_RAM__)
19 int snprintf(char * buf, size_t size, const char *fmt, ...);
20 #endif
22 // simple string functions
24 static inline size_t strnlen(const char *src, size_t max)
26 size_t i = 0;
27 while((*src++) && (i < max)) {
28 i++;
30 return i;
33 static inline size_t strlen(const char *src)
35 size_t i = 0;
36 while(*src++) {
37 i++;
39 return i;
42 static inline char *strchr(const char *s, int c)
44 for (; *s; s++) {
45 if (*s == c)
46 return (char *) s;
48 return 0;
51 #if !defined(__PRE_RAM__)
52 static inline char *strdup(const char *s)
54 size_t sz = strlen(s) + 1;
55 char *d = malloc(sz);
56 memcpy(d, s, sz);
57 return d;
60 static inline char *strconcat(const char *s1, const char *s2)
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;
69 #endif
71 static inline char *strncpy(char *to, const char *from, int count)
73 register char *ret = to;
75 while (count > 0) {
76 count--;
77 if ((*to++ = *from++) == '\0')
78 break;
81 while (count > 0) {
82 count--;
83 *to++ = '\0';
85 return ret;
88 static inline char *strcpy(char *dst, const char *src)
90 char *ptr = dst;
92 while (*src)
93 *dst++ = *src++;
94 *dst = '\0';
96 return ptr;
99 static inline int strcmp(const char *s1, const char *s2)
101 int r;
103 while ((r = (*s1 - *s2)) == 0 && *s1) {
104 s1++;
105 s2++;
107 return r;
110 static inline int strncmp(const char *s1, const char *s2, int maxlen)
112 int i;
114 for (i = 0; i < maxlen; i++) {
115 if ((s1[i] != s2[i]) || (s1[i] == '\0'))
116 return s1[i] - s2[i];
119 return 0;
122 static inline int isspace(int c)
124 switch (c) {
125 case ' ': case '\f': case '\n':
126 case '\r': case '\t': case '\v':
127 return 1;
128 default:
129 return 0;
133 static inline int isdigit(int c)
135 return (c >= '0' && c <= '9');
138 static inline int isxdigit(int c)
140 return ((c >= '0' && c <= '9') ||
141 (c >= 'a' && c <= 'f') ||
142 (c >= 'A' && c <= 'F'));
145 static inline int isupper(int c)
147 return (c >= 'A' && c <= 'Z');
150 static inline int islower(int c)
152 return (c >= 'a' && c <= 'z');
155 static inline int toupper(int c)
157 if (islower(c))
158 c -= 'a'-'A';
159 return c;
162 static inline int tolower(int c)
164 if (isupper(c))
165 c -= 'A'-'a';
166 return c;
168 #endif /* STRING_H */