diff --git a/static_core/plugins/ets/stdlib/escompat/RegExp.ets b/static_core/plugins/ets/stdlib/escompat/RegExp.ets index a2591a0df85ce6f93a8828456065aaa67a9f9d0c..390aec699b2207864a00490346c6f9481b84b272 100644 --- a/static_core/plugins/ets/stdlib/escompat/RegExp.ets +++ b/static_core/plugins/ets/stdlib/escompat/RegExp.ets @@ -20,11 +20,10 @@ type RegExpGroupsContainer = Record /** * Regular expression result descriptor */ -export class RegExpResultArray /*extends Array*/ { +export class RegExpResultArray extends Array { /** true if match present */ readonly isCorrect: boolean - /** result itself */ - protected result_: Array = [] + private resultRaw_: String = "" /** The 0-based index of the match in the string */ protected index_: number = 0 @@ -42,28 +41,26 @@ export class RegExpResultArray /*extends Array*/ { /** named groups names and indeces */ public groups: RegExpGroupsContainer | undefined = undefined - protected isUndefined: Array = [] - - 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 this.input_ = input - this.result_ = result this.indices_ = indices - } - constructor(index : int, input : String, result : Array, indices : Array>, - isUndefined : Array) { - this.isCorrect = true - this.index_ = index - this.input_ = input - this.result_ = result - this.indices_ = indices - this.isUndefined = isUndefined + this.extendTo(result.length, undefined as (String | undefined)) + for (let i: int = 0; i < result.length; i++) { + this.$_set(i, result.$_get(i)) + } } protected constructor() { + super(0) + this.isCorrect = false + } + public get result(): Array { + return this } private setNumberedField(index: number, value: String): void { @@ -99,7 +96,7 @@ export class RegExpResultArray /*extends Array*/ { this.updateIndicesAndResult(hasIndices) RegExp.input_ = input RegExp.leftContext_ = input.substring(0, index) - RegExp.rightContext_ = input.substring(index + res.result[0].length) + RegExp.rightContext_ = input.substring(index + res.result[0]!.length) } /** @@ -111,10 +108,9 @@ export class RegExpResultArray /*extends Array*/ { */ public $_get(index: String): String { if (index == "0") { - if (this.result_.length > 0) { - return this.result_[0] - } - throw new Error("result is empty") + const v = this.$_get(0) + if (v !== undefined) return v + throw new Error("result[0] is undefined") } throw new Error("unsupported field name") } @@ -127,53 +123,17 @@ export class RegExpResultArray /*extends Array*/ { * @returns resulting string */ public $_get(index: int): String | undefined { - if (index < this.isUndefined.length && this.isUndefined[index]) { - return undefined - } - return this.result_[index]; + return super.$_get(index) } - - /** - * Returns result string by index - * - * @param index - * - * @returns resulting string - */ public $_get(index: number): String | undefined { - return this.$_get(index.toInt()); + return this.$_get(index.toInt()) } - - /** - * Sets value by index - * - * @param index - * - * @param val - */ - public $_set(index: int, val: String) { - this.result_[index] = val - } - - /** - * Sets value by index - * - * @param index - * - * @param val - */ - public $_set(index: number, val: String) { - return this.$_set(index.toInt(), val); - } - - get length(): int { - return Double.toInt(this.result_.length) + public $_set(index: int, val: String | undefined): void { + super.$_set(index, val) } - - public get result(): Array { - return this.result_ + public $_set(index: number, val: String | undefined): void { + this.$_set(index.toInt(), val) } - public get indices(): Array> { return this.indices_ } @@ -184,8 +144,8 @@ export class RegExpResultArray /*extends Array*/ { * @returns a string representing the match result */ public toString(): String { - if (this.result_.length > 0) { - return this.result_.toString(); + if (this.length > 0) { + return super.toString(); } throw new Error("result is empty") } @@ -201,554 +161,72 @@ export class RegExpResultArray /*extends Array*/ { if (index == 0) { RegExp.lastMatch_ = str } - if (index == this.result_.length - 1) { + if (index == this.length - 1) { RegExp.lastParen_ = str } this.setNumberedField(index, str) } private updateIndicesAndResult(hasIndices: boolean): void { - let data = this.resultRaw_.split(',') - this.result_ = new Array(data.length / 2) - this.isUndefined = new Array(data.length / 2) + const data = this.resultRaw_.split(',') + const n: int = (data.length / 2) + + if (n > this.length) { + this.extendTo(n, undefined as (String | undefined)) + } else if (n < this.length) { + this.shrinkTo(n) + } + if (hasIndices) { - this.indices_ = new Array>(data.length / 2) - for (let i = 0; i < data.length; i+= 2) { + 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 pos: int = (i / 2); if (index == -1 || endIndex == -1) { - this.indices_[i / 2] = [0, 0] - this.isUndefined[i / 2] = true + this.indices_[pos] = [0, 0] } else { - this.indices_[i / 2] = [index, endIndex] - this.result_[i / 2] = this.input_.substring(index, endIndex) - this.processStaticFields(i / 2, this.result_[i / 2]) - this.isUndefined[i / 2] = false + this.indices_[pos] = [index, endIndex] + const str = this.input_.substring(index, endIndex) + this.$_set(pos, str) + this.processStaticFields(pos, str) } } } else { this.indices_ = [] - for (let i = 0; i < data.length; i+= 2) { + for (let i = 0; i < data.length; i += 2) { const index = Number.parseInt(data[i]) const endIndex = Number.parseInt(data[i + 1]) + const pos: int = (i / 2); if (index != -1 && endIndex != -1) { - this.result_[i / 2] = this.input_.substring(index, endIndex) - this.processStaticFields(i / 2, this.result_[i / 2]) - this.isUndefined[i / 2] = false - } else { - this.isUndefined[i / 2] = true + const str = this.input_.substring(index, endIndex) + this.$_set(pos, str) + this.processStaticFields(pos, str) } } } } - /** - * Removes and returns the last match result in the array. - * Simulates the behavior of Array.prototype.pop(). - * - * @returns The last matched string if present; otherwise, undefined. - */ - public pop(): String | undefined { - return this.result_.pop() - } - - /** - * Takes an integer value and returns the item at that index, - * allowing for positive and negative integers. - */ - public at(index: number): String | undefined { - return this.result_.at(index) - } - - /** - * Takes an integer value and returns the item at that index, - * allowing for positive and negative integers. - */ - public at(index: int): String | undefined { - return this.result_.at(index) - } - - /** - * Creates a new array with all elements concatenated. - */ - public concat(...items: FixedArray>): Array { - return this.result_.concat(...items) - } - - /** - * Makes a shallow copy of part to another location in the same array. - */ - public copyWithin(target: number, start: number, end?: Number): RegExpResultArray { - this.result_.copyWithin(target, start, end) - return this - } - - /** - * Makes a shallow copy of part to another location in the same array. - */ - public copyWithin(target: int, start: int, end: int): RegExpResultArray { - this.result_.copyWithin(target, start, end) - return this - } - - /** - * Makes a shallow copy of part to another location in the same array. - */ - public copyWithin(target: int, start: int): RegExpResultArray { - this.result_.copyWithin(target, start) - return this - } - - /** - * Makes a shallow copy of part to another location in the same array. - */ - public copyWithin(target: int): RegExpResultArray { - this.result_.copyWithin(target) - return this - } - - /** - * Changes all elements to a static value. - */ - public fill(value: String, start?: Number, end?: Number): RegExpResultArray { - this.result_.fill(value, start, end) - return this - } - - /** - * Changes all elements to a static value. - */ - public fill(value: String, start: int, end: int): RegExpResultArray { - this.result_.fill(value, start, end) - return this - } - - /** - * Returns elements that meet the condition specified in a callback function. - */ - public filter(predicate: (value: String, index: number, array: RegExpResultArray) => boolean): Array { - return this.result_.filter((value: String, index: number, array: Array) => { - return predicate(value, index, this) - }) - } - - /** - * Returns the value of the first element where predicate is true. - */ - public find(predicate: (value: String, index: number, array: RegExpResultArray) => boolean): String | undefined { - return this.result_.find((value: String, index: number, array: Array) => { - return predicate(value, index, this) - }) - } - - /** - * Returns the index of the first element where predicate is true. - */ - public findIndex(predicate: (value: String, index: number, array: RegExpResultArray) => boolean): number { - return this.result_.findIndex((value: String, index: number, array: Array) => { - return predicate(value, index, this) - }) - } - - /** - * Returns the value of the last element that satisfies the testing function. - */ - public findLast(predicate: (elem: String, index: number, array: RegExpResultArray) => boolean): String | undefined { - return this.result_.findLast((elem: String, index: number, array: Array) => { - return predicate(elem, index, this) - }) - } - - /** - * Returns the index of the last element that satisfies the testing function. - */ - public findLastIndex(predicate: (element: String, index: number, array: RegExpResultArray) => boolean): number { - return this.result_.findLastIndex((element: String, index: number, array: Array) => { - return predicate(element, index, this) - }) - } - - /** - * Creates a new array with all sub-array elements concatenated. - */ - public flat(depth: number): Array { - return this.result_.flat(depth) - } - - /** - * Creates a new array with all sub-array elements concatenated. - */ - public flat(depth: int): Array { - return this.result_.flat(depth) - } - - /** - * Creates a new array with all sub-array elements concatenated. - */ - public flat(): Array { - return this.result_.flat() - } - - /** - * Applies flat and then map. - */ - public flatMap(fn: (v: String, k: number, arr: RegExpResultArray) => U): Array { - return this.result_.flatMap((v: String, k: number, arr: Array) => { - return fn(v, k, this) - }) - } - - /** - * Determines whether all members satisfy the specified test. - */ - public every(predicate: (value: String, index: number, array: RegExpResultArray) => boolean): boolean { - return this.result_.every((value: String, index: number, array: Array) => { - return predicate(value, index, this) - }) - } - - /** - * Determines whether any element satisfies the specified test. - */ - public some(predicate: (value: String, index: number, array: RegExpResultArray) => boolean): boolean { - return this.result_.some((value: String, index: number, array: Array) => { - return predicate(value, index, this) - }) - } - - /** - * Calls the callback function for all elements and accumulates the result. - */ - public reduce(callbackfn: (previousValue: String, currentValue: String, index: number, array: RegExpResultArray) => String): String { - return this.result_.reduce((previousValue: String, currentValue: String, index: number, array: Array) => { - return callbackfn(previousValue, currentValue, index, this) - }) - } - - /** - * Calls the callback function for all elements and accumulates the result. - */ - public reduce(callbackfn: (previousValue: U, currentValue: String, index: number, array: RegExpResultArray) => U, initialValue: U): U { - return this.result_.reduce((previousValue: U, currentValue: String, index: number, array: Array) => { - return callbackfn(previousValue, currentValue, index, this) - }, initialValue) - } - - /** - * Calls the callback function for all elements in descending order and accumulates the result. - */ - public reduceRight(callbackfn: (previousValue: U, currentValue: String, index: number, array: RegExpResultArray) => U, initialValue: U): U { - return this.result_.reduceRight((previousValue: U, currentValue: String, index: number, array: Array) => { - return callbackfn(previousValue, currentValue, index, this) - }, initialValue) - } - - /** - * Calls the callback function for all elements in descending order and accumulates the result. - */ - public reduceRight(callbackfn: (previousValue: String, currentValue: String, index: number, array: RegExpResultArray) => String): String { - return this.result_.reduceRight((previousValue: String, currentValue: String, index: number, array: Array) => { - return callbackfn(previousValue, currentValue, index, this) - }) - } - - /** - * Performs the specified action for each element. - */ - public forEach(callbackfn: (value: String, index: number, array: RegExpResultArray) => void): void { - this.result_.forEach((value: String, index: number, array: Array) => { - callbackfn(value, index, this) - }) - } - - /** - * Creates a new array with elements selected from start to end. - */ - public slice(start?: Number, end?: Number): Array { - return this.result_.slice(start, end) - } - - /** - * Creates a new array with elements selected from start to end. - */ - public slice(start: int, end: int): Array { - return this.result_.slice(start, end) - } - - /** - * Creates a new array with elements selected from start to end. - */ - public slice(start: int): Array { - return this.result_.slice(start) - } - - /** - * Returns the last index of the specified element. - */ - public lastIndexOf(searchElement: String, fromIndex: int): int { - return this.result_.lastIndexOf(searchElement, fromIndex) - } - - /** - * Returns the last index of the specified element. - */ - public lastIndexOf(searchElement: String, fromIndex: number|undefined): number { - // NOTE(cheezzario) workaround for #23963 - return this.result_.lastIndexOf(searchElement, fromIndex === undefined ? undefined : Int.valueOf(fromIndex.toInt())) - } - - /** - * Returns the last index of the specified element. - */ - public lastIndexOf(searchElement: String): number { - return this.result_.lastIndexOf(searchElement) - } - - /** - * Creates a string by concatenating all elements, separated by a separator. - */ - public join(sep?: String): string { - return this.result_.join(sep) - } - - /** - * Returns a locale string representing the array. - */ - public toLocaleString(locales: Object, options: Object): string { - return this.result_.toLocaleString(locales, options) - } - - /** - * Returns a locale string representing the array. - */ - public toLocaleString(locales: Object): string { - return this.result_.toLocaleString(locales) - } - - /** - * Returns a locale string representing the array. - */ - public toLocaleString(): string { - return this.result_.toLocaleString() - } - - /** - * Copying version of the splice method. - */ - public toSpliced(start?: Number, delete?: Number): Array { - return this.result_.toSpliced(start, delete) - } - - /** - * Copying version of the splice method. - */ - public toSpliced(start: number, delete: number, ...items: FixedArray): Array { - return this.result_.toSpliced(start, delete, ...items) - } - - /** - * Copying version of the splice method. - */ - public toSpliced(start: int, delete: int, ...items: FixedArray): Array { - return this.result_.toSpliced(start, delete, ...items) - } - - /** - * Copying version of the splice method. - */ - public toSpliced(start: int): Array { - return this.result_.toSpliced(start) - } - - /** - * Checks whether an array includes a certain value. - */ - public includes(val: String, fromIndex?: Number): boolean { - return this.result_.includes(val, fromIndex) - } - - /** - * Returns the first index of the specified element. - */ - public indexOf(val: String, fromIndex: int): int { - return this.result_.indexOf(val, fromIndex) - } - - /** - * Returns the first index of the specified element. - */ - public indexOf(val: String, fromIndex?: Number): number { - // NOTE(cheezzario) workaround for #23963 - return this.result_.indexOf(val, fromIndex === undefined ? undefined : Int.valueOf(fromIndex.toInt())) - } - - /** - * Returns the first index of the specified element. - */ - public indexOf(val: String): number { - return this.result_.indexOf(val) - } - - /** - * Copying version of the sort method using default comparator. - */ - public toSorted(): Array { - return this.result_.toSorted() - } - - /** - * Copying version of the sort method. - */ - public toSorted(comparator: (a: String, b: String) => number): Array { - return this.result_.toSorted(comparator) - } - - /** - * Reverses the elements in place. - */ - public reverse(): RegExpResultArray { - this.result_.reverse() - return this - } - - /** - * Copying version of the reverse method. - */ - public toReversed(): Array { - return this.result_.toReversed() - } - - /** - * Copying version of setting a value at an index. - */ - public with(index: number, value: String): Array { - return this.result_.with(index, value) - } - - /** - * Copying version of setting a value at an index. - */ - public with(index: int, value: String): Array { - return this.result_.with(index, value) - } - - /** - * Removes the first element and returns it. - */ - public shift(): String | undefined { - return this.result_.shift() - } - - /** - * Adds elements to the end and returns the new length. - */ - public push(...val: String[]): number { - return this.result_.push(...val) - } - - /** - * Adds elements to the end and returns the new length. - */ - public pushECMA(...val: String[]): number { - return this.result_.pushECMA(...val) - } - - /** - * Changes contents by removing/replacing existing elements and/or adding new elements. - */ - public splice(start: number, delete: Number | undefined, ...items: String[]): Array { - return this.result_.splice(start, delete, ...items) - } - - /** - * Changes contents by removing/replacing existing elements and/or adding new elements. - */ - public splice(start: int, delete: int, ...items: String[]): Array { - return this.result_.splice(start, delete, ...items) - } - - /** - * Changes contents by removing elements from start to end. - */ - public splice(start: number): Array { - return this.result_.splice(start) - } - - /** - * Changes contents by removing elements from start to end. - */ - public splice(start: int): Array { - return this.result_.splice(start) - } - - /** - * Adds elements to the beginning and returns the new length. - */ - public unshift(...values: String[]): number { - return this.result_.unshift(...values) - } - - /** - * Returns an iterator over all indices. - */ - public keys(): IterableIterator { - return this.result_.keys() - } - - /** - * Returns an iterator over all values. - */ - public $_iterator(): IterableIterator { - return this.result_.$_iterator() - } - - /** - * Returns an iterator over all values. - */ - public values(): IterableIterator { - return this.result_.values() - } - - /** - * Returns an iterable of key, value pairs. - */ - public entries(): IterableIterator<[number, String]> { - return this.result_.entries() - } - - /** - * Calls a callback function on each element and returns an array with the results. - */ - public map(callbackfn: (value: String, index: number, array: RegExpResultArray) => U): Array { - return this.result_.map((value: String, index: number, array: Array) => { - return callbackfn(value, index, this) - }) - } - - /** - * Reorders elements using comparator function. - */ - public sort(comparator?: (a: String, b: String) => number): RegExpResultArray { - this.result_.sort(comparator) - return this - } } export class RegExpMatchArray extends RegExpResultArray { private hasIndex_: boolean = true private hasInput_: boolean = true - constructor(index : int, input : String, result : Array, indices : Array>, - isUndefined : Array) { - super(Double.toInt(index), input, result, indices, isUndefined) - }; - - constructor(index: int, input: String, result: Array, indices: Array>) { + constructor(index: int, input: String, result: Array, indices: Array>) { super(Double.toInt(index), input, result, indices) } - constructor(index: int, input: String, result: Array) { - super(Double.toInt(index), input, result, []) + 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) } protected constructor() { @@ -829,12 +307,20 @@ export class RegExpMatchArray extends RegExpResultArray { } export class RegExpExecArray extends RegExpResultArray { - constructor(index: int, input: String, result: Array, indices: Array>) { - super(index, input, result, indices) + constructor(index: int, input: String, result: Array, indices: Array>) { + super(Double.toInt(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: int, input: String, result: Array) { - super(index, input, result, []) + constructor(index: number, input: String, result: Array) { + this(Double.toInt(index), input, result) } protected constructor() { @@ -1440,13 +926,12 @@ export class RegExp extends Object { if (n == 0) { return null } - return new RegExpMatchArray(-1, "", matches.toArray(), indices, isUndefined) + return new RegExpMatchArray(-1, "", matches.toUndefinableArray(), indices) } else { let matchStr = result.$_get(0) matches.pushBack(matchStr) indices = indices.concat(result.indices) - isUndefined = isUndefined.concat(result.isUndefined) if (matchStr == "") { this.lastIndex = RegExp.advanceStringIndex(str, this.lastIndex, this.unicode) } diff --git a/static_core/plugins/ets/stdlib/std/containers/UndefinableStringArray.ets b/static_core/plugins/ets/stdlib/std/containers/UndefinableStringArray.ets index 3d7badaf62b1c291102ea0654ee0116246c4dc94..b1a84c58a6576efcfbe36ec5998c287f305bbe3e 100644 --- a/static_core/plugins/ets/stdlib/std/containers/UndefinableStringArray.ets +++ b/static_core/plugins/ets/stdlib/std/containers/UndefinableStringArray.ets @@ -97,6 +97,14 @@ export namespace containers { return newData } + public toUndefinableArray(): (String | undefined)[] { + let newData = new (String | undefined)[this.curSize] + for (let i = 0; i < this.curSize; ++i) { + newData[i] = this.data[i] + } + return newData + } + private static getNewCapacity(currentCapacity: int): int { const fastGrowThreshold = 8192 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 90fae40c5919cc5bade229ea072b68ce34335b1e..0ee93bd5e03385a9d00577d4a8153b2176d4ad52 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 @@ -265,7 +265,7 @@ function testSplice(): void { */ function testConcat(): void { let match = new RegExpExecArray(0, "abc", ["a", "b"]); - let result = match.concat(["c", "d"]); + let result = match.concat(["c", "d"] as Array); arktest.assertEQ(result.length, 4, "concat length"); arktest.assertEQ(result[0], "a", "original elements"); @@ -360,8 +360,8 @@ function testIncludes(): void { function testFilter(): void { let match = new RegExpExecArray(0, "abc", ["apple", "banana", "apricot", "cherry"]); - let filtered = match.filter((value: String, index: number, array: RegExpResultArray) => { - return value.startsWith("a"); + let filtered = match.filter((value: String | undefined, index: number, array: Array) => { + return value!.startsWith("a"); }); arktest.assertEQ(filtered.length, 2, "filter result length"); @@ -375,8 +375,8 @@ function testFilter(): void { function testMap(): void { let match = new RegExpExecArray(0, "abc", ["a", "b", "c"]); - let mapped = match.map((value: String, index: number, array: RegExpResultArray) => { - return value.toUpperCase(); + let mapped = match.map((value: String | undefined, index: number, array: Array) => { + return value!.toUpperCase(); }); arktest.assertEQ(mapped.length, 3, "map result length"); @@ -391,14 +391,14 @@ function testMap(): void { function testFind(): void { let match = new RegExpExecArray(0, "abc", ["apple", "banana", "apricot"]); - let found = match.find((value: String, index: number, array: RegExpResultArray) => { - return value.startsWith("b"); + let found = match.find((value: String | undefined, index: number, array: Array) => { + return value!.startsWith("b"); }); arktest.assertEQ(found, "banana", "find returns element"); - let notFound = match.find((value: String, index: number, array: RegExpResultArray) => { - return value.startsWith("z"); + let notFound = match.find((value: String | undefined, index: number, array: Array) => { + return value!.startsWith("z"); }); arktest.assertEQ(notFound, undefined, "find returns undefined for not found"); @@ -410,14 +410,14 @@ function testFind(): void { function testFindIndex(): void { let match = new RegExpExecArray(0, "abc", ["apple", "banana", "apricot"]); - let foundIndex = match.findIndex((value: String, index: number, array: RegExpResultArray) => { - return value.startsWith("b"); + let foundIndex = match.findIndex((value: String | undefined, index: number, array: Array) => { + return value!.startsWith("b"); }); arktest.assertEQ(foundIndex, 1, "findIndex returns index"); - let notFoundIndex = match.findIndex((value: String, index: number, array: RegExpResultArray) => { - return value.startsWith("z"); + let notFoundIndex = match.findIndex((value: String | undefined, index: number, array: Array) => { + return value!.startsWith("z"); }); arktest.assertEQ(notFoundIndex, -1, "findIndex returns -1 for not found"); @@ -429,14 +429,14 @@ function testFindIndex(): void { function testEvery(): void { let match = new RegExpExecArray(0, "abc", ["apple", "apricot", "avocado"]); - let allStartWithA = match.every((value: String, index: number, array: RegExpResultArray) => { - return value.startsWith("a"); + let allStartWithA = match.every((value: String | undefined, index: number, array: Array) => { + return value!.startsWith("a"); }); arktest.assertTrue(allStartWithA, "every returns true when all match"); - let allStartWithB = match.every((value: String, index: number, array: RegExpResultArray) => { - return value.startsWith("b"); + let allStartWithB = match.every((value: String | undefined, index: number, array: Array) => { + return value!.startsWith("b"); }); arktest.assertFalse(allStartWithB, "every returns false when not all match"); @@ -448,14 +448,14 @@ function testEvery(): void { function testSome(): void { let match = new RegExpExecArray(0, "abc", ["apple", "banana", "cherry"]); - let someStartWithB = match.some((value: String, index: number, array: RegExpResultArray) => { - return value.startsWith("b"); + let someStartWithB = match.some((value: String | undefined, index: number, array: Array) => { + return value!.startsWith("b"); }); arktest.assertTrue(someStartWithB, "some returns true when any match"); - let someStartWithZ = match.some((value: String, index: number, array: RegExpResultArray) => { - return value.startsWith("z"); + let someStartWithZ = match.some((value: String | undefined, index: number, array: Array) => { + return value!.startsWith("z"); }); arktest.assertFalse(someStartWithZ, "some returns false when none match"); @@ -467,14 +467,14 @@ function testSome(): void { function testReduce(): void { let match = new RegExpExecArray(0, "abc", ["a", "b", "c"]); - let concatenated = match.reduce((prev: String, curr: String, index: number, array: RegExpResultArray) => { - return prev + curr; + let concatenated = match.reduce((prev: String | undefined, curr: String | undefined, index: number, array: Array) => { + return prev! + curr!; }); arktest.assertEQ(concatenated, "abc", "reduce concatenates"); - let withInitial = match.reduce((prev: String, curr: String, index: number, array: RegExpResultArray) => { - return prev + curr; + let withInitial = match.reduce((prev: String | undefined, curr: String | undefined, index: number, array: Array) => { + return prev! + curr!; }, "start:"); arktest.assertEQ(withInitial, "start:abc", "reduce with initial value"); @@ -487,8 +487,8 @@ function testForEach(): void { let match = new RegExpExecArray(0, "abc", ["a", "b", "c"]); let result = ""; - match.forEach((value: String, index: number, array: RegExpResultArray) => { - result += value + index.toString(); + match.forEach((value: String | undefined, index: number, array: Array) => { + result += value! + index.toString(); }); arktest.assertEQ(result, "a0b1c2", "forEach iterates correctly"); diff --git a/static_core/plugins/ets/tests/ets_func_tests/escompat/RegExpMatchAllTest.ets b/static_core/plugins/ets/tests/ets_func_tests/escompat/RegExpMatchAllTest.ets index e3f9a13ba7014871afba6c5c22290dc650e15e8d..9bdf75f1568962c6cf3b8422e49d9a1d5dae0735 100644 --- a/static_core/plugins/ets/tests/ets_func_tests/escompat/RegExpMatchAllTest.ets +++ b/static_core/plugins/ets/tests/ets_func_tests/escompat/RegExpMatchAllTest.ets @@ -101,7 +101,7 @@ function checkTestResult(actual : RegExpMatchArray | null, expected : RegExpMatc } for (let i = 0; i < actual.result.length; i++ ) { - if (!actual.result[i].equals(expected.result[i])) { + if (!actual.result[i]!.equals(expected.result[i])) { console.println ("check index : " + i); console.println("content of result array from actual is not equals to expected"); console.println("actual : " + actual.result[i]); diff --git a/static_core/plugins/ets/tests/ets_func_tests/escompat/RegExpTest.ets b/static_core/plugins/ets/tests/ets_func_tests/escompat/RegExpTest.ets index d0aeddcc7f903382e544ed380f8e3b3aebbc896b..4c81ec5e0fb5e34bf19768ed53be075e8d420ce4 100644 --- a/static_core/plugins/ets/tests/ets_func_tests/escompat/RegExpTest.ets +++ b/static_core/plugins/ets/tests/ets_func_tests/escompat/RegExpTest.ets @@ -386,18 +386,23 @@ function GreekAlphabetTest(): void { GreekAlphabetTestFunction48(); } +function asNonUndefined(r: RegExpExecArray | null): Array { + return r!.map((x: String | undefined): String => x!); +} + + function main(): void { { let reg = new RegExp("ab|cd||") - arktest.assertEQ(JSON.stringify(reg.exec("cd")), '["cd"]') + arktest.assertEQ(JSON.stringify(asNonUndefined(reg.exec("cd"))), '["cd"]') } { let reg = new RegExp("ab||") - arktest.assertEQ(JSON.stringify(reg.exec("cd")), '[""]') + arktest.assertEQ(JSON.stringify(asNonUndefined(reg.exec("cd"))), '[""]') } { let reg = new RegExp("ab|cd|ef") - arktest.assertEQ(JSON.stringify(reg.exec("cd")), '["cd"]') + arktest.assertEQ(JSON.stringify(asNonUndefined(reg.exec("cd"))), '["cd"]') } { let str = "😀"; @@ -723,14 +728,14 @@ function main(): void { try { let pattern = "\\["; let reg = new RegExp(pattern); - arktest.assertEQ(pattern + " " + JSON.stringify(reg.exec(str)), '\\[ ["["]') + arktest.assertEQ(pattern + " " + JSON.stringify(asNonUndefined(reg.exec(str))), '\\[ ["["]') } catch (error) { arktest.assertEQ(true, false, "Expected SyntaxError but got none"); } try { let pattern = "\\\["; let reg = new RegExp(pattern); - arktest.assertEQ(pattern + " " + JSON.stringify(reg.exec(str)), '\\[ ["["]') + arktest.assertEQ(pattern + " " + JSON.stringify(asNonUndefined(reg.exec(str))), '\\[ ["["]') } catch (error) { arktest.assertEQ(true, false, "Expected SyntaxError but got none"); } @@ -751,14 +756,14 @@ function main(): void { try { let pattern = "\\\\\\["; let reg = new RegExp(pattern); - arktest.assertEQ(pattern + " " + JSON.stringify(reg.exec(str)), '\\\\\\[ ["\\\\["]') + arktest.assertEQ(pattern + " " + JSON.stringify(asNonUndefined(reg.exec(str))), '\\\\\\[ ["\\\\["]') } catch (error) { arktest.assertEQ(true, false, "Expected SyntaxError but got none"); } try { let pattern = "\\\\\\\["; let reg = new RegExp(pattern); - arktest.assertEQ(pattern + " " + JSON.stringify(reg.exec(str)), '\\\\\\[ ["\\\\["]') + arktest.assertEQ(pattern + " " + JSON.stringify(asNonUndefined(reg.exec(str))), '\\\\\\[ ["\\\\["]') } catch (error) { arktest.assertEQ(true, false, "Expected SyntaxError but got none"); } @@ -800,6 +805,6 @@ function main(): void { { let reg = new RegExp("a(?:|x)$"); - arktest.assertEQ(JSON.stringify(reg.exec("ax")), '["ax"]') + arktest.assertEQ(JSON.stringify(asNonUndefined(reg.exec("ax"))), '["ax"]') } } diff --git a/static_core/plugins/ets/tests/stdlib-templates/utils/test_check_test_regexp_result.j2 b/static_core/plugins/ets/tests/stdlib-templates/utils/test_check_test_regexp_result.j2 index d51cb0614a0e2f298849ca301dc78fbc084c59af..f4249b7120e96fed4158cc2375801f75c7be1f77 100644 --- a/static_core/plugins/ets/tests/stdlib-templates/utils/test_check_test_regexp_result.j2 +++ b/static_core/plugins/ets/tests/stdlib-templates/utils/test_check_test_regexp_result.j2 @@ -50,7 +50,7 @@ function checkTestResultMatchArray(actual : RegExpMatchArray, expected : RegExpM } for (let i = 0; i < actual.result.length; i++ ) { - if (!actual.result[i].equals(expected.result[i])) { + if (!actual.result[i]!.equals(expected.result[i]!)) { console.println ("check index : " + i); console.println("content of result array from actual is not equals to expected"); console.println("actual : " + actual.result[i]); @@ -97,7 +97,7 @@ function checkTestResultExecArray(actual : RegExpExecArray, expected : RegExpExe } for (let i = 0; i < actual.result.length; i++ ) { - if (!actual.result[i].equals(expected.result[i])) { + if (!actual.result[i]!.equals(expected.result[i]!)) { console.println ("check index : " + i); console.println("content of result array from actual is not equals to expected"); console.println("actual : " + actual.result[i]);