diff --git a/js_api_module/buffer/converter.cpp b/js_api_module/buffer/converter.cpp index b3ea54ae..0a9cea42 100644 --- a/js_api_module/buffer/converter.cpp +++ b/js_api_module/buffer/converter.cpp @@ -15,7 +15,6 @@ #include "converter.h" -#include #include #include #include @@ -301,13 +300,6 @@ bool IsValidHex(const string &hex) return isValid; } -bool convertToInt(const std::string& str, int& value) -{ - // 16 : the base is 16 - auto [ptr, ec] = std::from_chars(str.data(), str.data() + str.size(), value, 16); - return ec == std::errc{} && ptr == str.data() + str.size(); -} - string HexDecode(const string &hexStr) { string nums = ""; @@ -324,10 +316,8 @@ string HexDecode(const string &hexStr) if (!IsValidHex(hexStrTmp)) { break; } - if (!convertToInt(hexStrTmp, num)) { - HILOG_ERROR("convertToInt fail."); - return ""; - } + // 16 : the base is 16 + num = stoi(hexStrTmp, nullptr, 16); nums.push_back(static_cast(num)); } diff --git a/js_api_module/url/js_url.cpp b/js_api_module/url/js_url.cpp index 56c4800c..390c0733 100644 --- a/js_api_module/url/js_url.cpp +++ b/js_api_module/url/js_url.cpp @@ -14,7 +14,6 @@ */ #include "js_url.h" -#include #include #include #include "securec.h" @@ -44,12 +43,6 @@ namespace OHOS::Url { std::bitset(BitsetStatusFlag::MAX_BIT_SIZE)> g_specialCharForBit; - bool convertToInt(const std::string& str, int& value) - { - auto [ptr, ec] = std::from_chars(str.data(), str.data() + str.size(), value); - return ec == std::errc{} && ptr == str.data() + str.size(); - } - void PreliminaryWork() { std::vector g_specialSymbolsTmp = {'#', '%', '/', ':', '?', '@', '[', '\\', ']', '<', '>', '^', '|'}; @@ -319,11 +312,7 @@ namespace OHOS::Url { flags.set(static_cast(BitsetStatusFlag::BIT0)); return; } - int it = 0; - if (!convertToInt(input, it)) { - HILOG_ERROR("convertToInt fail."); - return; - } + int it = stoi(input); const int maxPort = 65535; // 65535:Maximum port number if (it > maxPort) { flags.set(static_cast(BitsetStatusFlag::BIT0)); @@ -364,14 +353,9 @@ namespace OHOS::Url { size_t left = pos + 1; char hexVal[3] = { 0 }; std::string val = ""; - int valNum = 0; while ((pos = str.find(".", left)) != std::string::npos) { val = str.substr(left, pos - left); - if (!convertToInt(val, valNum)) { - HILOG_ERROR("convertToInt fail."); - return val; - } - if (sprintf_s(hexVal, sizeof(hexVal), "%02x", valNum) == -1) { + if (sprintf_s(hexVal, sizeof(hexVal), "%02x", stoi(val)) == -1) { HILOG_ERROR("sprintf_s is falie"); return val; } @@ -380,11 +364,7 @@ namespace OHOS::Url { left = pos + 1; } val = str.substr(left); - if (!convertToInt(val, valNum)) { - HILOG_ERROR("convertToInt fail."); - return val; - } - if (sprintf_s(hexVal, sizeof(hexVal), "%02x", valNum) == -1) { + if (sprintf_s(hexVal, sizeof(hexVal), "%02x", stoi(val)) == -1) { HILOG_ERROR("sprintf_s is falie"); return val; } @@ -653,11 +633,7 @@ namespace OHOS::Url { number = num.size(); return num; } - int val = 0; - if (!convertToInt(num, val)) { - HILOG_ERROR("convertToInt fail."); - return num; - } + int val = stoi(num); std::vector nums; std::string res = ""; while (val > 0) { diff --git a/js_api_module/url/test/test_napi.cpp b/js_api_module/url/test/test_napi.cpp index 7efd15b9..c1204816 100644 --- a/js_api_module/url/test/test_napi.cpp +++ b/js_api_module/url/test/test_napi.cpp @@ -2557,48 +2557,4 @@ HWTEST_F(NativeEngineTest, testUrlutilities050, testing::ext::TestSize.Level0) std::string input = "99::1080:8:800:200C:417A"; OHOS::Url::FormatIpv6(input); ASSERT_STREQ(input.c_str(), "99:0:0:1080:8:800:200C:417A"); -} - -HWTEST_F(NativeEngineTest, testUrlutilities051, testing::ext::TestSize.Level0) -{ - std::string num = "123456a"; - size_t number = 0; - std::string rst = OHOS::Url::SplitNum(num, number); - ASSERT_STREQ(rst.c_str(), "123456a"); -} - -HWTEST_F(NativeEngineTest, testUrlutilities052, testing::ext::TestSize.Level0) -{ - std::string num = "12.34"; - size_t number = 0; - std::string rst = OHOS::Url::SplitNum(num, number); - ASSERT_STREQ(rst.c_str(), "12.34"); -} - -HWTEST_F(NativeEngineTest, testUrlutilities053, testing::ext::TestSize.Level0) -{ - std::string num = "12.34.56a:123:123abc.4a"; - std::string rst = OHOS::Url::DealIpv4(num); - ASSERT_STREQ(rst.c_str(), "123abc"); -} - -HWTEST_F(NativeEngineTest, testUrlutilities054, testing::ext::TestSize.Level0) -{ - std::string num = "56a:123:######.4a"; - std::string rst = OHOS::Url::DealIpv4(num); - ASSERT_STREQ(rst.c_str(), "######"); -} - -HWTEST_F(NativeEngineTest, testUrlutilities055, testing::ext::TestSize.Level0) -{ - std::string num = "12.34.56a:123a"; - std::string rst = OHOS::Url::DealIpv4(num); - ASSERT_STREQ(rst.c_str(), "123a"); -} - -HWTEST_F(NativeEngineTest, testUrlutilities056, testing::ext::TestSize.Level0) -{ - std::string num = "12.34.56a:&&&&&&"; - std::string rst = OHOS::Url::DealIpv4(num); - ASSERT_STREQ(rst.c_str(), "&&&&&&"); } \ No newline at end of file diff --git a/js_util_module/util/js_base64.cpp b/js_util_module/util/js_base64.cpp index 12f6a877..31515f5e 100644 --- a/js_util_module/util/js_base64.cpp +++ b/js_util_module/util/js_base64.cpp @@ -524,7 +524,6 @@ namespace OHOS::Util { reinterpret_cast(stdEncodeInfo->sinputEncoding), bufferSize) != EOK) { HILOG_ERROR("copy ret to arraybuffer error"); napi_delete_async_work(env, stdEncodeInfo->worker); - napi_close_handle_scope(env, scope); return; } napi_value result = nullptr; @@ -759,7 +758,6 @@ namespace OHOS::Util { reinterpret_cast(stdDecodeInfo->sinputDecoding), bufferSize) != EOK) { HILOG_ERROR("copy ret to arraybuffer error"); napi_delete_async_work(env, stdDecodeInfo->worker); - napi_close_handle_scope(env, scope); return; } napi_value result = nullptr;