1 /*
2  * This file is part of the MicroPython project, http://micropython.org/
3  *
4  * The MIT License (MIT)
5  *
6  * Copyright (c) 2013, 2014 Damien P. George
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a copy
9  * of this software and associated documentation files (the "Software"), to deal
10  * in the Software without restriction, including without limitation the rights
11  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12  * copies of the Software, and to permit persons to whom the Software is
13  * furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24  * THE SOFTWARE.
25  */
26 
27 #include <stdint.h>
28 #include <string.h>
29 
30 #define likely(x) __builtin_expect((x), 1)
31 
memcpy(void * dst,const void * src,size_t n)32 void *memcpy(void *dst, const void *src, size_t n) {
33     if (likely(!(((uintptr_t)dst) & 3) && !(((uintptr_t)src) & 3))) {
34         // pointers aligned
35         uint32_t *d = dst;
36         const uint32_t *s = src;
37 
38         // copy words first
39         for (size_t i = (n >> 2); i; i--) {
40             *d++ = *s++;
41         }
42 
43         if (n & 2) {
44             // copy half-word
45             *(uint16_t*)d = *(const uint16_t*)s;
46             d = (uint32_t*)((uint16_t*)d + 1);
47             s = (const uint32_t*)((const uint16_t*)s + 1);
48         }
49 
50         if (n & 1) {
51             // copy byte
52             *((uint8_t*)d) = *((const uint8_t*)s);
53         }
54     } else {
55         // unaligned access, copy bytes
56         uint8_t *d = dst;
57         const uint8_t *s = src;
58 
59         for (; n; n--) {
60             *d++ = *s++;
61         }
62     }
63 
64     return dst;
65 }
66 
memmove(void * dest,const void * src,size_t n)67 void *memmove(void *dest, const void *src, size_t n) {
68     if (src < dest && (uint8_t*)dest < (const uint8_t*)src + n) {
69         // need to copy backwards
70         uint8_t *d = (uint8_t*)dest + n - 1;
71         const uint8_t *s = (const uint8_t*)src + n - 1;
72         for (; n > 0; n--) {
73             *d-- = *s--;
74         }
75         return dest;
76     } else {
77         // can use normal memcpy
78         return memcpy(dest, src, n);
79     }
80 }
81 
memset(void * s,int c,size_t n)82 void *memset(void *s, int c, size_t n) {
83     if (c == 0 && ((uintptr_t)s & 3) == 0) {
84         // aligned store of 0
85         uint32_t *s32 = s;
86         for (size_t i = n >> 2; i > 0; i--) {
87             *s32++ = 0;
88         }
89         if (n & 2) {
90             *((uint16_t*)s32) = 0;
91             s32 = (uint32_t*)((uint16_t*)s32 + 1);
92         }
93         if (n & 1) {
94             *((uint8_t*)s32) = 0;
95         }
96     } else {
97         uint8_t *s2 = s;
98         for (; n > 0; n--) {
99             *s2++ = c;
100         }
101     }
102     return s;
103 }
104 
memcmp(const void * s1,const void * s2,size_t n)105 int memcmp(const void *s1, const void *s2, size_t n) {
106     const uint8_t *s1_8 = s1;
107     const uint8_t *s2_8 = s2;
108     while (n--) {
109         char c1 = *s1_8++;
110         char c2 = *s2_8++;
111         if (c1 < c2) return -1;
112         else if (c1 > c2) return 1;
113     }
114     return 0;
115 }
116 
memchr(const void * s,int c,size_t n)117 void *memchr(const void *s, int c, size_t n) {
118     if (n != 0) {
119         const unsigned char *p = s;
120 
121         do {
122             if (*p++ == c)
123                 return ((void *)(p - 1));
124         } while (--n != 0);
125     }
126     return 0;
127 }
128 
strlen(const char * str)129 size_t strlen(const char *str) {
130     int len = 0;
131     for (const char *s = str; *s; s++) {
132         len += 1;
133     }
134     return len;
135 }
136 
strcmp(const char * s1,const char * s2)137 int strcmp(const char *s1, const char *s2) {
138     while (*s1 && *s2) {
139         char c1 = *s1++; // XXX UTF8 get char, next char
140         char c2 = *s2++; // XXX UTF8 get char, next char
141         if (c1 < c2) return -1;
142         else if (c1 > c2) return 1;
143     }
144     if (*s2) return -1;
145     else if (*s1) return 1;
146     else return 0;
147 }
148 
strncmp(const char * s1,const char * s2,size_t n)149 int strncmp(const char *s1, const char *s2, size_t n) {
150     while (*s1 && *s2 && n > 0) {
151         char c1 = *s1++; // XXX UTF8 get char, next char
152         char c2 = *s2++; // XXX UTF8 get char, next char
153         n--;
154         if (c1 < c2) return -1;
155         else if (c1 > c2) return 1;
156     }
157     if (n == 0) return 0;
158     else if (*s2) return -1;
159     else if (*s1) return 1;
160     else return 0;
161 }
162 
strcpy(char * dest,const char * src)163 char *strcpy(char *dest, const char *src) {
164     char *d = dest;
165     while (*src) {
166         *d++ = *src++;
167     }
168     *d = '\0';
169     return dest;
170 }
171 
172 // Public Domain implementation of strncpy from:
173 // http://en.wikibooks.org/wiki/C_Programming/Strings#The_strncpy_function
strncpy(char * s1,const char * s2,size_t n)174 char *strncpy(char *s1, const char *s2, size_t n) {
175      char *dst = s1;
176      const char *src = s2;
177      /* Copy bytes, one at a time.  */
178      while (n > 0) {
179          n--;
180          if ((*dst++ = *src++) == '\0') {
181              /* If we get here, we found a null character at the end
182                 of s2, so use memset to put null bytes at the end of
183                 s1.  */
184              memset(dst, '\0', n);
185              break;
186          }
187      }
188      return s1;
189  }
190 
191 // needed because gcc optimises strcpy + strcat to this
stpcpy(char * dest,const char * src)192 char *stpcpy(char *dest, const char *src) {
193     while (*src) {
194         *dest++ = *src++;
195     }
196     *dest = '\0';
197     return dest;
198 }
199 
strcat(char * dest,const char * src)200 char *strcat(char *dest, const char *src) {
201     char *d = dest;
202     while (*d) {
203         d++;
204     }
205     while (*src) {
206         *d++ = *src++;
207     }
208     *d = '\0';
209     return dest;
210 }
211 
212 // Public Domain implementation of strchr from:
213 // http://en.wikibooks.org/wiki/C_Programming/Strings#The_strchr_function
strchr(const char * s,int c)214 char *strchr(const char *s, int c)
215 {
216     /* Scan s for the character.  When this loop is finished,
217        s will either point to the end of the string or the
218        character we were looking for.  */
219     while (*s != '\0' && *s != (char)c)
220         s++;
221     return ((*s == c) ? (char *) s : 0);
222 }
223 
224 
225 // Public Domain implementation of strstr from:
226 // http://en.wikibooks.org/wiki/C_Programming/Strings#The_strstr_function
strstr(const char * haystack,const char * needle)227 char *strstr(const char *haystack, const char *needle)
228 {
229     size_t needlelen;
230     /* Check for the null needle case.  */
231     if (*needle == '\0')
232         return (char *) haystack;
233     needlelen = strlen(needle);
234     for (; (haystack = strchr(haystack, *needle)) != 0; haystack++)
235         if (strncmp(haystack, needle, needlelen) == 0)
236             return (char *) haystack;
237     return 0;
238 }
239 
strspn(const char * s,const char * accept)240 size_t strspn(const char *s, const char *accept) {
241     const char *ss = s;
242     while (*s && strchr(accept, *s) != NULL) {
243         ++s;
244     }
245     return s - ss;
246 }
247 
strcspn(const char * s,const char * reject)248 size_t strcspn(const char *s, const char *reject) {
249     const char *ss = s;
250     while (*s && strchr(reject, *s) == NULL) {
251         ++s;
252     }
253     return s - ss;
254 }
255