summaryrefslogtreecommitdiffstats
path: root/src/lib/string.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/string.c')
-rw-r--r--src/lib/string.c392
1 files changed, 392 insertions, 0 deletions
diff --git a/src/lib/string.c b/src/lib/string.c
new file mode 100644
index 0000000..cca9e60
--- /dev/null
+++ b/src/lib/string.c
@@ -0,0 +1,392 @@
1/*
2 * linux/lib/string.c
3 *
4 * (C) 1991 Linus Torvalds
5 */
6
7#ifndef __GNUC__
8#error I want gcc!
9#endif
10
11#define extern
12#define inline
13#define static
14#define __LIBRARY__
15#include <string.h>
16
17inline char * strcpy(char * dest,const char *src)
18{
19 __asm__("cld\n"
20 "1:\tlodsb\n\t"
21 "stosb\n\t"
22 "testb %%al,%%al\n\t"
23 "jne 1b"
24 ::"S" (src),"D" (dest));
25 return dest;
26}
27
28static inline char * strncpy(char * dest,const char *src,int count)
29{
30 __asm__("cld\n"
31 "1:\tdecl %2\n\t"
32 "js 2f\n\t"
33 "lodsb\n\t"
34 "stosb\n\t"
35 "testb %%al,%%al\n\t"
36 "jne 1b\n\t"
37 "rep\n\t"
38 "stosb\n"
39 "2:"
40 ::"S" (src),"D" (dest),"c" (count));
41 return dest;
42}
43
44inline char * strcat(char * dest,const char * src)
45{
46 __asm__("cld\n\t"
47 "repne\n\t"
48 "scasb\n\t"
49 "decl %1\n"
50 "1:\tlodsb\n\t"
51 "stosb\n\t"
52 "testb %%al,%%al\n\t"
53 "jne 1b"
54 ::"S" (src),"D" (dest),"a" (0),"c" (0xffffffff));
55 return dest;
56}
57
58static inline char * strncat(char * dest,const char * src,int count)
59{
60 __asm__("cld\n\t"
61 "repne\n\t"
62 "scasb\n\t"
63 "decl %1\n\t"
64 "movl %4,%3\n"
65 "1:\tdecl %3\n\t"
66 "js 2f\n\t"
67 "lodsb\n\t"
68 "stosb\n\t"
69 "testb %%al,%%al\n\t"
70 "jne 1b\n"
71 "2:\txorl %2,%2\n\t"
72 "stosb"
73 ::"S" (src),"D" (dest),"a" (0),"c" (0xffffffff),"g" (count)
74 );
75 return dest;
76}
77
78inline int strcmp(const char * cs,const char * ct)
79{
80 register int __res ;
81 __asm__("cld\n"
82 "1:\tlodsb\n\t"
83 "scasb\n\t"
84 "jne 2f\n\t"
85 "testb %%al,%%al\n\t"
86 "jne 1b\n\t"
87 "xorl %%eax,%%eax\n\t"
88 "jmp 3f\n"
89 "2:\tmovl $1,%%eax\n\t"
90 "jl 3f\n\t"
91 "negl %%eax\n"
92 "3:"
93 :"=a" (__res):"D" (cs),"S" (ct));
94 return __res;
95}
96
97
98static inline int strncmp(const char * cs,const char * ct,int count)
99{
100 register int __res ;
101 __asm__("cld\n"
102 "1:\tdecl %3\n\t"
103 "js 2f\n\t"
104 "lodsb\n\t"
105 "scasb\n\t"
106 "jne 3f\n\t"
107 "testb %%al,%%al\n\t"
108 "jne 1b\n"
109 "2:\txorl %%eax,%%eax\n\t"
110 "jmp 4f\n"
111 "3:\tmovl $1,%%eax\n\t"
112 "jl 4f\n\t"
113 "negl %%eax\n"
114 "4:"
115 :"=a" (__res):"D" (cs),"S" (ct),"c" (count));
116 return __res;
117}
118
119static inline char * strchr(const char * s,char c)
120{
121 register char * __res ;
122 __asm__("cld\n\t"
123 "movb %%al,%%ah\n"
124 "1:\tlodsb\n\t"
125 "cmpb %%ah,%%al\n\t"
126 "je 2f\n\t"
127 "testb %%al,%%al\n\t"
128 "jne 1b\n\t"
129 "movl $1,%1\n"
130 "2:\tmovl %1,%0\n\t"
131 "decl %0"
132 :"=a" (__res):"S" (s),"0" (c));
133 return __res;
134}
135
136static inline char * strrchr(const char * s,char c)
137{
138 register char * __res;
139 __asm__("cld\n\t"
140 "movb %%al,%%ah\n"
141 "1:\tlodsb\n\t"
142 "cmpb %%ah,%%al\n\t"
143 "jne 2f\n\t"
144 "movl %%esi,%0\n\t"
145 "decl %0\n"
146 "2:\ttestb %%al,%%al\n\t"
147 "jne 1b"
148 :"=d" (__res):"0" (0),"S" (s),"a" (c));
149 return __res;
150}
151
152inline int strspn(const char * cs, const char * ct)
153{
154 register char * __res;
155 __asm__("cld\n\t"
156 "movl %4,%%edi\n\t"
157 "repne\n\t"
158 "scasb\n\t"
159 "notl %%ecx\n\t"
160 "decl %%ecx\n\t"
161 "movl %%ecx,%%edx\n"
162 "1:\tlodsb\n\t"
163 "testb %%al,%%al\n\t"
164 "je 2f\n\t"
165 "movl %4,%%edi\n\t"
166 "movl %%edx,%%ecx\n\t"
167 "repne\n\t"
168 "scasb\n\t"
169 "je 1b\n"
170 "2:\tdecl %0"
171 :"=S" (__res):"a" (0),"c" (0xffffffff),"0" (cs),"g" (ct)
172 );
173 return __res-cs;
174}
175
176inline int strcspn(const char * cs, const char * ct)
177{
178 register char * __res;
179 __asm__("cld\n\t"
180 "movl %4,%%edi\n\t"
181 "repne\n\t"
182 "scasb\n\t"
183 "notl %%ecx\n\t"
184 "decl %%ecx\n\t"
185 "movl %%ecx,%%edx\n"
186 "1:\tlodsb\n\t"
187 "testb %%al,%%al\n\t"
188 "je 2f\n\t"
189 "movl %4,%%edi\n\t"
190 "movl %%edx,%%ecx\n\t"
191 "repne\n\t"
192 "scasb\n\t"
193 "jne 1b\n"
194 "2:\tdecl %0"
195 :"=S" (__res):"a" (0),"c" (0xffffffff),"0" (cs),"g" (ct)
196 );
197 return __res-cs;
198}
199
200inline char * strpbrk(const char * cs,const char * ct)
201{
202 register char * __res ;
203 __asm__("cld\n\t"
204 "movl %4,%%edi\n\t"
205 "repne\n\t"
206 "scasb\n\t"
207 "notl %%ecx\n\t"
208 "decl %%ecx\n\t"
209 "movl %%ecx,%%edx\n"
210 "1:\tlodsb\n\t"
211 "testb %%al,%%al\n\t"
212 "je 2f\n\t"
213 "movl %4,%%edi\n\t"
214 "movl %%edx,%%ecx\n\t"
215 "repne\n\t"
216 "scasb\n\t"
217 "jne 1b\n\t"
218 "decl %0\n\t"
219 "jmp 3f\n"
220 "2:\txorl %0,%0\n"
221 "3:"
222 :"=S" (__res):"a" (0),"c" (0xffffffff),"0" (cs),"g" (ct)
223 );
224 return __res;
225}
226
227inline char * strstr(const char * cs,const char * ct)
228{
229 register char * __res ;
230 __asm__("cld\n\t" \
231 "movl %4,%%edi\n\t"
232 "repne\n\t"
233 "scasb\n\t"
234 "notl %%ecx\n\t"
235 "decl %%ecx\n\t" /* NOTE! This also sets Z if searchstring='' */
236 "movl %%ecx,%%edx\n"
237 "1:\tmovl %4,%%edi\n\t"
238 "movl %%esi,%%eax\n\t"
239 "movl %%edx,%%ecx\n\t"
240 "repe\n\t"
241 "cmpsb\n\t"
242 "je 2f\n\t" /* also works for empty string, see above */
243 "xchgl %%eax,%%esi\n\t"
244 "incl %%esi\n\t"
245 "cmpb $0,-1(%%eax)\n\t"
246 "jne 1b\n\t"
247 "xorl %%eax,%%eax\n\t"
248 "2:"
249 :"=a" (__res):"0" (0),"c" (0xffffffff),"S" (cs),"g" (ct)
250 );
251 return __res;
252}
253
254inline int strlen(const char * s)
255{
256 register int __res ;
257 __asm__("cld\n\t"
258 "repne\n\t"
259 "scasb\n\t"
260 "notl %0\n\t"
261 "decl %0"
262 :"=c" (__res):"D" (s),"a" (0),"0" (0xffffffff));
263 return __res;
264}
265
266inline char * strtok(char * s,const char * ct)
267{
268 register char * __res ;
269 __asm__("testl %1,%1\n\t"
270 "jne 1f\n\t"
271 "testl %0,%0\n\t"
272 "je 8f\n\t"
273 "movl %0,%1\n"
274 "1:\txorl %0,%0\n\t"
275 "movl $-1,%%ecx\n\t"
276 "xorl %%eax,%%eax\n\t"
277 "cld\n\t"
278 "movl %4,%%edi\n\t"
279 "repne\n\t"
280 "scasb\n\t"
281 "notl %%ecx\n\t"
282 "decl %%ecx\n\t"
283 "je 7f\n\t" /* empty delimeter-string */
284 "movl %%ecx,%%edx\n"
285 "2:\tlodsb\n\t"
286 "testb %%al,%%al\n\t"
287 "je 7f\n\t"
288 "movl %4,%%edi\n\t"
289 "movl %%edx,%%ecx\n\t"
290 "repne\n\t"
291 "scasb\n\t"
292 "je 2b\n\t"
293 "decl %1\n\t"
294 "cmpb $0,(%1)\n\t"
295 "je 7f\n\t"
296 "movl %1,%0\n"
297 "3:\tlodsb\n\t"
298 "testb %%al,%%al\n\t"
299 "je 5f\n\t"
300 "movl %4,%%edi\n\t"
301 "movl %%edx,%%ecx\n\t"
302 "repne\n\t"
303 "scasb\n\t"
304 "jne 3b\n\t"
305 "decl %1\n\t"
306 "cmpb $0,(%1)\n\t"
307 "je 5f\n\t"
308 "movb $0,(%1)\n\t"
309 "incl %1\n\t"
310 "jmp 6f\n"
311 "5:\txorl %1,%1\n"
312 "6:\tcmpb $0,(%0)\n\t"
313 "jne 7f\n\t"
314 "xorl %0,%0\n"
315 "7:\ttestl %0,%0\n\t"
316 "jne 8f\n\t"
317 "movl %0,%1\n"
318 "8:"
319 :"=b" (__res),"=S" (___strtok)
320 :"0" (___strtok),"1" (s),"g" (ct)
321 );
322 return __res;
323}
324
325inline void * memcpy(void * dest,const void * src, int n)
326{
327 __asm__ ("cld\n\t"
328 "rep\n\t"
329 "movsb"
330 ::"c" (n),"S" (src),"D" (dest)
331 );
332 return dest;
333}
334
335inline void * memmove(void * dest,const void * src, int n)
336{
337 if (dest<src)
338 __asm__("cld\n\t"
339 "rep\n\t"
340 "movsb"
341 ::"c" (n),"S" (src),"D" (dest)
342 );
343 else
344 __asm__("std\n\t"
345 "rep\n\t"
346 "movsb"
347 ::"c" (n),"S" (src+n-1),"D" (dest+n-1)
348 );
349 return dest;
350}
351
352static inline int memcmp(const void * cs,const void * ct,int count)
353{
354 register int __res ;
355 __asm__("cld\n\t"
356 "repe\n\t"
357 "cmpsb\n\t"
358 "je 1f\n\t"
359 "movl $1,%%eax\n\t"
360 "jl 1f\n\t"
361 "negl %%eax\n"
362 "1:"
363 :"=a" (__res):"0" (0),"D" (cs),"S" (ct),"c" (count)
364 );
365 return __res;
366}
367
368inline void * memchr(const void * cs,char c,int count)
369{
370 register void * __res ;
371 if (!count)
372 return NULL;
373 __asm__("cld\n\t"
374 "repne\n\t"
375 "scasb\n\t"
376 "je 1f\n\t"
377 "movl $1,%0\n"
378 "1:\tdecl %0"
379 :"=D" (__res):"a" (c),"D" (cs),"c" (count)
380 );
381 return __res;
382}
383
384static inline void * memset(void * s,char c,int count)
385{
386 __asm__("cld\n\t"
387 "rep\n\t"
388 "stosb"
389 ::"a" (c),"D" (s),"c" (count)
390 );
391 return s;
392} \ No newline at end of file