Skip to content

Commit 4876f72

Browse files
author
Fabrice Bellard
committed
added String.prototype.isWellFormed and String.prototype.toWellFormed
1 parent b8791e9 commit 4876f72

File tree

3 files changed

+83
-3
lines changed

3 files changed

+83
-3
lines changed

TODO

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,5 @@ Optimization ideas:
6363
Test262o: 0/11262 errors, 463 excluded
6464
Test262o commit: 7da91bceb9ce7613f87db47ddd1292a2dda58b42 (es5-tests branch)
6565

66-
Result: 16/76387 errors, 1497 excluded, 8397 skipped
66+
Result: 16/76419 errors, 1497 excluded, 8381 skipped
6767
Test262 commit: 31126581e7290f9233c29cefd93f66c6ac78f1c9

quickjs.c

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40824,6 +40824,84 @@ static int64_t string_advance_index(JSString *p, int64_t index, BOOL unicode)
4082440824
return index;
4082540825
}
4082640826

40827+
/* return the position of the first invalid character in the string or
40828+
-1 if none */
40829+
static int js_string_find_invalid_codepoint(JSString *p)
40830+
{
40831+
int i, c;
40832+
if (!p->is_wide_char)
40833+
return -1;
40834+
for(i = 0; i < p->len; i++) {
40835+
c = p->u.str16[i];
40836+
if (c >= 0xD800 && c <= 0xDFFF) {
40837+
if (c >= 0xDC00 || (i + 1) >= p->len)
40838+
return i;
40839+
c = p->u.str16[i + 1];
40840+
if (c < 0xDC00 || c > 0xDFFF)
40841+
return i;
40842+
i++;
40843+
}
40844+
}
40845+
return -1;
40846+
}
40847+
40848+
static JSValue js_string_isWellFormed(JSContext *ctx, JSValueConst this_val,
40849+
int argc, JSValueConst *argv)
40850+
{
40851+
JSValue str;
40852+
JSString *p;
40853+
BOOL ret;
40854+
40855+
str = JS_ToStringCheckObject(ctx, this_val);
40856+
if (JS_IsException(str))
40857+
return JS_EXCEPTION;
40858+
p = JS_VALUE_GET_STRING(str);
40859+
ret = (js_string_find_invalid_codepoint(p) < 0);
40860+
JS_FreeValue(ctx, str);
40861+
return JS_NewBool(ctx, ret);
40862+
}
40863+
40864+
static JSValue js_string_toWellFormed(JSContext *ctx, JSValueConst this_val,
40865+
int argc, JSValueConst *argv)
40866+
{
40867+
JSValue str, ret;
40868+
JSString *p;
40869+
int c, i;
40870+
40871+
str = JS_ToStringCheckObject(ctx, this_val);
40872+
if (JS_IsException(str))
40873+
return JS_EXCEPTION;
40874+
40875+
p = JS_VALUE_GET_STRING(str);
40876+
/* avoid reallocating the string if it is well-formed */
40877+
i = js_string_find_invalid_codepoint(p);
40878+
if (i < 0)
40879+
return str;
40880+
40881+
ret = js_new_string16(ctx, p->u.str16, p->len);
40882+
JS_FreeValue(ctx, str);
40883+
if (JS_IsException(ret))
40884+
return JS_EXCEPTION;
40885+
40886+
p = JS_VALUE_GET_STRING(ret);
40887+
for (; i < p->len; i++) {
40888+
c = p->u.str16[i];
40889+
if (c >= 0xD800 && c <= 0xDFFF) {
40890+
if (c >= 0xDC00 || (i + 1) >= p->len) {
40891+
p->u.str16[i] = 0xFFFD;
40892+
} else {
40893+
c = p->u.str16[i + 1];
40894+
if (c < 0xDC00 || c > 0xDFFF) {
40895+
p->u.str16[i] = 0xFFFD;
40896+
} else {
40897+
i++;
40898+
}
40899+
}
40900+
}
40901+
}
40902+
return ret;
40903+
}
40904+
4082740905
static JSValue js_string_indexOf(JSContext *ctx, JSValueConst this_val,
4082840906
int argc, JSValueConst *argv, int lastIndexOf)
4082940907
{
@@ -42044,6 +42122,8 @@ static const JSCFunctionListEntry js_string_proto_funcs[] = {
4204442122
JS_CFUNC_MAGIC_DEF("charAt", 1, js_string_charAt, 0 ),
4204542123
JS_CFUNC_DEF("concat", 1, js_string_concat ),
4204642124
JS_CFUNC_DEF("codePointAt", 1, js_string_codePointAt ),
42125+
JS_CFUNC_DEF("isWellFormed", 0, js_string_isWellFormed ),
42126+
JS_CFUNC_DEF("toWellFormed", 0, js_string_toWellFormed ),
4204742127
JS_CFUNC_MAGIC_DEF("indexOf", 1, js_string_indexOf, 0 ),
4204842128
JS_CFUNC_MAGIC_DEF("lastIndexOf", 1, js_string_indexOf, 1 ),
4204942129
JS_CFUNC_MAGIC_DEF("includes", 1, js_string_includes, 0 ),

test262.conf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,10 @@ String.fromCodePoint
169169
String.prototype.at
170170
String.prototype.endsWith
171171
String.prototype.includes
172-
String.prototype.isWellFormed=skip
172+
String.prototype.isWellFormed
173173
String.prototype.matchAll
174174
String.prototype.replaceAll
175-
String.prototype.toWellFormed=skip
175+
String.prototype.toWellFormed
176176
String.prototype.trimEnd
177177
String.prototype.trimStart
178178
super

0 commit comments

Comments
 (0)