14514f5e3Sopenharmony_ci/*
24514f5e3Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License.
54514f5e3Sopenharmony_ci * You may obtain a copy of the License at
64514f5e3Sopenharmony_ci *
74514f5e3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
84514f5e3Sopenharmony_ci *
94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and
134514f5e3Sopenharmony_ci * limitations under the License.
144514f5e3Sopenharmony_ci */
154514f5e3Sopenharmony_ci
164514f5e3Sopenharmony_ci#include "ecmascript/base/string_helper.h"
174514f5e3Sopenharmony_ci#include "ecmascript/base/utf_helper.h"
184514f5e3Sopenharmony_ci#include "ecmascript/global_env.h"
194514f5e3Sopenharmony_ci#include "ecmascript/tests/test_helper.h"
204514f5e3Sopenharmony_ci
214514f5e3Sopenharmony_ciusing namespace panda::ecmascript;
224514f5e3Sopenharmony_ciusing namespace panda::ecmascript::base;
234514f5e3Sopenharmony_ciusing namespace panda::ecmascript::base::utf_helper;
244514f5e3Sopenharmony_ci
254514f5e3Sopenharmony_cinamespace panda::test {
264514f5e3Sopenharmony_ciclass StringHelperTest : public BaseTestWithScope<true> {
274514f5e3Sopenharmony_ci};
284514f5e3Sopenharmony_ci
294514f5e3Sopenharmony_ciHWTEST_F_L0(StringHelperTest, ReplaceAll)
304514f5e3Sopenharmony_ci{
314514f5e3Sopenharmony_ci    CString sourceStr = "H\\e\\l\\l\\o\\W\\o\\r\\l\\d!\0";
324514f5e3Sopenharmony_ci    const CString oldValue1 = "\\";
334514f5e3Sopenharmony_ci    const CString oldValue2 = "World";
344514f5e3Sopenharmony_ci    CString result = StringHelper::ReplaceAll(sourceStr, oldValue1, "");
354514f5e3Sopenharmony_ci    result = StringHelper::ReplaceAll(result, oldValue2, " OpenHarmony");
364514f5e3Sopenharmony_ci    EXPECT_STREQ(result.c_str(), "Hello OpenHarmony!");
374514f5e3Sopenharmony_ci}
384514f5e3Sopenharmony_ci
394514f5e3Sopenharmony_ciHWTEST_F_L0(StringHelperTest, Utf16ToU16String_Utf8ToU16String)
404514f5e3Sopenharmony_ci{
414514f5e3Sopenharmony_ci    const uint32_t utf16DataLen1 = 11;
424514f5e3Sopenharmony_ci    const uint16_t utf16Data1[utf16DataLen1] = { // "OpenHarmony"
434514f5e3Sopenharmony_ci        0x4f, 0x70, 0x65, 0x6e,
444514f5e3Sopenharmony_ci        0x48, 0x61, 0x72, 0x6d,
454514f5e3Sopenharmony_ci        0x6f, 0x6e, 0x79
464514f5e3Sopenharmony_ci    };
474514f5e3Sopenharmony_ci    std::u16string u16Str1 = StringHelper::Utf16ToU16String(utf16Data1, utf16DataLen1);
484514f5e3Sopenharmony_ci    std::string u16Value1 = std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t>{}.to_bytes(u16Str1);
494514f5e3Sopenharmony_ci    EXPECT_STREQ(u16Value1.c_str(), "OpenHarmony");
504514f5e3Sopenharmony_ci
514514f5e3Sopenharmony_ci    const uint32_t utf16DataLen2 = 2;
524514f5e3Sopenharmony_ci    const uint16_t utf16Data2[utf16DataLen2] = { 0x9e3f, 0x8499 }; // "鸿蒙"
534514f5e3Sopenharmony_ci    std::u16string u16Str2 = StringHelper::Utf16ToU16String(utf16Data2, utf16DataLen2);
544514f5e3Sopenharmony_ci    std::string u16Value2 = std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t>{}.to_bytes(u16Str2);
554514f5e3Sopenharmony_ci    EXPECT_STREQ(u16Value2.c_str(), "鸿蒙");
564514f5e3Sopenharmony_ci
574514f5e3Sopenharmony_ci    const uint32_t utf8DataLen1 = 11;
584514f5e3Sopenharmony_ci    const uint8_t utf8Data1[utf8DataLen1] = { // "OpenHarmony"
594514f5e3Sopenharmony_ci        0x4f, 0x70, 0x65, 0x6e,
604514f5e3Sopenharmony_ci        0x48, 0x61, 0x72, 0x6d,
614514f5e3Sopenharmony_ci        0x6f, 0x6e, 0x79
624514f5e3Sopenharmony_ci    };
634514f5e3Sopenharmony_ci    std::u16string u8Str1 = StringHelper::Utf8ToU16String(utf8Data1, utf8DataLen1);
644514f5e3Sopenharmony_ci    std::string u8Value1 = std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t>{}.to_bytes(u8Str1);
654514f5e3Sopenharmony_ci    EXPECT_STREQ(u8Value1.c_str(), "OpenHarmony");
664514f5e3Sopenharmony_ci
674514f5e3Sopenharmony_ci    const uint32_t utf8DataLen2 = 6;
684514f5e3Sopenharmony_ci    const uint8_t utf8Data2[utf8DataLen2] = { 0xe9, 0xb8, 0xbf, 0xe8, 0x92, 0x99 }; // "鸿蒙"
694514f5e3Sopenharmony_ci    std::u16string u8Str2 = StringHelper::Utf8ToU16String(utf8Data2, utf8DataLen2);
704514f5e3Sopenharmony_ci    std::string u8Value2 = std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t>{}.to_bytes(u8Str2);
714514f5e3Sopenharmony_ci    EXPECT_STREQ(u8Value2.c_str(), "鸿蒙");
724514f5e3Sopenharmony_ci}
734514f5e3Sopenharmony_ci
744514f5e3Sopenharmony_ciHWTEST_F_L0(StringHelperTest, Find_RFind)
754514f5e3Sopenharmony_ci{
764514f5e3Sopenharmony_ci    const std::string valueStr = "Hello worldworld";
774514f5e3Sopenharmony_ci    const std::string searchStr1 = "world";
784514f5e3Sopenharmony_ci    const std::string searchStr2 = "undefined";
794514f5e3Sopenharmony_ci    const std::u16string u16ValueStr = StringHelper::StringToU16string(valueStr);
804514f5e3Sopenharmony_ci    const std::u16string u16SearchStr1 = StringHelper::StringToU16string(searchStr1);
814514f5e3Sopenharmony_ci    const std::u16string u16SearchStr2 = StringHelper::StringToU16string(searchStr2);
824514f5e3Sopenharmony_ci
834514f5e3Sopenharmony_ci    size_t pos1 = StringHelper::Find(valueStr, searchStr1, 0);
844514f5e3Sopenharmony_ci    size_t pos2 = StringHelper::Find(valueStr, searchStr2, 0);
854514f5e3Sopenharmony_ci    size_t pos3 = StringHelper::Find(u16ValueStr, u16SearchStr1, 0);
864514f5e3Sopenharmony_ci    size_t pos4 = StringHelper::Find(u16ValueStr, u16SearchStr2, 0);
874514f5e3Sopenharmony_ci    size_t pos5 = StringHelper::RFind(u16ValueStr, u16SearchStr1, 17); // 17 : Search to the end
884514f5e3Sopenharmony_ci    size_t pos6 = StringHelper::RFind(u16ValueStr, u16SearchStr2, 17); // 17 : Search to the end
894514f5e3Sopenharmony_ci    EXPECT_EQ(pos1, 6U);
904514f5e3Sopenharmony_ci    EXPECT_EQ(pos2, std::string::npos);
914514f5e3Sopenharmony_ci    EXPECT_EQ(pos3, 6U);
924514f5e3Sopenharmony_ci    EXPECT_EQ(pos4, std::string::npos);
934514f5e3Sopenharmony_ci    EXPECT_EQ(pos5, 11U);
944514f5e3Sopenharmony_ci    EXPECT_EQ(pos6, std::string::npos);
954514f5e3Sopenharmony_ci}
964514f5e3Sopenharmony_ci
974514f5e3Sopenharmony_ciHWTEST_F_L0(StringHelperTest, ToUpper_ToLower_ToLocaleUpper_ToLocaleLower)
984514f5e3Sopenharmony_ci{
994514f5e3Sopenharmony_ci    const std::u16string u16SourceStr1 = StringHelper::StringToU16string("AbCdEfGhIjKlMnOpQrStUvWxYz");
1004514f5e3Sopenharmony_ci    std::string upperStr = StringHelper::ToUpper(u16SourceStr1);
1014514f5e3Sopenharmony_ci    std::string lowerStr = StringHelper::ToLower(u16SourceStr1);
1024514f5e3Sopenharmony_ci    EXPECT_STREQ(upperStr.c_str(), "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
1034514f5e3Sopenharmony_ci    EXPECT_STREQ(lowerStr.c_str(), "abcdefghijklmnopqrstuvwxyz");
1044514f5e3Sopenharmony_ci
1054514f5e3Sopenharmony_ci    icu::Locale locale("el", "Latn", "GR");
1064514f5e3Sopenharmony_ci    const std::u16string u16SourceStr2 = StringHelper::StringToU16string("Greek : Αυτό είναι ένα δοκιμαστικό κείμενο.");
1074514f5e3Sopenharmony_ci    std::string localeUpperStr = StringHelper::ToLocaleUpper(u16SourceStr2, locale);
1084514f5e3Sopenharmony_ci    EXPECT_STREQ(localeUpperStr.c_str(), "GREEK : ΑΥΤΟ ΕΙΝΑΙ ΕΝΑ ΔΟΚΙΜΑΣΤΙΚΟ ΚΕΙΜΕΝΟ.");
1094514f5e3Sopenharmony_ci
1104514f5e3Sopenharmony_ci    const std::u16string u16SourceStr3 = StringHelper::StringToU16string("GREEK : ΑΥΤΌ ΕΊΝΑΙ ΈΝΑ ΔΟΚΙΜΑΣΤΙΚΌ ΚΕΊΜΕΝΟ.");
1114514f5e3Sopenharmony_ci    std::string localeLowerStr = StringHelper::ToLocaleLower(u16SourceStr3, locale);
1124514f5e3Sopenharmony_ci    EXPECT_STREQ(localeLowerStr.c_str(), "greek : αυτό είναι ένα δοκιμαστικό κείμενο.");
1134514f5e3Sopenharmony_ci}
1144514f5e3Sopenharmony_ci
1154514f5e3Sopenharmony_ciHWTEST_F_L0(StringHelperTest, FindFromU16ToUpper)
1164514f5e3Sopenharmony_ci{
1174514f5e3Sopenharmony_ci    const std::u16string u16SourceStr = StringHelper::StringToU16string("HELLO WORLD!");
1184514f5e3Sopenharmony_ci    const uint32_t utf16DataLen = 5;
1194514f5e3Sopenharmony_ci    const uint32_t utf16DataLenUpper = 9;
1204514f5e3Sopenharmony_ci    uint16_t utf16Data[utf16DataLen] = { // "world"
1214514f5e3Sopenharmony_ci        0x77, 0x6f, 0x72, 0x6c, 0x64
1224514f5e3Sopenharmony_ci    };
1234514f5e3Sopenharmony_ci    uint16_t utf16DataUpper[utf16DataLenUpper] = { // "WORLD"
1244514f5e3Sopenharmony_ci        0x57, 0x4f, 0x52, 0x4c, 0x44
1254514f5e3Sopenharmony_ci    };
1264514f5e3Sopenharmony_ci    size_t pos1 = StringHelper::FindFromU16ToUpper(u16SourceStr, utf16Data);
1274514f5e3Sopenharmony_ci    size_t pos2 = StringHelper::FindFromU16ToUpper(u16SourceStr, utf16DataUpper);
1284514f5e3Sopenharmony_ci    EXPECT_EQ(pos1, 6U);
1294514f5e3Sopenharmony_ci    EXPECT_EQ(pos2, 6U);
1304514f5e3Sopenharmony_ci}
1314514f5e3Sopenharmony_ci
1324514f5e3Sopenharmony_ciHWTEST_F_L0(StringHelperTest, UnicodeFromUtf8)
1334514f5e3Sopenharmony_ci{
1344514f5e3Sopenharmony_ci    int maxLen = 1;
1354514f5e3Sopenharmony_ci    const uint8_t *p = new uint8_t[6] { 0x00 };
1364514f5e3Sopenharmony_ci    const uint8_t **pp = &p;
1374514f5e3Sopenharmony_ci    const uint8_t oneByteVaild1[1] = { 0x00 };
1384514f5e3Sopenharmony_ci    const uint8_t oneByteVaild2[1] = { BIT_MASK_1 - 0x01 };
1394514f5e3Sopenharmony_ci    const uint8_t oneByteUnvaild[1] = { BIT_MASK_1 };
1404514f5e3Sopenharmony_ci    EXPECT_EQ(StringHelper::UnicodeFromUtf8(oneByteVaild1, maxLen, pp), 0);
1414514f5e3Sopenharmony_ci    EXPECT_EQ(StringHelper::UnicodeFromUtf8(oneByteVaild2, maxLen, pp), 127);
1424514f5e3Sopenharmony_ci    EXPECT_EQ(StringHelper::UnicodeFromUtf8(oneByteUnvaild, maxLen, pp), -1);
1434514f5e3Sopenharmony_ci
1444514f5e3Sopenharmony_ci    maxLen = 2;
1454514f5e3Sopenharmony_ci    const uint8_t twoByteVaild[2] = { BIT_MASK_3 - 0x01, BIT_MASK_2 - 0x01 };
1464514f5e3Sopenharmony_ci    const uint8_t twoByteUnvaild1[2] = { BIT_MASK_2, BIT_MASK_2 };
1474514f5e3Sopenharmony_ci    const uint8_t twoByteUnvaild2[2] = { BIT_MASK_3, BIT_MASK_1 };
1484514f5e3Sopenharmony_ci    EXPECT_EQ(StringHelper::UnicodeFromUtf8(twoByteVaild, maxLen, pp), 2047); // 2047 : utf8 [0xDF, 0xBF]
1494514f5e3Sopenharmony_ci    EXPECT_EQ(StringHelper::UnicodeFromUtf8(twoByteUnvaild1, maxLen, pp), -1);
1504514f5e3Sopenharmony_ci    EXPECT_EQ(StringHelper::UnicodeFromUtf8(twoByteUnvaild2, maxLen, pp), -1);
1514514f5e3Sopenharmony_ci
1524514f5e3Sopenharmony_ci    maxLen = 3;
1534514f5e3Sopenharmony_ci    const uint8_t threeByteVaild[3] = { BIT_MASK_4 - 0x01, BIT_MASK_2 - 0x01, BIT_MASK_2 - 0x01 };
1544514f5e3Sopenharmony_ci    const uint8_t threeByteUnvaild1[3] = { BIT_MASK_3, BIT_MASK_1, BIT_MASK_2 };
1554514f5e3Sopenharmony_ci    const uint8_t threeByteUnvaild2[3] = { BIT_MASK_3, BIT_MASK_2, BIT_MASK_1 };
1564514f5e3Sopenharmony_ci    const uint8_t threeByteUnvaild3[3] = { BIT_MASK_4, BIT_MASK_1, BIT_MASK_1 };
1574514f5e3Sopenharmony_ci    const uint8_t threeByteUnvaild4[3] = { BIT_MASK_4, BIT_MASK_2, BIT_MASK_2 };
1584514f5e3Sopenharmony_ci    EXPECT_EQ(StringHelper::UnicodeFromUtf8(threeByteVaild, maxLen, pp), 65535); // 65535 : utf8 [0xEF, 0xBF, 0xBF]
1594514f5e3Sopenharmony_ci    EXPECT_EQ(StringHelper::UnicodeFromUtf8(threeByteUnvaild1, maxLen, pp), -1);
1604514f5e3Sopenharmony_ci    EXPECT_EQ(StringHelper::UnicodeFromUtf8(threeByteUnvaild2, maxLen, pp), -1);
1614514f5e3Sopenharmony_ci    EXPECT_EQ(StringHelper::UnicodeFromUtf8(threeByteUnvaild3, maxLen, pp), -1);
1624514f5e3Sopenharmony_ci    EXPECT_EQ(StringHelper::UnicodeFromUtf8(threeByteUnvaild4, maxLen, pp), -1);
1634514f5e3Sopenharmony_ci
1644514f5e3Sopenharmony_ci    maxLen = 4;
1654514f5e3Sopenharmony_ci    const uint8_t fourByteVaild[4] = {
1664514f5e3Sopenharmony_ci        BIT_MASK_5 - 0x01, BIT_MASK_2 - 0x01, BIT_MASK_2 - 0x01, BIT_MASK_2 - 0x01
1674514f5e3Sopenharmony_ci    };
1684514f5e3Sopenharmony_ci    const uint8_t fourByteUnvaild1[4] = { BIT_MASK_4, BIT_MASK_1, BIT_MASK_1, BIT_MASK_2 };
1694514f5e3Sopenharmony_ci    const uint8_t fourByteUnvaild2[4] = { BIT_MASK_4, BIT_MASK_1, BIT_MASK_2, BIT_MASK_1 };
1704514f5e3Sopenharmony_ci    const uint8_t fourByteUnvaild3[4] = { BIT_MASK_4, BIT_MASK_2, BIT_MASK_1, BIT_MASK_1 };
1714514f5e3Sopenharmony_ci    const uint8_t fourByteUnvaild4[4] = { BIT_MASK_5, BIT_MASK_1, BIT_MASK_1, BIT_MASK_1 };
1724514f5e3Sopenharmony_ci    const uint8_t fourByteUnvaild5[4] = { BIT_MASK_5, BIT_MASK_2, BIT_MASK_2, BIT_MASK_2 };
1734514f5e3Sopenharmony_ci    EXPECT_EQ(StringHelper::UnicodeFromUtf8(
1744514f5e3Sopenharmony_ci        fourByteVaild, maxLen, pp), 2097151); // 2097151 : utf [0xF7, 0xBF, 0xBF, 0xBF]
1754514f5e3Sopenharmony_ci    EXPECT_EQ(StringHelper::UnicodeFromUtf8(fourByteUnvaild1, maxLen, pp), -1);
1764514f5e3Sopenharmony_ci    EXPECT_EQ(StringHelper::UnicodeFromUtf8(fourByteUnvaild2, maxLen, pp), -1);
1774514f5e3Sopenharmony_ci    EXPECT_EQ(StringHelper::UnicodeFromUtf8(fourByteUnvaild3, maxLen, pp), -1);
1784514f5e3Sopenharmony_ci    EXPECT_EQ(StringHelper::UnicodeFromUtf8(fourByteUnvaild4, maxLen, pp), -1);
1794514f5e3Sopenharmony_ci    EXPECT_EQ(StringHelper::UnicodeFromUtf8(fourByteUnvaild5, maxLen, pp), -1);
1804514f5e3Sopenharmony_ci}
1814514f5e3Sopenharmony_ci
1824514f5e3Sopenharmony_ciHWTEST_F_L0(StringHelperTest, Append_SplitString)
1834514f5e3Sopenharmony_ci{
1844514f5e3Sopenharmony_ci    const std::u16string str1 = StringHelper::StringToU16string("Hello ");
1854514f5e3Sopenharmony_ci    const std::u16string str2 = StringHelper::StringToU16string("world!");
1864514f5e3Sopenharmony_ci    std::u16string u16Result = StringHelper::Append(str1, str2);
1874514f5e3Sopenharmony_ci    std::string result = StringHelper::U16stringToString(u16Result);
1884514f5e3Sopenharmony_ci    EXPECT_STREQ(result.c_str(), "Hello world!");
1894514f5e3Sopenharmony_ci
1904514f5e3Sopenharmony_ci    const std::string delimiter = " ";
1914514f5e3Sopenharmony_ci    std::vector<std::string> resultList = StringHelper::SplitString(result, delimiter);
1924514f5e3Sopenharmony_ci    EXPECT_STREQ(resultList[0].c_str(), "Hello");
1934514f5e3Sopenharmony_ci    EXPECT_STREQ(resultList[1].c_str(), "world!");
1944514f5e3Sopenharmony_ci}
1954514f5e3Sopenharmony_ci
1964514f5e3Sopenharmony_ciHWTEST_F_L0(StringHelperTest, GetSpecifiedLine)
1974514f5e3Sopenharmony_ci{
1984514f5e3Sopenharmony_ci    const std::string srcStr = "Hello\nworld\n!";
1994514f5e3Sopenharmony_ci    std::string resLine1 = StringHelper::GetSpecifiedLine(srcStr, 1);
2004514f5e3Sopenharmony_ci    std::string resLine2 = StringHelper::GetSpecifiedLine(srcStr, 2);
2014514f5e3Sopenharmony_ci    std::string resLine3 = StringHelper::GetSpecifiedLine(srcStr, 3);
2024514f5e3Sopenharmony_ci    EXPECT_STREQ(resLine1.c_str(), "Hello");
2034514f5e3Sopenharmony_ci    EXPECT_STREQ(resLine2.c_str(), "world");
2044514f5e3Sopenharmony_ci    EXPECT_STREQ(resLine3.c_str(), "!");
2054514f5e3Sopenharmony_ci    const std::string srcStr1 = "Hello\\nworld\\n!";
2064514f5e3Sopenharmony_ci    std::string resLine11 = StringHelper::GetSpecifiedLine(srcStr1, 1);
2074514f5e3Sopenharmony_ci    std::string resLine22 = StringHelper::GetSpecifiedLine(srcStr1, 2);
2084514f5e3Sopenharmony_ci    std::string resLine33 = StringHelper::GetSpecifiedLine(srcStr1, 3);
2094514f5e3Sopenharmony_ci    EXPECT_STREQ(resLine11.c_str(), "Hello");
2104514f5e3Sopenharmony_ci    EXPECT_STREQ(resLine22.c_str(), "world");
2114514f5e3Sopenharmony_ci    EXPECT_STREQ(resLine33.c_str(), "!");
2124514f5e3Sopenharmony_ci}
2134514f5e3Sopenharmony_ci
2144514f5e3Sopenharmony_ciHWTEST_F_L0(StringHelperTest, Replace)
2154514f5e3Sopenharmony_ci{
2164514f5e3Sopenharmony_ci    CString sourceStr = "@arkui-x.test.path";
2174514f5e3Sopenharmony_ci    CString result = StringHelper::Replace(sourceStr, "@arkui-x.", "@ohos:");
2184514f5e3Sopenharmony_ci    EXPECT_STREQ(result.c_str(), "@ohos:test.path");
2194514f5e3Sopenharmony_ci}
2204514f5e3Sopenharmony_ci}  // namespace panda::test
221