From 42e924da01897c79af4b32a451a31fe257bc8d37 Mon Sep 17 00:00:00 2001 From: Ekaterina Zaytseva Date: Tue, 9 Sep 2025 12:10:29 +0300 Subject: [PATCH] [ArkTS][Std] regexp: number->int Issue: #ICX0C5 Signed-off-by: Ekaterina Zaytseva --- .../plugins/ets/stdlib/escompat/RegExp.ets | 60 +++++++------------ .../ets/stdlib/native/escompat/RegExp.cpp | 2 +- .../escompat/RegExpExecArrayTest.ets | 8 --- .../escompat/RegExpMatchArrayTest.ets | 6 -- 4 files changed, 21 insertions(+), 55 deletions(-) diff --git a/static_core/plugins/ets/stdlib/escompat/RegExp.ets b/static_core/plugins/ets/stdlib/escompat/RegExp.ets index 3b20eb4ba5..8203ddcf44 100644 --- a/static_core/plugins/ets/stdlib/escompat/RegExp.ets +++ b/static_core/plugins/ets/stdlib/escompat/RegExp.ets @@ -26,14 +26,14 @@ export class RegExpResultArray extends Array { private resultRaw_: String = "" /** The 0-based index of the match in the string */ - protected index_: number = 0 + protected index_: int = 0 /** The original string that was matched against */ protected input_: String = "" - private endIndex: number = 0 + private endIndex: int = 0 // NOTE(shumilov-petr): Make nullable when #14813 will be fixed - protected indices_: Array> = [] + protected indices_: Array> = [] /** named groups names and indeces */ protected groupsRaw_: String = "" @@ -41,7 +41,7 @@ export class RegExpResultArray extends Array { /** named groups names and indeces */ public groups: RegExpGroupsContainer | undefined = undefined - constructor(index: int, input: String, result: Array, indices: Array>) { + constructor(index: int, input: String, result: Array, indices: Array>) { super(0) this.isCorrect = true this.index_ = index @@ -63,7 +63,7 @@ export class RegExpResultArray extends Array { return this } - private setNumberedField(index: number, value: String): void { + private setNumberedField(index: int, value: String): void { if (index > 9 || index <= 0) { return } @@ -79,7 +79,7 @@ export class RegExpResultArray extends Array { } // NOTE(ekaterinzaytseva): unable to replace internal - crash in analyzer - internal postExecProcessing(res: RegExpResultArray, input: String, index: number, hasIndices: boolean): void { + internal postExecProcessing(res: RegExpResultArray, input: String, index: int, hasIndices: boolean): void { if (!res.isCorrect) { return } @@ -125,16 +125,12 @@ export class RegExpResultArray extends Array { public $_get(index: int): String | undefined { return super.$_get(index) } - public $_get(index: number): String | undefined { - return this.$_get(index.toInt()) - } + public $_set(index: int, val: String | undefined): void { super.$_set(index, val) } - public $_set(index: number, val: String | undefined): void { - this.$_set(index.toInt(), val) - } - public get indices(): Array> { + + public get indices(): Array> { return this.indices_ } @@ -157,7 +153,7 @@ export class RegExpResultArray extends Array { throw new Error("not overridden") } - private processStaticFields(index: number, str: String): void { + private processStaticFields(index: int, str: String): void { if (index == 0) { RegExp.lastMatch_ = str } @@ -178,10 +174,10 @@ export class RegExpResultArray extends Array { } if (hasIndices) { - this.indices_ = new Array>(n) + this.indices_ = new Array>(n) for (let i = 0; i < data.length; i += 2) { - const index = Number.parseInt(data[i]) - const endIndex = Number.parseInt(data[i + 1]) + const index: int = Number.parseInt(data[i]) + const endIndex: int = Number.parseInt(data[i + 1]) const pos: int = (i / 2); if (index == -1 || endIndex == -1) { this.indices_[pos] = [0, 0] @@ -212,20 +208,12 @@ export class RegExpMatchArray extends RegExpResultArray { private hasIndex_: boolean = true private hasInput_: boolean = true - constructor(index: int, input: String, result: Array, indices: Array>) { - super(Double.toInt(index), input, result, indices) + constructor(index: int, input: String, result: Array, indices: Array>) { + super(index, input, result, indices) } constructor(index: int, input: String, result: Array) { - this(Double.toInt(index), input, result, []) - } - - constructor(index: number, input: String, result: Array, indices: Array>) { - this(Double.toInt(index), input, result, indices) - } - - constructor(index: number, input: String, result: Array) { - this(Double.toInt(index), input, result) + this(index, input, result, []) } protected constructor() { @@ -306,20 +294,12 @@ export class RegExpMatchArray extends RegExpResultArray { } export class RegExpExecArray extends RegExpResultArray { - constructor(index: int, input: String, result: Array, indices: Array>) { - super(Double.toInt(index), input, result, indices) + constructor(index: int, input: String, result: Array, indices: Array>) { + super(index, input, result, indices) } constructor(index: int, input: String, result: Array) { - this(Double.toInt(index), input, result, []) - } - - constructor(index: number, input: String, result: Array, indices: Array>) { - this(Double.toInt(index), input, result, indices) - } - - constructor(index: number, input: String, result: Array) { - this(Double.toInt(index), input, result) + this(index, input, result, []) } protected constructor() { @@ -912,7 +892,7 @@ export class RegExp extends Object { } this.lastIndex = 0; let matches = new containers.UndefinableStringArray(); - let indices = new Array>(); + let indices = new Array>(); let isUndefined = new Array(); let n = 0 while (true) { diff --git a/static_core/plugins/ets/stdlib/native/escompat/RegExp.cpp b/static_core/plugins/ets/stdlib/native/escompat/RegExp.cpp index 66b2f20b28..d2a39e8148 100644 --- a/static_core/plugins/ets/stdlib/native/escompat/RegExp.cpp +++ b/static_core/plugins/ets/stdlib/native/escompat/RegExp.cpp @@ -274,7 +274,7 @@ static void SetIndexField(ani_env *env, ani_class regexpResultClass, ani_object { ani_field indexField; ANI_FATAL_IF_ERROR(env->Class_FindField(regexpResultClass, INDEX_FIELD_NAME, &indexField)); - ANI_FATAL_IF_ERROR(env->Object_SetField_Double(regexpExecArrayObj, indexField, static_cast(index))); + ANI_FATAL_IF_ERROR(env->Object_SetField_Int(regexpExecArrayObj, indexField, static_cast(index))); } static void SetLastIndexField(ani_env *env, ani_object regexp, ani_field lastIndexField, ani_int value) diff --git a/static_core/plugins/ets/tests/ets_func_tests/escompat/RegExpExecArrayTest.ets b/static_core/plugins/ets/tests/ets_func_tests/escompat/RegExpExecArrayTest.ets index a1800a086f..bfc0ef505d 100644 --- a/static_core/plugins/ets/tests/ets_func_tests/escompat/RegExpExecArrayTest.ets +++ b/static_core/plugins/ets/tests/ets_func_tests/escompat/RegExpExecArrayTest.ets @@ -78,13 +78,10 @@ function testGet(): void { let match = new RegExpExecArray(0, "abc", ["x", "y", "z"]); arktest.assertEQ(match[0], "x", "testGet, int index, step 1"); - arktest.assertEQ(match[0 as number], "x", "testGet, number index, step 1"); arktest.assertEQ(match[1], "y", "testGet, int index, step 2"); - arktest.assertEQ(match[1 as number], "y", "testGet, number index, step 2"); arktest.assertEQ(match[2], "z", "testGet, int index, step 3"); - arktest.assertEQ(match[2 as number], "z", "testGet, number index, step 3"); arktest.expectError((): void => { match[-1] }, new RangeError("Out of bounds")) arktest.expectError((): void => { match[match.length] }, new RangeError("Out of bounds")) @@ -98,15 +95,12 @@ function testSet(): void { match[0] = "x1" arktest.assertEQ(match[0], "x1", "testSet, int index, step 1"); - arktest.assertEQ(match[0 as number], "x1", "testSet, number index, step 1"); match[1] = "y1" arktest.assertEQ(match[1], "y1", "testSet, int index, step 2"); - arktest.assertEQ(match[1 as number], "y1", "testSet, number index, step 2"); match[2] = "z1" arktest.assertEQ(match[2], "z1", "testSet, int index, step 3"); - arktest.assertEQ(match[2 as number], "z1", "testSet, number index, step 3"); arktest.expectError((): void => { match[-1] = "a" }, new RangeError("Out of bounds")) arktest.expectError((): void => { match[match.length] = "b" }, new RangeError("Out of bounds")) @@ -215,11 +209,9 @@ function testUnshift(): void { match[1] = "y1" arktest.assertEQ(match[1], "y1", "testSet, int index, step 2"); - arktest.assertEQ(match[1 as number], "y1", "testSet, number index, step 2"); match[2] = "z1" arktest.assertEQ(match[2], "z1", "testSet, int index, step 3"); - arktest.assertEQ(match[2 as number], "z1", "testSet, number index, step 3"); arktest.expectError((): void => { match[-1] = "a" }, new RangeError("Out of bounds")) arktest.expectError((): void => { match[match.length] = "b" }, new RangeError("Out of bounds")) diff --git a/static_core/plugins/ets/tests/ets_func_tests/escompat/RegExpMatchArrayTest.ets b/static_core/plugins/ets/tests/ets_func_tests/escompat/RegExpMatchArrayTest.ets index c268eb1326..c1eb04deb5 100644 --- a/static_core/plugins/ets/tests/ets_func_tests/escompat/RegExpMatchArrayTest.ets +++ b/static_core/plugins/ets/tests/ets_func_tests/escompat/RegExpMatchArrayTest.ets @@ -70,13 +70,10 @@ function testGet(): void { let match = new RegExpMatchArray(0, "abc", ["x", "y", "z"]); arktest.assertEQ(match[0], "x", "testGet, int index, step 1"); - arktest.assertEQ(match[0 as number], "x", "testGet, number index, step 1"); arktest.assertEQ(match[1], "y", "testGet, int index, step 2"); - arktest.assertEQ(match[1 as number], "y", "testGet, number index, step 2"); arktest.assertEQ(match[2], "z", "testGet, int index, step 3"); - arktest.assertEQ(match[2 as number], "z", "testGet, number index, step 3"); arktest.expectError((): void => { match[-1] }, new RangeError("Out of bounds")) arktest.expectError((): void => { match[match.length] }, new RangeError("Out of bounds")) @@ -90,15 +87,12 @@ function testSet(): void { match[0] = "x1" arktest.assertEQ(match[0], "x1", "testSet, int index, step 1"); - arktest.assertEQ(match[0 as number], "x1", "testSet, number index, step 1"); match[1] = "y1" arktest.assertEQ(match[1], "y1", "testSet, int index, step 2"); - arktest.assertEQ(match[1 as number], "y1", "testSet, number index, step 2"); match[2] = "z1" arktest.assertEQ(match[2], "z1", "testSet, int index, step 3"); - arktest.assertEQ(match[2 as number], "z1", "testSet, number index, step 3"); arktest.expectError((): void => { match[-1] = "a" }, new RangeError("Out of bounds")) arktest.expectError((): void => { match[match.length] = "b" }, new RangeError("Out of bounds")) -- Gitee