Skip to content

Commit d308a13

Browse files
authored
Use string_get for clarity (#352)
1 parent 3f06c95 commit d308a13

File tree

1 file changed

+14
-41
lines changed

1 file changed

+14
-41
lines changed

quickjs.c

+14-41
Original file line numberDiff line numberDiff line change
@@ -1513,6 +1513,10 @@ static inline int is_digit(int c) {
15131513
return c >= '0' && c <= '9';
15141514
}
15151515

1516+
static inline int string_get(const JSString *p, int idx) {
1517+
return p->is_wide_char ? p->u.str16[idx] : p->u.str8[idx];
1518+
}
1519+
15161520
static inline BOOL is_be(void)
15171521
{
15181522
union {
@@ -2404,10 +2408,7 @@ static inline BOOL is_num_string(uint32_t *pval, const JSString *p)
24042408
len = p->len;
24052409
if (len == 0 || len > 10)
24062410
return FALSE;
2407-
if (p->is_wide_char)
2408-
c = p->u.str16[0];
2409-
else
2410-
c = p->u.str8[0];
2411+
c = string_get(p, 0);
24112412
if (is_num(c)) {
24122413
if (c == '0') {
24132414
if (len != 1)
@@ -2416,10 +2417,7 @@ static inline BOOL is_num_string(uint32_t *pval, const JSString *p)
24162417
} else {
24172418
n = c - '0';
24182419
for(i = 1; i < len; i++) {
2419-
if (p->is_wide_char)
2420-
c = p->u.str16[i];
2421-
else
2422-
c = p->u.str8[i];
2420+
c = string_get(p, i);
24232421
if (!is_num(c))
24242422
return FALSE;
24252423
n64 = (uint64_t)n * 10 + (c - '0');
@@ -2477,10 +2475,7 @@ static __maybe_unused void JS_DumpString(JSRuntime *rt,
24772475
sep = (p->header.ref_count == 1) ? '\"' : '\'';
24782476
putchar(sep);
24792477
for(i = 0; i < p->len; i++) {
2480-
if (p->is_wide_char)
2481-
c = p->u.str16[i];
2482-
else
2483-
c = p->u.str8[i];
2478+
c = string_get(p, i);
24842479
if (c == sep || c == '\\') {
24852480
putchar('\\');
24862481
putchar(c);
@@ -3042,10 +3037,7 @@ static const char *JS_AtomGetStrRT(JSRuntime *rt, char *buf, int buf_size,
30423037
return (const char *)str->u.str8;
30433038
}
30443039
for(i = 0; i < str->len; i++) {
3045-
if (str->is_wide_char)
3046-
c = str->u.str16[i];
3047-
else
3048-
c = str->u.str8[i];
3040+
c = string_get(str, i);
30493041
if ((q - buf) >= buf_size - UTF8_CHAR_LEN_MAX)
30503042
break;
30513043
if (c < 128) {
@@ -3671,10 +3663,6 @@ static int string_buffer_putc(StringBuffer *s, uint32_t c)
36713663
return string_buffer_putc16(s, c);
36723664
}
36733665

3674-
static int string_get(const JSString *p, int idx) {
3675-
return p->is_wide_char ? p->u.str16[idx] : p->u.str8[idx];
3676-
}
3677-
36783666
static int string_getc(const JSString *p, int *pidx)
36793667
{
36803668
int idx, c, c1;
@@ -7142,10 +7130,7 @@ static JSValue JS_GetPropertyInternal2(JSContext *ctx, JSValue obj,
71427130
uint32_t idx, ch;
71437131
idx = __JS_AtomToUInt32(prop);
71447132
if (idx < p1->len) {
7145-
if (p1->is_wide_char)
7146-
ch = p1->u.str16[idx];
7147-
else
7148-
ch = p1->u.str8[idx];
7133+
ch = string_get(p1, idx);
71497134
return js_new_string_char(ctx, ch);
71507135
}
71517136
} else if (prop == JS_ATOM_length) {
@@ -39106,10 +39091,7 @@ static int js_string_get_own_property(JSContext *ctx,
3910639091
idx = __JS_AtomToUInt32(prop);
3910739092
if (idx < p1->len) {
3910839093
if (desc) {
39109-
if (p1->is_wide_char)
39110-
ch = p1->u.str16[idx];
39111-
else
39112-
ch = p1->u.str8[idx];
39094+
ch = string_get(p1, idx);
3911339095
desc->flags = JS_PROP_ENUMERABLE;
3911439096
desc->value = js_new_string_char(ctx, ch);
3911539097
desc->getter = JS_UNDEFINED;
@@ -39264,7 +39246,7 @@ static JSValue js_string_fromCodePoint(JSContext *ctx, JSValue this_val,
3926439246
} else {
3926539247
if (JS_ToFloat64(ctx, &d, argv[i]))
3926639248
goto fail;
39267-
if (d < 0 || d > 0x10ffff || (c = (int)d) != d)
39249+
if (!(d >= 0 && d <= 0x10ffff) || (c = (int)d) != d)
3926839250
goto range_error;
3926939251
}
3927039252
if (string_buffer_putc(b, c))
@@ -39366,10 +39348,7 @@ static JSValue js_string_at(JSContext *ctx, JSValue this_val,
3936639348
if (idx < 0 || idx >= p->len) {
3936739349
ret = JS_UNDEFINED;
3936839350
} else {
39369-
if (p->is_wide_char)
39370-
c = p->u.str16[idx];
39371-
else
39372-
c = p->u.str8[idx];
39351+
c = string_get(p, idx);
3937339352
ret = js_new_string_char(ctx, c);
3937439353
}
3937539354
JS_FreeValue(ctx, val);
@@ -39394,10 +39373,7 @@ static JSValue js_string_charCodeAt(JSContext *ctx, JSValue this_val,
3939439373
if (idx < 0 || idx >= p->len) {
3939539374
ret = JS_NAN;
3939639375
} else {
39397-
if (p->is_wide_char)
39398-
c = p->u.str16[idx];
39399-
else
39400-
c = p->u.str8[idx];
39376+
c = string_get(p, idx);
3940139377
ret = js_int32(c);
3940239378
}
3940339379
JS_FreeValue(ctx, val);
@@ -39422,10 +39398,7 @@ static JSValue js_string_charAt(JSContext *ctx, JSValue this_val,
3942239398
if (idx < 0 || idx >= p->len) {
3942339399
ret = js_new_string8(ctx, NULL, 0);
3942439400
} else {
39425-
if (p->is_wide_char)
39426-
c = p->u.str16[idx];
39427-
else
39428-
c = p->u.str8[idx];
39401+
c = string_get(p, idx);
3942939402
ret = js_new_string_char(ctx, c);
3943039403
}
3943139404
JS_FreeValue(ctx, val);

0 commit comments

Comments
 (0)