1 #include <string.h>
2
strlen(const char * s)3 size_t strlen(const char *s)
4 {
5 register int __res = 0;
6 while (s[__res] != '\0')
7 {
8 ++__res;
9 }
10 return __res;
11 }
12
strcmp(const char * FirstPart,const char * SecondPart)13 int strcmp(const char *FirstPart, const char *SecondPart)
14 {
15 register int __res;
16 __asm__ __volatile__("cld \n\t"
17 "1: \n\t"
18 "lodsb \n\t"
19 "scasb \n\t"
20 "jne 2f \n\t"
21 "testb %%al, %%al \n\t"
22 "jne 1b \n\t"
23 "xorl %%eax, %%eax \n\t"
24 "jmp 3f \n\t"
25 "2: \n\t"
26 "movl $1, %%eax \n\t"
27 "jl 3f \n\t"
28 "negl %%eax \n\t"
29 "3: \n\t"
30 : "=a"(__res)
31 : "D"(FirstPart), "S"(SecondPart)
32 :);
33 return __res;
34 }
35
memset(void * dst,unsigned char C,uint64_t size)36 void *memset(void *dst, unsigned char C, uint64_t size)
37 {
38
39 int d0, d1;
40 unsigned long tmp = C * 0x0101010101010101UL;
41 __asm__ __volatile__("cld \n\t"
42 "rep \n\t"
43 "stosq \n\t"
44 "testb $4, %b3 \n\t"
45 "je 1f \n\t"
46 "stosl \n\t"
47 "1:\ttestb $2, %b3 \n\t"
48 "je 2f\n\t"
49 "stosw \n\t"
50 "2:\ttestb $1, %b3 \n\t"
51 "je 3f \n\t"
52 "stosb \n\t"
53 "3: \n\t"
54 : "=&c"(d0), "=&D"(d1)
55 : "a"(tmp), "q"(size), "0"(size / 8), "1"(dst)
56 : "memory");
57 return dst;
58 }
59
60 /**
61 * @brief 拷贝指定字节数的字符串
62 *
63 * @param dst 目标地址
64 * @param src 源字符串
65 * @param Count 字节数
66 * @return char*
67 */
strncpy(char * dst,const char * src,size_t Count)68 char *strncpy(char *dst, const char *src, size_t Count)
69 {
70 __asm__ __volatile__("cld \n\t"
71 "1: \n\t"
72 "decq %2 \n\t"
73 "js 2f \n\t"
74 "lodsb \n\t"
75 "stosb \n\t"
76 "testb %%al, %%al \n\t"
77 "jne 1b \n\t"
78 "rep \n\t"
79 "stosb \n\t"
80 "2: \n\t"
81 :
82 : "S"(src), "D"(dst), "c"(Count)
83 : "ax", "memory");
84 return dst;
85 }
86
87 /**
88 * @brief 拼接两个字符串(将src接到dest末尾)
89 *
90 * @param dest 目标串
91 * @param src 源串
92 * @return char*
93 */
strcat(char * dest,const char * src)94 char *strcat(char *dest, const char *src)
95 {
96 strcpy(dest + strlen(dest), src);
97 return dest;
98 }
99
100 /**
101 * @brief 拷贝整个字符串
102 *
103 * @param dst 目标地址
104 * @param src 源地址
105 * @return char* 目标字符串
106 */
strcpy(char * dst,const char * src)107 char *strcpy(char *dst, const char *src)
108 {
109 while (*src)
110 {
111 *(dst++) = *(src++);
112 }
113 *dst = 0;
114
115 return dst;
116 }