1/*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16/*
17 * @tc.name:String
18 * @tc.desc:test String
19 * @tc.type: FUNC
20 * @tc.require: issueI5NO8G
21 */
22print("string".length);
23print("string"[3]);
24print("string".at(-1));
25
26let tmp = '今天吃什么:告诉了会晤今天吃什么促“让今天吃什么下来好起来”今天吃今天吃什么于6月1日于北京今天吃什么面。大杂烩中国都五年来首位访华\
27的今天吃什么王刚说,你吵的菜今天吃什么了赞不绝口,但是今天吃什么吃,他接到一个快递单号为#nsExpress3SF#123456789的包裹';
28let flag = tmp;
29let str1 = tmp.substring(0, 111) + ',,,,,,,,,,,,,,,,,,,,,,,' + tmp.substring(111 + 23);
30let str2 = tmp.substring(0, 111) + 'nsExpress3SF#123456789的' + tmp.substring(111 + 23,111 + 24) + '需';
31let str3 = tmp.substring(0, 111) + 'nsExpress3SF#123456789的' + tmp.substring(111 + 23);
32print((flag === str1).toString());
33print((flag === str2).toString());
34print((flag === str3).toString());
35
36let utf81 = '12312463347659634287446568765344234356798653455678899865746435323456709876543679334675235523463756875153764736256\
37235252537585734523759078945623465357867096834523523765868';
38let utf82 = utf81.substring(0, 169) + '8';
39let utf83 = utf81.substring(0, 169) + '0';
40print((utf81 === utf82).toString());
41print((utf81 === utf83).toString());
42
43function foo(a) {
44    return a;
45}
46try {
47    for (let i = 0; i < 25; i++) {
48        foo += foo;
49    }
50} catch (e) {
51    print(e.message);
52}
53let string1 = "fdjDJSAjkdfalDDGETG";
54let string2 = string1.substring(1);
55print(str1.charAt());
56print(str2.charCodeAt());
57print(str1.substr(2, 4));
58print(string2.toLowerCase(2, 4));
59
60
61let tmp1 = '今天吃什么:哈哈哈会晤美国phonePlatformphonePlatformphonePlatformphonePlatformphonePlatformphonePlatformphonePlatformphonePlatformphonePlatformphonePlatform国务卿促“让今天吃什么下来好起来”今天吃今天吃什么于6月1日于北京今天吃什么面。大杂烩中国都五年来首位访华\
62的今天吃什么王刚说,你吵的菜今天吃什么了赞不绝口,但是今天吃什么吃,他接到一个快递单号为#nsExpress3SF#123456789的包裹';
63let flag1 = tmp1;
64let str = tmp1.substring(13, 143);
65let str4 = tmp1.substring(13, 26);
66let str5 = "phonePlatformphonePlatformphonePlatformphonePlatformphonePlatformphonePlatformphonePlatformphonePlatformphonePlatformphonePlatform";
67let str6 = "phonePlatform";
68var obj = {phonePlatformphonePlatformphonePlatformphonePlatformphonePlatformphonePlatformphonePlatformphonePlatformphonePlatformphonePlatform: "wode",
69phonePlatform: "wode1"};
70
71print(obj[str]);
72print(obj[str4]);
73print((str === str5).toString());
74print((str4 == str6).toString());
75
76var s0 = String.fromCharCode();
77var s1 = String.fromCharCode(65);
78var s2 = String.fromCharCode(65, 66.678);
79var s3 = String.fromCharCode(0x12014);
80var s4 = String.fromCharCode(true);
81print(s0);
82print(s1);
83print(s2);
84print(s3);
85print(s4);
86
87let name1 = 'testfiles就发(2).png';
88let filename1 = name1.substring(0, 14);
89let kk1 = filename1.toLowerCase();
90print(kk1);
91
92let name2 = 'testfileswodetwwwpng';
93let filename2 = name2.substring(0, 14);
94let kk2 = filename2.toLowerCase();
95print(kk2);
96
97var strA = name2 + name1;
98for (let i = 0; i < 100; i++) {
99strA.toLocaleString();
100}
101print("true");
102
103var a = "";
104var b = a.split("");
105var c = a.split("1");
106print(b.length);
107print(c.length);
108var a1 = "123_352_42342_483297538927943872943_12i2";
109var a2 = "12哈哈3尼352尼42好呀342_4832975389279你们43872943尼12i2";
110var a3 = a2.substring(1);
111var b1 = a1.split();
112var c1 = a1.split("");
113var d1 = a1.split(" ");
114var e1 = a1.split("_");
115var f1 = a1.split("_", 2);
116var g1 = a2.split("尼", 3);
117var r1 = a3.split("尼", 44);
118var k1 = a3.split("尼", 0);
119var k2 = a3.split("4", 5);
120print(b1);
121print(c1);
122print(d1);
123print(e1);
124print(f1);
125print(g1);
126print(r1);
127print(k1.length);
128print(k2);
129try {
130    var sym = Symbol();
131    a2.split(sym, 0);
132} catch (e) {
133    print(e.message);
134}
135
136var test1 = "fun123ctionManualChapter123Tip1_352_42342_483297538927943872943_12i2哈哈哈";
137var test2 = test1.split("_");
138var test3 = test2[0];
139var test4 = "1111fun123ctionManualChapter123Tip1"
140let pos = test4.indexOf(test3);
141print(pos);
142
143Object.defineProperty(Array.prototype, "1", {
144    value:37,
145    writable:false,
146});
147var test5 = "123_wode2222222222222222_fast_哈哈哈";
148var test6 = "123_wode2222222222222222_fast_hhh";
149var test7 = test5.split("_");
150var test8 = test6.split("_");
151print(test7);
152print(test8);
153
154var test9 = "abc"
155var test10 = new Number(4);
156test10[Symbol.split] = RegExp.prototype[Symbol.split];
157print(test9.split(test10));
158var test11 = test1.substring(0, 31);
159var test12 = "1111fun123ctionManualChapter123Tip12222"
160var test13 = test12.split(test11);
161var test14 = test12.lastIndexOf(test11);
162print(test13);
163print(test14);
164
165let s = "abaddbdc"
166s.split("")
167let split = s.split("")
168split[0] = "0"
169print(split)
170print(s.split(""))
171s.split("b")
172split = s.split("b")
173split[2] = 1
174print(split)
175print(s.split("b"))
176s = "d的發的給的点点滴滴多"
177s.split("的")
178split = s.split("的")
179split[2] = 1
180print(split)
181print(s.split("的"))
182
183let newStr = new String("adcdcdccda")
184newStr.split("d")
185split = newStr.split("d")
186split[0] = "0"
187print(split)
188print(newStr.split("d"))
189
190var Tag = {
191    t82 : "82"
192}
193var tag1 = "823".substring(0, 2)
194var val = "val";
195var m = new Map();
196m.set(tag1, val);
197m.forEach((a, b)=>{
198  print("Tag.t82 == b : ", Tag.t82 == b);
199})
200
201try {
202  "123".padEnd(Math.pow(2, 40))
203} catch (e) {
204    print(e)
205}
206
207print("abc".startsWith("a", Infinity))
208print("abc".startsWith("a", -Infinity))
209print("abc".endsWith("c", Infinity))
210
211const strings = [
212    "ab\uD800",
213    "ab\uD800c",
214    "\uDFFFab",
215    "c\uDFFFab",
216    "abc",
217    "ab\uD83D\uDE04c",
218];
219
220for (const str of strings) {
221    print(str.isWellFormed());
222}
223
224const strings2 = [
225    "ab\uD800",
226    "ab\uD800c",
227    "\uDFFFab",
228    "c\uDFFFab",
229    "abc",
230    "ab\uD83D\uDE04c",
231];
232
233for (const str of strings2) {
234    print(str.toWellFormed());
235}
236
237const text_str = "So we beat on, boats against the current, borne back ceaselessly into the past."
238const search_str1 = "So we"
239const search_str2 = "borne back"
240print(text_str.startsWith(search_str1));
241print(text_str.startsWith(search_str1, 10));
242print(text_str.startsWith(search_str2, 42));
243print(text_str.startsWith(search_str1, text_str.length - 1));
244const text_str2 = "So we beat on, boats against the current," + " borne back ceaselessly into the past."
245print(text_str2.startsWith(search_str1));
246print(text_str2.startsWith(search_str1, 10));
247print(text_str2.startsWith(search_str2, 42));
248print(text_str2.startsWith(search_str1, text_str.length - 1));
249
250const str7 = "测试"
251const iter = str7[Symbol.iterator]();
252print(iter.next().value);
253print(iter.next().value);
254print(iter.next().done);
255print(iter.next().value);
256
257const str_endsWith1 = 'this is a test for string endsWith!';
258print(str_endsWith1.endsWith('string', '25'));    // true
259print(str_endsWith1.endsWith('string', 25));      // true
260print(str_endsWith1.endsWith('endsWith!', 25));   // false
261print(str_endsWith1.endsWith('endsWith!'));       // true
262print(str_endsWith1.endsWith('endsWith!', -1));   // false
263print(str_endsWith1.endsWith('endsWith!', 100));  // true
264print(str_endsWith1.endsWith('string', 25.3));    // true
265
266let arr = Array(14);
267let tree_str = arr + 512;
268tree_str.substring(0,arr.length);
269print("sub_string test success!");
270
271const v0 = [3.0,-1.5053];
272const v4 = v0.join();
273print(v4.startsWith(v4,v0))
274let strFromCodePoint = String.fromCodePoint(49,50,51,0)
275print(strFromCodePoint);
276
277{
278    const v1 = [5.101952721978723,-1.2836190437943387,-1.1815850946332814e+308,2.220446049250313e-16,-680.5741773264597,0.8970365641701358,NaN];
279    function f2(a3, a4, a5) {
280        return BigInt64Array.bind();
281    }
282
283    const o8 = {
284        "get": f2,
285    };
286    const v10 = new Proxy(v1, o8);
287    try {
288        ("n").search(v10)
289    } catch (err) {
290        print(err);
291    }
292}
293
294// startsWith 边界值测试
295const textStr = "abcd"
296const matchStr = "abcd"
297const offset = 0
298
299// case offset = 0, match true
300class C3 {
301    constructor() {
302        let bb = textStr.startsWith(matchStr, offset);
303        print(bb); // true
304    }
305}
306
307new C3();
308
309// case offset = 1, match false
310const offset1 = 1
311
312class C4 {
313    constructor() {
314        let flag = textStr.startsWith(matchStr, offset1);
315        print(flag); // false
316    }
317}
318
319new C4();
320
321print("abcd汉字".startsWith("abcd"));
322
323{
324    class C2 {
325        constructor(a4) {
326            a4(-3, parseFloat, ..."isFrozen");
327        }
328    }
329
330    try {
331        new C2(new C2);
332    } catch (err) {
333        print(err);
334    }
335}
336let greeting = "          Hello World!          ";
337let fooStr = "foo     ";
338let barStr = "     bar";
339let chinese = "          我,是一个中文句子    我前面有一些空格,我中间有一些空格。     我后面也有一些空格。          ";
340// test for trimStart()
341print(greeting.trimStart());
342print(fooStr.trimStart());
343print(barStr.trimStart());
344print(chinese.trimStart());
345// test for trimEnd()
346print(greeting.trimEnd());
347print(fooStr.trimEnd());
348print(barStr.trimEnd());
349print(chinese.trimEnd());
350// test for trimLeft()
351print(greeting.trimLeft());
352print(fooStr.trimLeft());
353print(barStr.trimLeft());
354print(chinese.trimLeft());
355// test for trimRight()
356print(greeting.trimRight());
357print(fooStr.trimRight());
358print(barStr.trimRight());
359print(chinese.trimRight());
360
361// Test case for padStart()
362let start_str = "abc";
363print(start_str.padStart(10, "123"));
364print(start_str.padStart(5.666, "123"));
365print(start_str.padStart(NaN, "123"));
366print(start_str.padStart(-Infinity, "123"));
367print(start_str.padStart(0, "123"));
368print(start_str.padStart(10, ""));
369
370// test for padEnd()
371print("abc".padEnd(5, 'abc'));
372print('abc'.padEnd(undefined, 'def'));
373print('abc'.padEnd(null, 'def') );
374print('abc'.padEnd(NaN, 'def') );
375print('abc'.padEnd(-Infinity, 'def') );
376print('abc'.padEnd(0, 'def') );
377print('abc'.padEnd(-1, 'def') );
378print('abc'.padEnd(5, ''))
379
380let arr1 = [0,0,0,0,0];
381print(arr1.toString("a"));
382const abc_str = "abc";
383const negativeHeapNumber = -1 * 2**32;
384const positiveHeapNumber = 2**32;
385print(abc_str.substr(positiveHeapNumber));
386print(abc_str.substr(negativeHeapNumber, positiveHeapNumber));
387print(abc_str.substr(-abc_str.length, positiveHeapNumber));
388print(abc_str.substr(0, positiveHeapNumber));
389print(abc_str.substr(-2, positiveHeapNumber));
390print(abc_str.substr(-1, positiveHeapNumber));
391
392{
393    function f(n) { return "cba".endsWith("a", n); }
394    print(f());
395    print(f(4294967296));
396}
397
398[
399  ['a', 'b', 'c'],
400  'abc',
401  new Uint8Array([8, 9, 10])
402].forEach((v) => {
403    // Test the limit value.
404    print(v.at(-Infinity) == undefined);
405});
406
407const arr_at = new Uint8Array([8, 9, 10]);
408ArkTools.arrayBufferDetach(arr_at.buffer);
409try {
410    arr_at.at(0);
411} catch (e) {
412    print(e instanceof TypeError)
413}
414
415print('trimStart' == String.prototype.trimLeft.name);
416print('trimEnd' == String.prototype.trimRight.name);
417
418{
419    const abc_str = "abc";
420    const negativeHeapNumber = -1 * 2**32;
421    const positiveHeapNumber = 2**32;
422    print(abc_str.substring(positiveHeapNumber))
423    print(abc_str.substring(negativeHeapNumber, positiveHeapNumber))
424    print(abc_str.substring(-abc_str.length, positiveHeapNumber))
425    print(abc_str.substring(0, positiveHeapNumber))
426    print(abc_str.substring(-2, positiveHeapNumber))
427    print(abc_str.substring(-1, positiveHeapNumber))
428}
429
430{
431    const localestr = "island"
432    print(localestr.toLocaleUpperCase());
433    print(localestr.toLocaleUpperCase('TR'));
434}
435
436// Test empty string search an empty string(Array.tostring).
437let indexOf = String.prototype.indexOf;
438print(0 == "".indexOf([]));
439
440Object.defineProperty(RegExp,"name",{value:1});
441RegExp.toString();
442print("native Func toString test Success!")
443
444
445print("================Test unroll char copy================");
446let srcutf8 = "1234567812345678123456781234567812345678123456781234567812345678";  // bytes == 64
447let srcutf16 = "一二三四五六七八一二三四五六七八一二三四五六七八一二三四五六七八"; // bytes == 64
448
449print(srcutf8.slice(0, 0)); // bytes == 0
450print(srcutf8.slice(64, 64)); // bytes == 0
451print(srcutf8.slice(0, 6)); // bytes < 8
452print(srcutf8.slice(0, 8)); // bytes == 8
453print(srcutf8.slice(0, 14)); // bytes < 16
454print(srcutf8.slice(0, 16)); // bytes == 16
455print(srcutf8.slice(0, 21)); // bytes > 16 && bytes % 16 != 0
456print(srcutf8.slice(0, 24)); // bytes == 3 * 8
457print(srcutf8.slice(0, 26)); // bytes > 16 && bytes % 16 > 8 && bytes % 8 > 0
458print(srcutf8.slice(0, 32)); // bytes == 2 * 16
459print(srcutf8.slice(0, 50)); // bytes > 16 && bytes % 16 != 0
460print(srcutf8.slice(0, 56)); // bytes == 7 * 8
461print(srcutf8.slice(0, 64)); // bytes == 4 * 16
462
463print(srcutf16.slice(0, 0)); // bytes == 0
464print(srcutf16.slice(32, 32)); // bytes == 0
465print(srcutf16.slice(0, 3)); // bytes < 8
466print(srcutf16.slice(0, 4)); // bytes == 8
467print(srcutf16.slice(0, 7)); // bytes < 16
468print(srcutf16.slice(0, 8)); // bytes == 16
469print(srcutf16.slice(0, 10)); // bytes > 16 && bytes % 16 != 0
470print(srcutf16.slice(0, 12)); // bytes == 3 * 8
471print(srcutf16.slice(0, 13)); // bytes > 16 && bytes % 16 > 8 && bytes % 8 > 0
472print(srcutf16.slice(0, 16)); // bytes == 2 * 16
473print(srcutf16.slice(0, 25)); // bytes > 16 && bytes % 16 != 0
474print(srcutf16.slice(0, 28)); // bytes == 7 * 8
475print(srcutf16.slice(0, 32)); // bytes == 4 * 16
476
477var str11;
478var result11;
479var re11 = /[Cz]/;
480var re12 = /([Cz])/;
481
482function createHaystack() {
483    let s = "abCdefgz";
484    for (let i = 0; i < 3; i++) s += s;
485    return s;
486}
487
488function SimpleSplit2() {
489  result11 = str11.split(re11);
490  if (result11.length != 17) {
491      print("fail!");
492  }
493}
494
495function Function1Replace(re) {
496  result11 = re[Symbol.replace](str11, String);
497  if (result11 != "abCdefgzabCdefgzabCdefgzabCdefgzabCdefgzabCdefgzabCdefgzabCdefgz") {
498      print("fail!");
499  }
500}
501
502str11 = createHaystack();
503
504function SimpleMatch1() {
505  result11 = str11.match(re11);
506  if (result11[0] != 'C') {
507      print("fail!");
508  }
509}
510
511for (let i = 0; i < 10; i++) {
512    SimpleMatch1();
513}
514
515for (let i = 0; i < 10; i++) {
516    SimpleSplit2();
517}
518
519for (let i = 0; i < 100; i++) {
520    Function1Replace(re12);
521}
522