Skip to content

Commit 166fdc9

Browse files
committed
Fixing / simplifying strcmp.
1 parent 020e699 commit 166fdc9

File tree

1 file changed

+6
-23
lines changed

1 file changed

+6
-23
lines changed

libc/include/string.h

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -154,34 +154,17 @@ static __inline__ char * strncat(char * s1, const char * s2, size_t n) {
154154

155155
static __inline__ int strcmp(const char * s1, const char * s2) {
156156
char c1, c2;
157-
while ((c1 = *s1++) && (c2 = *s2++)) {
158-
if (!c1) {
159-
return -1;
160-
} else if (!c2) {
161-
return 1;
162-
} else if (c1 < c2) {
163-
return -1;
164-
} else if (c1 > c2) {
165-
return 1;
166-
}
157+
while ((c1 = *s1++)) {
158+
if ((c2 = *s2++) != c1) break;
167159
}
168-
169-
return 0;
160+
return c1 - c2;
170161
}
171162

172163
static __inline__ int strncmp(const char * s1, const char * s2, size_t n) {
173164
char c1, c2;
174-
while ((c1 = *s1++) && (c2 = *s2++) && n--) {
175-
if (!c1) {
176-
return -1;
177-
} else if (!c2) {
178-
return 1;
179-
} else if (c1 < c2) {
180-
return -1;
181-
} else if (c1 > c2) {
182-
return 1;
183-
}
184-
}
165+
while (n--)
166+
if ((c1 = *s1++) != (c2 = *s2++))
167+
return c1 - c2;
185168

186169
return 0;
187170
}

0 commit comments

Comments
 (0)