Skip to content

Commit 74fd4d7

Browse files
Add js_string_eq (#765)
Avoids checking equality when string lengths don't match. Co-authored-by: Richard Davison <[email protected]>
1 parent 8d88f32 commit 74fd4d7

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

quickjs.c

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,8 +1273,8 @@ static JSValue js_promise_resolve(JSContext *ctx, JSValue this_val,
12731273
int argc, JSValue *argv, int magic);
12741274
static JSValue js_promise_then(JSContext *ctx, JSValueConst this_val,
12751275
int argc, JSValueConst *argv);
1276-
static int js_string_compare(JSContext *ctx,
1277-
const JSString *p1, const JSString *p2);
1276+
static BOOL js_string_eq(const JSString *p1, const JSString *p2);
1277+
static int js_string_compare(const JSString *p1, const JSString *p2);
12781278
static JSValue JS_ToNumber(JSContext *ctx, JSValue val);
12791279
static int JS_SetPropertyValue(JSContext *ctx, JSValue this_obj,
12801280
JSValue prop, JSValue val, int flags);
@@ -3367,9 +3367,9 @@ static JSValue JS_AtomIsNumericIndex1(JSContext *ctx, JSAtom atom)
33673367
JS_FreeValue(ctx, num);
33683368
return str;
33693369
}
3370-
ret = js_string_compare(ctx, p, JS_VALUE_GET_STRING(str));
3370+
ret = js_string_eq(p, JS_VALUE_GET_STRING(str));
33713371
JS_FreeValue(ctx, str);
3372-
if (ret == 0) {
3372+
if (ret) {
33733373
return num;
33743374
} else {
33753375
JS_FreeValue(ctx, num);
@@ -4276,9 +4276,14 @@ static int js_string_memcmp(const JSString *p1, const JSString *p2, int len)
42764276
return res;
42774277
}
42784278

4279+
static BOOL js_string_eq(const JSString *p1, const JSString *p2) {
4280+
if (p1->len != p2->len)
4281+
return FALSE;
4282+
return js_string_memcmp(p1, p2, p1->len) == 0;
4283+
}
4284+
42794285
/* return < 0, 0 or > 0 */
4280-
static int js_string_compare(JSContext *ctx,
4281-
const JSString *p1, const JSString *p2)
4286+
static int js_string_compare(const JSString *p1, const JSString *p2)
42824287
{
42834288
int res, len;
42844289
len = min_int(p1->len, p2->len);
@@ -12939,7 +12944,7 @@ static no_inline int js_relational_slow(JSContext *ctx, JSValue *sp,
1293912944
JSString *p1, *p2;
1294012945
p1 = JS_VALUE_GET_STRING(op1);
1294112946
p2 = JS_VALUE_GET_STRING(op2);
12942-
res = js_string_compare(ctx, p1, p2);
12947+
res = js_string_compare(p1, p2);
1294312948
switch(op) {
1294412949
case OP_lt:
1294512950
res = (res < 0);
@@ -13230,7 +13235,7 @@ static BOOL js_strict_eq2(JSContext *ctx, JSValue op1, JSValue op2,
1323013235
} else {
1323113236
p1 = JS_VALUE_GET_STRING(op1);
1323213237
p2 = JS_VALUE_GET_STRING(op2);
13233-
res = (js_string_compare(ctx, p1, p2) == 0);
13238+
res = js_string_eq(p1, p2);
1323413239
}
1323513240
}
1323613241
break;
@@ -26874,7 +26879,7 @@ static int exported_names_cmp(const void *p1, const void *p2, void *opaque)
2687426879
/* XXX: raise an error ? */
2687526880
ret = 0;
2687626881
} else {
26877-
ret = js_string_compare(ctx, JS_VALUE_GET_STRING(str1),
26882+
ret = js_string_compare(JS_VALUE_GET_STRING(str1),
2687826883
JS_VALUE_GET_STRING(str2));
2687926884
}
2688026885
JS_FreeValue(ctx, str1);
@@ -39796,7 +39801,7 @@ static int js_array_cmp_generic(const void *a, const void *b, void *opaque) {
3979639801
goto exception;
3979739802
bp->str = JS_VALUE_GET_STRING(str);
3979839803
}
39799-
cmp = js_string_compare(ctx, ap->str, bp->str);
39804+
cmp = js_string_compare(ap->str, bp->str);
3980039805
}
3980139806
if (cmp != 0)
3980239807
return cmp;

0 commit comments

Comments
 (0)