Skip to content

Commit ba95cb0

Browse files
huxinxruben-ayrapetyan
authored andcommitted
Update strcmp implementation, to improve performance
JerryScript-DCO-1.0-Signed-off-by: Xin Hu [email protected]
1 parent f6bd5af commit ba95cb0

File tree

1 file changed

+10
-30
lines changed

1 file changed

+10
-30
lines changed

jerry-libc/jerry-libc.c

Lines changed: 10 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -156,43 +156,23 @@ CALL_PRAGMA (GCC diagnostic pop)
156156
/** Compare two strings. return an integer less than, equal to, or greater than zero
157157
if s1 is found, respectively, to be less than, to match, or be greater than s2. */
158158
int
159-
strcmp (const char *s1, const char *s2)
159+
strcmp (const char *str1, const char *str2)
160160
{
161-
size_t i;
162-
if (s1 == NULL)
163-
{
164-
if (s2 != NULL)
165-
{
166-
return -1;
167-
}
168-
else
169-
{
170-
return 0;
171-
}
172-
}
173-
if (s2 == NULL)
161+
int s1;
162+
int s2;
163+
do
174164
{
175-
return 1;
176-
}
165+
s1 = *str1++;
166+
s2 = *str2++;
177167

178-
for (i = 0; s1[i]; i++)
179-
{
180-
if (s1[i] > s2[i])
181-
{
182-
return 1;
183-
}
184-
else if (s1[i] < s2[i])
168+
if (s1 == 0)
185169
{
186-
return -1;
170+
break;
187171
}
188172
}
173+
while (s1 == s2);
189174

190-
if (s2[i])
191-
{
192-
return -1;
193-
}
194-
195-
return 0;
175+
return (s1 < s2) ? -1 : (s1 > s2 ? 1 : 0);
196176
}
197177

198178
/** Compare two strings. return an integer less than, equal to, or greater than zero

0 commit comments

Comments
 (0)