Factor out fill_processor_name() and strcpy() functions.
[coreboot.git] / src / include / string.h
1 #ifndef STRING_H
2 #define STRING_H
3
4 #include <stddef.h>
5 #include <stdlib.h>
6
7 void *memcpy(void *dest, const void *src, size_t n);
8 void *memmove(void *dest, const void *src, size_t n);
9 void *memset(void *s, int c, size_t n);
10 int memcmp(const void *s1, const void *s2, size_t n);
11 #if !defined(__PRE_RAM__)
12 int sprintf(char * buf, const char *fmt, ...);
13 #endif
14
15 // simple string functions
16
17 static inline size_t strnlen(const char *src, size_t max)
18 {
19         size_t i = 0;
20         while((*src++) && (i < max)) {
21                 i++;
22         }
23         return i;
24 }
25
26 static inline size_t strlen(const char *src)
27 {
28         size_t i = 0;
29         while(*src++) {
30                 i++;
31         }
32         return i;
33 }
34
35 static inline char *strchr(const char *s, int c)
36 {
37         for (; *s; s++) {
38                 if (*s == c)
39                         return (char *) s;
40         }
41         return 0;
42 }
43
44 #if !defined(__PRE_RAM__)
45 static inline char *strdup(const char *s)
46 {
47         size_t sz = strlen(s) + 1;
48         char *d = malloc(sz);
49         memcpy(d, s, sz);
50         return d;
51 }
52 #endif
53
54 static inline char *strncpy(char *to, const char *from, int count)
55 {
56         register char *ret = to;
57
58         while (count > 0) {
59                 count--;
60                 if ((*to++ = *from++) == '\0')
61                         break;
62         }
63
64         while (count > 0) {
65                 count--;
66                 *to++ = '\0';
67         }
68         return ret;
69 }
70
71 static inline void strcpy(char *dst, const char *src)
72 {
73         while (*src)
74                 *dst++ = *src++;
75 }
76
77 static inline int strcmp(const char *s1, const char *s2)
78 {
79         int r;
80
81         while ((r = (*s1 - *s2)) == 0 && *s1) {
82                 s1++;
83                 s2++;
84         }
85         return r;
86 }
87
88 static inline int strncmp(const char *s1, const char *s2, int maxlen)
89 {
90         int i;
91
92         for (i = 0; i < maxlen; i++) {
93                 if (s1[i] != s2[i])
94                         return s1[i] - s2[i];
95         }
96
97         return 0;
98 }
99
100 static inline int isspace(int c)
101 {
102         switch (c) {
103         case ' ': case '\f': case '\n':
104         case '\r': case '\t': case '\v':
105                 return 1;
106         default:
107                 return 0;
108         }
109 }
110
111 static inline int isdigit(int c)
112 {
113         return (c >= '0' && c <= '9');
114 }
115
116 static inline int isxdigit(int c)
117 {
118         return ((c >= '0' && c <= '9') ||
119                 (c >= 'a' && c <= 'f') ||
120                 (c >= 'A' && c <= 'F'));
121 }
122
123 static inline int isupper(int c)
124 {
125         return (c >= 'A' && c <= 'Z');
126 }
127
128 static inline int islower(int c)
129 {
130         return (c >= 'a' && c <= 'z');
131 }
132
133 static inline int toupper(int c)
134 {
135         if (islower(c))
136                 c -= 'a'-'A';
137         return c;
138 }
139
140 static inline int tolower(int c)
141 {
142         if (isupper(c))
143                 c -= 'A'-'a';
144         return c;
145 }
146 #endif /* STRING_H */