Skip to content

Commit

Permalink
Add js_string_eq (#765)
Browse files Browse the repository at this point in the history
Avoids checking equality when string lengths don't match.

Co-authored-by: Richard Davison <[email protected]>
  • Loading branch information
richarddd and richarddavison authored Dec 27, 2024
1 parent 8d88f32 commit 74fd4d7
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions quickjs.c
Original file line number Diff line number Diff line change
Expand Up @@ -1273,8 +1273,8 @@ static JSValue js_promise_resolve(JSContext *ctx, JSValue this_val,
int argc, JSValue *argv, int magic);
static JSValue js_promise_then(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv);
static int js_string_compare(JSContext *ctx,
const JSString *p1, const JSString *p2);
static BOOL js_string_eq(const JSString *p1, const JSString *p2);
static int js_string_compare(const JSString *p1, const JSString *p2);
static JSValue JS_ToNumber(JSContext *ctx, JSValue val);
static int JS_SetPropertyValue(JSContext *ctx, JSValue this_obj,
JSValue prop, JSValue val, int flags);
Expand Down Expand Up @@ -3367,9 +3367,9 @@ static JSValue JS_AtomIsNumericIndex1(JSContext *ctx, JSAtom atom)
JS_FreeValue(ctx, num);
return str;
}
ret = js_string_compare(ctx, p, JS_VALUE_GET_STRING(str));
ret = js_string_eq(p, JS_VALUE_GET_STRING(str));
JS_FreeValue(ctx, str);
if (ret == 0) {
if (ret) {
return num;
} else {
JS_FreeValue(ctx, num);
Expand Down Expand Up @@ -4276,9 +4276,14 @@ static int js_string_memcmp(const JSString *p1, const JSString *p2, int len)
return res;
}

static BOOL js_string_eq(const JSString *p1, const JSString *p2) {
if (p1->len != p2->len)
return FALSE;
return js_string_memcmp(p1, p2, p1->len) == 0;
}

/* return < 0, 0 or > 0 */
static int js_string_compare(JSContext *ctx,
const JSString *p1, const JSString *p2)
static int js_string_compare(const JSString *p1, const JSString *p2)
{
int res, len;
len = min_int(p1->len, p2->len);
Expand Down Expand Up @@ -12939,7 +12944,7 @@ static no_inline int js_relational_slow(JSContext *ctx, JSValue *sp,
JSString *p1, *p2;
p1 = JS_VALUE_GET_STRING(op1);
p2 = JS_VALUE_GET_STRING(op2);
res = js_string_compare(ctx, p1, p2);
res = js_string_compare(p1, p2);
switch(op) {
case OP_lt:
res = (res < 0);
Expand Down Expand Up @@ -13230,7 +13235,7 @@ static BOOL js_strict_eq2(JSContext *ctx, JSValue op1, JSValue op2,
} else {
p1 = JS_VALUE_GET_STRING(op1);
p2 = JS_VALUE_GET_STRING(op2);
res = (js_string_compare(ctx, p1, p2) == 0);
res = js_string_eq(p1, p2);
}
}
break;
Expand Down Expand Up @@ -26874,7 +26879,7 @@ static int exported_names_cmp(const void *p1, const void *p2, void *opaque)
/* XXX: raise an error ? */
ret = 0;
} else {
ret = js_string_compare(ctx, JS_VALUE_GET_STRING(str1),
ret = js_string_compare(JS_VALUE_GET_STRING(str1),
JS_VALUE_GET_STRING(str2));
}
JS_FreeValue(ctx, str1);
Expand Down Expand Up @@ -39796,7 +39801,7 @@ static int js_array_cmp_generic(const void *a, const void *b, void *opaque) {
goto exception;
bp->str = JS_VALUE_GET_STRING(str);
}
cmp = js_string_compare(ctx, ap->str, bp->str);
cmp = js_string_compare(ap->str, bp->str);
}
if (cmp != 0)
return cmp;
Expand Down

0 comments on commit 74fd4d7

Please sign in to comment.