rm index and indexof

This commit is contained in:
2026-01-17 16:21:02 -06:00
parent 97ece8e5cb
commit a7a323a74e
19 changed files with 86 additions and 303 deletions

View File

@@ -30302,103 +30302,9 @@ static int js_string_find_invalid_codepoint(JSString *p)
return -1;
}
static JSValue js_string_indexOf(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv, int lastIndexOf)
{
JSValue str, v;
int i, len, v_len, pos, start, stop, ret, inc;
JSString *p;
JSString *p1;
str = JS_ToStringCheckObject(ctx, this_val);
if (JS_IsException(str))
return str;
v = JS_ToString(ctx, argv[0]);
if (JS_IsException(v))
goto fail;
p = JS_VALUE_GET_STRING(str);
p1 = JS_VALUE_GET_STRING(v);
len = p->len;
v_len = p1->len;
if (lastIndexOf) {
pos = len - v_len;
if (argc > 1) {
double d;
if (JS_ToFloat64(ctx, &d, argv[1]))
goto fail;
if (!isnan(d)) {
if (d <= 0)
pos = 0;
else if (d < pos)
pos = d;
}
}
start = pos;
stop = 0;
inc = -1;
} else {
pos = 0;
if (argc > 1) {
if (JS_ToInt32Clamp(ctx, &pos, argv[1], 0, len, 0))
goto fail;
}
start = pos;
stop = len - v_len;
inc = 1;
}
ret = -1;
if (len >= v_len && inc * (stop - start) >= 0) {
for (i = start;; i += inc) {
if (!string_cmp(p, p1, i, 0, v_len)) {
ret = i;
break;
}
if (i == stop)
break;
}
}
JS_FreeValue(ctx, str);
JS_FreeValue(ctx, v);
return JS_NewInt32(ctx, ret);
fail:
JS_FreeValue(ctx, str);
JS_FreeValue(ctx, v);
return JS_EXCEPTION;
}
/* return < 0 if exception or TRUE/FALSE */
static int js_is_regexp(JSContext *ctx, JSValueConst obj);
static int check_regexp_g_flag(JSContext *ctx, JSValueConst regexp)
{
int ret;
JSValue flags;
ret = js_is_regexp(ctx, regexp);
if (ret < 0)
return -1;
if (ret) {
flags = JS_GetProperty(ctx, regexp, JS_ATOM_flags);
if (JS_IsException(flags))
return -1;
if (JS_IsNull(flags) || JS_IsNull(flags)) {
JS_ThrowTypeError(ctx, "cannot convert to object");
return -1;
}
flags = JS_ToStringFree(ctx, flags);
if (JS_IsException(flags))
return -1;
ret = string_indexof_char(JS_VALUE_GET_STRING(flags), 'g', 0);
JS_FreeValue(ctx, flags);
if (ret < 0) {
JS_ThrowTypeError(ctx, "regexp must have the 'g' flag");
return -1;
}
}
return 0;
}
static JSValue js_string___GetSubstitution(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
@@ -30507,119 +30413,6 @@ exception:
return JS_EXCEPTION;
}
static JSValue js_string_replace(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv,
int is_replaceAll)
{
// replace(rx, rep)
JSValueConst O = this_val, searchValue = argv[0], replaceValue = argv[1];
JSValueConst args[6];
JSValue str, search_str, replaceValue_str, repl_str;
JSString *sp, *searchp;
StringBuffer b_s, *b = &b_s;
int pos, functionalReplace, endOfLastMatch;
BOOL is_first;
if (JS_IsNull(O) || JS_IsNull(O))
return JS_ThrowTypeError(ctx, "cannot convert to object");
search_str = JS_NULL;
replaceValue_str = JS_NULL;
repl_str = JS_NULL;
if (!JS_IsNull(searchValue) && !JS_IsNull(searchValue)) {
JSValue replacer;
if (is_replaceAll) {
if (check_regexp_g_flag(ctx, searchValue) < 0)
return JS_EXCEPTION;
}
replacer = JS_GetProperty(ctx, searchValue, JS_ATOM_Symbol_replace);
if (JS_IsException(replacer))
return JS_EXCEPTION;
if (!JS_IsNull(replacer) && !JS_IsNull(replacer)) {
args[0] = O;
args[1] = replaceValue;
return JS_CallFree(ctx, replacer, searchValue, 2, args);
}
}
string_buffer_init(ctx, b, 0);
str = JS_ToString(ctx, O);
if (JS_IsException(str))
goto exception;
search_str = JS_ToString(ctx, searchValue);
if (JS_IsException(search_str))
goto exception;
functionalReplace = JS_IsFunction(ctx, replaceValue);
if (!functionalReplace) {
replaceValue_str = JS_ToString(ctx, replaceValue);
if (JS_IsException(replaceValue_str))
goto exception;
}
sp = JS_VALUE_GET_STRING(str);
searchp = JS_VALUE_GET_STRING(search_str);
endOfLastMatch = 0;
is_first = TRUE;
for(;;) {
if (unlikely(searchp->len == 0)) {
if (is_first)
pos = 0;
else if (endOfLastMatch >= sp->len)
pos = -1;
else
pos = endOfLastMatch + 1;
} else {
pos = string_indexof(sp, searchp, endOfLastMatch);
}
if (pos < 0) {
if (is_first) {
string_buffer_free(b);
JS_FreeValue(ctx, search_str);
JS_FreeValue(ctx, replaceValue_str);
return str;
} else {
break;
}
}
if (functionalReplace) {
args[0] = search_str;
args[1] = JS_NewInt32(ctx, pos);
args[2] = str;
repl_str = JS_ToStringFree(ctx, JS_Call(ctx, replaceValue, JS_NULL, 3, args));
} else {
args[0] = search_str;
args[1] = str;
args[2] = JS_NewInt32(ctx, pos);
args[3] = JS_NULL;
args[4] = JS_NULL;
args[5] = replaceValue_str;
repl_str = js_string___GetSubstitution(ctx, JS_NULL, 6, args);
}
if (JS_IsException(repl_str))
goto exception;
string_buffer_concat(b, sp, endOfLastMatch, pos);
string_buffer_concat_value_free(b, repl_str);
endOfLastMatch = pos + searchp->len;
is_first = FALSE;
if (!is_replaceAll)
break;
}
string_buffer_concat(b, sp, endOfLastMatch, sp->len);
JS_FreeValue(ctx, search_str);
JS_FreeValue(ctx, replaceValue_str);
JS_FreeValue(ctx, str);
return string_buffer_end(b);
exception:
string_buffer_free(b);
JS_FreeValue(ctx, search_str);
JS_FreeValue(ctx, replaceValue_str);
JS_FreeValue(ctx, str);
return JS_EXCEPTION;
}
/* also used for String.prototype.valueOf */
static JSValue js_string_toString(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
@@ -30686,10 +30479,6 @@ static JSValue js_string_concat(JSContext *ctx, JSValueConst this_val,
static const JSCFunctionListEntry js_string_proto_funcs[] = {
JS_PROP_INT32_DEF("length", 0, JS_PROP_CONFIGURABLE ),
JS_CFUNC_DEF("concat", 1, js_string_concat),
JS_CFUNC_MAGIC_DEF("indexOf", 1, js_string_indexOf, 0 ),
JS_CFUNC_MAGIC_DEF("lastIndexOf", 1, js_string_indexOf, 1 ),
JS_CFUNC_MAGIC_DEF("replace", 2, js_string_replace, 0 ),
JS_CFUNC_MAGIC_DEF("replaceAll", 2, js_string_replace, 1 ),
JS_CFUNC_DEF("toString", 0, js_string_toString ),
JS_CFUNC_DEF("valueOf", 0, js_string_toString ),
JS_CFUNC_MAGIC_DEF("[Symbol.iterator]", 0, js_create_array_iterator, JS_ITERATOR_KIND_VALUE | 4 ),