Implement String.prototype.isWellFormed (#41)

This commit is contained in:
Ben Noordhuis 2023-11-11 13:47:22 +01:00 committed by GitHub
parent 0dc99e4ac2
commit 0720b068ca
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 1 deletions

View file

@ -38206,6 +38206,46 @@ static int64_t string_advance_index(JSString *p, int64_t index, BOOL unicode)
return index; return index;
} }
static JSValue js_string_isWellFormed(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
JSValue str;
JSValue ret;
JSString *p;
uint32_t i, n, hi, lo;
ret = JS_TRUE;
str = JS_ToStringCheckObject(ctx, this_val);
if (JS_IsException(str))
return JS_EXCEPTION;
p = JS_VALUE_GET_STRING(str);
if (p->is_wide_char) {
for (i = 0, n = p->len; i < n; i++) {
hi = p->u.str16[i];
if (hi < 0xD800 || hi > 0xDFFF)
continue;
if (hi > 0xDBFF) {
ret = JS_FALSE;
break;
}
i++;
if (i == n) {
ret = JS_FALSE;
break;
}
lo = p->u.str16[i];
if (lo < 0xDC00 || lo > 0xDFFF) {
ret = JS_FALSE;
break;
}
}
}
JS_FreeValue(ctx, str);
return ret;
}
static JSValue js_string_indexOf(JSContext *ctx, JSValueConst this_val, static JSValue js_string_indexOf(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv, int lastIndexOf) int argc, JSValueConst *argv, int lastIndexOf)
{ {
@ -39357,6 +39397,7 @@ static const JSCFunctionListEntry js_string_proto_funcs[] = {
JS_CFUNC_DEF("charAt", 1, js_string_charAt ), JS_CFUNC_DEF("charAt", 1, js_string_charAt ),
JS_CFUNC_DEF("concat", 1, js_string_concat ), JS_CFUNC_DEF("concat", 1, js_string_concat ),
JS_CFUNC_DEF("codePointAt", 1, js_string_codePointAt ), JS_CFUNC_DEF("codePointAt", 1, js_string_codePointAt ),
JS_CFUNC_DEF("isWellFormed", 0, js_string_isWellFormed ),
JS_CFUNC_MAGIC_DEF("indexOf", 1, js_string_indexOf, 0 ), JS_CFUNC_MAGIC_DEF("indexOf", 1, js_string_indexOf, 0 ),
JS_CFUNC_MAGIC_DEF("lastIndexOf", 1, js_string_indexOf, 1 ), JS_CFUNC_MAGIC_DEF("lastIndexOf", 1, js_string_indexOf, 1 ),
JS_CFUNC_MAGIC_DEF("includes", 1, js_string_includes, 0 ), JS_CFUNC_MAGIC_DEF("includes", 1, js_string_includes, 0 ),

View file

@ -169,7 +169,7 @@ String.fromCodePoint
String.prototype.at String.prototype.at
String.prototype.endsWith String.prototype.endsWith
String.prototype.includes String.prototype.includes
String.prototype.isWellFormed=skip String.prototype.isWellFormed
String.prototype.matchAll String.prototype.matchAll
String.prototype.replaceAll String.prototype.replaceAll
String.prototype.toWellFormed=skip String.prototype.toWellFormed=skip