Rev 2735 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2735 | Rev 3245 | ||
---|---|---|---|
1 | #include "string.h" |
1 | #include "string.h" |
2 | 2 | ||
3 | void* memset(void *mem, int c, unsigned size) |
3 | void* memset(void *mem, int c, unsigned size) |
4 | { |
4 | { |
5 | unsigned i; |
5 | unsigned i; |
6 | 6 | ||
7 | for ( i = 0; i < size; i++ ) |
7 | for ( i = 0; i < size; i++ ) |
8 | *((char *)mem+i) = (char) c; |
8 | *((char *)mem+i) = (char) c; |
9 | 9 | ||
10 | return NULL; |
10 | return NULL; |
11 | } |
11 | } |
12 | 12 | ||
13 | 13 | ||
14 | void* memcpy(void *dst, const void *src, unsigned size) |
14 | void* memcpy(void *dst, const void *src, unsigned size) |
15 | { |
15 | { |
16 | 16 | ||
17 | unsigned i; |
17 | unsigned i; |
18 | 18 | ||
19 | for ( i = 0; i < size; i++) |
19 | for ( i = 0; i < size; i++) |
20 | *(char *)(dst+i) = *(char *)(src+i); |
20 | *(char *)(dst+i) = *(char *)(src+i); |
21 | 21 | ||
22 | return NULL; |
22 | return NULL; |
23 | } |
23 | } |
24 | 24 | ||
25 | 25 | ||
26 | int memcmp(const void* buf1, const void* buf2, int count) |
26 | int memcmp(const void* buf1, const void* buf2, int count) |
27 | { |
27 | { |
28 | int i; |
28 | int i; |
29 | for (i=0;i |
29 | for (i=0;i |
30 | { |
30 | { |
31 | if (*(unsigned char*)buf1<*(unsigned char*)buf2) |
31 | if (*(unsigned char*)buf1<*(unsigned char*)buf2) |
32 | return -1; |
32 | return -1; |
33 | if (*(unsigned char*)buf1>*(unsigned char*)buf2) |
33 | if (*(unsigned char*)buf1>*(unsigned char*)buf2) |
34 | return 1; |
34 | return 1; |
35 | } |
35 | } |
36 | return 0; |
36 | return 0; |
37 | } |
37 | } |
38 | 38 | ||
39 | void strcat(char strDest[], char strSource[]) |
39 | void strcat(char strDest[], char strSource[]) |
40 | { |
40 | { |
41 | 41 | ||
42 | int i, j; |
42 | int i, j; |
43 | 43 | ||
44 | i = j = 0; |
44 | i = j = 0; |
45 | while (strDest[i] != '\0') |
45 | while (strDest[i] != '\0') |
46 | i++; |
46 | i++; |
47 | 47 | ||
48 | while ((strDest[i++] = strSource[j++]) != '\0') |
48 | while ((strDest[i++] = strSource[j++]) != '\0') |
49 | ; |
49 | ; |
50 | } |
50 | } |
51 | 51 | ||
52 | 52 | ||
53 | int strcmp(const char* string1, const char* string2) |
53 | int strcmp(const char* string1, const char* string2) |
54 | { |
54 | { |
55 | 55 | ||
56 | while (1) |
56 | while (1) |
57 | { |
57 | { |
58 | if (*string1<*string2) |
58 | if (*string1<*string2) |
59 | return -1; |
59 | return -1; |
60 | if (*string1>*string2) |
60 | if (*string1>*string2) |
61 | return 1; |
61 | return 1; |
62 | 62 | ||
63 | if (*string1=='\0') |
63 | if (*string1=='\0') |
64 | return 0; |
64 | return 0; |
65 | 65 | ||
66 | string1++; |
66 | string1++; |
67 | string2++; |
67 | string2++; |
68 | } |
68 | } |
69 | 69 | ||
70 | } |
70 | } |
71 | 71 | ||
72 | 72 | ||
73 | void strcpy(char strDest[], const char strSource[]) |
73 | void strcpy(char strDest[], const char strSource[]) |
74 | { |
74 | { |
75 | unsigned i; |
75 | unsigned i; |
76 | 76 | ||
77 | i = 0; |
77 | i = 0; |
78 | while ((strDest[i] = strSource[i]) != '\0') |
78 | while ((strDest[i] = strSource[i]) != '\0') |
79 | i++; |
79 | i++; |
80 | 80 | ||
81 | } |
81 | } |
82 | 82 | ||
83 | 83 | ||
84 | char* strncpy(char *strDest, const char *strSource, unsigned n) |
84 | char* strncpy(char *strDest, const char *strSource, unsigned n) |
85 | { |
85 | { |
86 | unsigned i; |
86 | unsigned i; |
87 | 87 | ||
88 | if (! n ) |
88 | if (! n ) |
89 | return strDest; |
89 | return strDest; |
90 | 90 | ||
91 | i = 0; |
91 | i = 0; |
92 | while ((strDest[i] = strSource[i]) != '\0') |
92 | while ((strDest[i] = strSource[i]) != '\0') |
93 | if ( (n-1) == i ) |
93 | if ( (n-1) == i ) |
94 | break; |
94 | break; |
95 | else |
95 | else |
96 | i++; |
96 | i++; |
97 | 97 | ||
98 | return strDest; |
98 | return strDest; |
99 | } |
99 | } |
100 | 100 | ||
101 | 101 | ||
102 | int strlen(const char* string) |
102 | int strlen(const char* string) |
103 | { |
103 | { |
104 | int i; |
104 | int i; |
105 | 105 | ||
106 | i=0; |
106 | i=0; |
107 | while (*string++) i++; |
107 | while (*string++) i++; |
108 | return i; |
108 | return i; |
109 | } |
109 | } |
110 | 110 | ||
111 | 111 | ||
112 | 112 | ||
113 | char* strchr(const char* string, int c) |
113 | char* strchr(const char* string, int c) |
114 | { |
114 | { |
115 | while (*string) |
115 | while (*string) |
116 | { |
116 | { |
117 | if (*string==c) |
117 | if (*string==c) |
118 | return (char*)string; |
118 | return (char*)string; |
119 | string++; |
119 | string++; |
120 | } |
120 | } |
121 | return (char*)0; |
121 | return (char*)0; |
122 | } |
122 | } |
123 | 123 | ||
124 | 124 | ||
125 | 125 | ||
126 | void _itoa(int i, char *s) |
126 | void _itoa(int i, char *s) |
127 | { |
127 | { |
128 | int a, b, c, d; |
128 | int a, b, c, d; |
129 | a = (i - i%1000)/1000; |
129 | a = (i - i%1000)/1000; |
130 | b = (i - i%100)/100 - a*10; |
130 | b = (i - i%100)/100 - a*10; |
131 | c = (i - i%10)/10 - a*100 - b*10; |
131 | c = (i - i%10)/10 - a*100 - b*10; |
132 | d = i%10; |
132 | d = i%10; |
133 | s[0] = a + '0'; |
133 | s[0] = a + '0'; |
134 | s[1] = b + '0'; |
134 | s[1] = b + '0'; |
135 | s[2] = c + '0'; |
135 | s[2] = c + '0'; |
136 | s[3] = d + '0'; |
136 | s[3] = d + '0'; |
137 | s[4] = 0; |
137 | s[4] = 0; |
138 | } |
138 | } |
139 | 139 | ||
140 | 140 | ||
141 | /* reverse: ïåðåâîðà÷èâàåì ñòðîêó s íà ìåñòå */ |
141 | /* reverse: ïåðåâîðà÷èâàåì ñòðîêó s íà ìåñòå */ |
142 | void reverse(char s[]) |
142 | void reverse(char s[]) |
143 | { |
143 | { |
144 | int i, j; |
144 | int i, j; |
145 | char c; |
145 | char c; |
146 | 146 | ||
147 | for (i = 0, j = strlen(s)-1; i |
147 | for (i = 0, j = strlen(s)-1; i |
148 | c = s[i]; |
148 | c = s[i]; |
149 | s[i] = s[j]; |
149 | s[i] = s[j]; |
150 | s[j] = c; |
150 | s[j] = c; |
151 | } |
151 | } |
152 | } |
152 | } |
153 | 153 | ||
154 | 154 | ||
155 | /* itoa: êîíâåðòèðóåì n â ñèìâîëû â s */ |
155 | /* itoa: êîíâåðòèðóåì n â ñèìâîëû â s */ |
156 | void itoa(int n, char s[]) |
156 | void itoa(int n, char s[]) |
157 | { |
157 | { |
158 | int i, sign; |
158 | int i, sign; |
159 | 159 | ||
160 | if ((sign = n) < 0) /* çàïèñûâàåì çíàê */ |
160 | if ((sign = n) < 0) |
161 | n = -n; /* äåëàåì n ïîëîæèòåëüíûì ÷èñëîì */ |
161 | n = -n; |
162 | i = 0; |
162 | i = 0; |
163 | do { /* ãåíåðèðóåì öèôðû â îáðàòíîì ïîðÿäêå */ |
163 | do { |
164 | s[i++] = n % 10 + '0'; /* áåðåì ñëåäóþùóþ öèôðó */ |
164 | s[i++] = n % 10 + '0'; |
165 | } while ((n /= 10) > 0); /* óäàëÿåì */ |
165 | } while ((n /= 10) > 0); |
166 | if (sign < 0) |
166 | if (sign < 0) |
167 | s[i++] = '-'; |
167 | s[i++] = '-'; |
168 | s[i] = '\0'; |
168 | s[i] = '\0'; |
169 | reverse(s); |
169 | reverse(s); |
170 | }>>*string2) |
170 | } |
171 | >*(unsigned> |
171 | |
- | 172 | ||
- | 173 | ||
- | 174 | int _atoi ( char *s ) |
|
- | 175 | { |
|
- | 176 | int i, n; |
|
- | 177 | ||
- | 178 | n = 0; |
|
- | 179 | for ( i = 0; s[i]!= '\0'; ++i) |
|
- | 180 | if ((s[i]<'0') || (s[i]>'9')) |
|
- | 181 | return 0; |
|
- | 182 | else |
|
- | 183 | n = 10 * n + s[i] - '0'; |
|
- | 184 | ||
- | 185 | return n; |
|
- | 186 | }'0')>>>*string2) |
|
172 | >>> |
187 | >*(unsigned> |
- | 188 | >>> |