1 /*
2 * Copyright (c) 2021 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 #include "ecmascript/mem/c_string.h"
17
18 #include <cmath>
19 #include <cstdlib>
20
21 #include "ecmascript/ecma_string-inl.h"
22 #include "ecmascript/js_symbol.h"
23 #include "ecmascript/mem/c_containers.h"
24 #include "libpandabase/macros.h"
25
26 namespace panda::ecmascript {
27 constexpr int BASE = 10;
28
CStringToLL(const CString & str)29 int64_t CStringToLL(const CString &str)
30 {
31 [[maybe_unused]] char *endPtr = nullptr;
32 int64_t result = std::strtoll(str.c_str(), &endPtr, BASE);
33 ASSERT(!(result == 0 && str.c_str() == endPtr) && "CString argument is not long long int");
34 return result;
35 }
36
CStringToULL(const CString & str)37 uint64_t CStringToULL(const CString &str)
38 {
39 [[maybe_unused]] char *endPtr = nullptr;
40 uint64_t result = std::strtoull(str.c_str(), &endPtr, BASE);
41 ASSERT(!(result == 0 && str.c_str() == endPtr) && "CString argument is not unsigned long long int");
42 return result;
43 }
44
CStringToF(const CString & str)45 float CStringToF(const CString &str)
46 {
47 [[maybe_unused]] char *endPtr = nullptr;
48 float result = std::strtof(str.c_str(), &endPtr);
49 ASSERT(result != HUGE_VALF && "CString argument is not float");
50 ASSERT(!(result == 0 && str.c_str() == endPtr) && "CString argument is not float");
51 return result;
52 }
53
CStringToD(const CString & str)54 double CStringToD(const CString &str)
55 {
56 [[maybe_unused]] char *endPtr = nullptr;
57 double result = std::strtod(str.c_str(), &endPtr);
58 ASSERT(result != HUGE_VALF && "CString argument is not double");
59 ASSERT(!(result == 0 && str.c_str() == endPtr) && "CString argument is not double");
60 return result;
61 }
62
63 template<class T>
ConvertToString(T sp)64 CString ConvertToString(T sp)
65 {
66 CString res;
67 res.reserve(sp.size());
68
69 // Also support ascii that great than 127, so using unsigned char here
70 constexpr size_t maxChar = std::numeric_limits<unsigned char>::max();
71
72 for (const auto &c : sp) {
73 if (c > maxChar) {
74 return "";
75 }
76 res.push_back(c);
77 }
78
79 return res;
80 }
81
82 // NB! the following function need additional mem allocation, don't use when unnecessary!
ConvertToString(const std::string & str)83 CString ConvertToString(const std::string &str)
84 {
85 CString res;
86 res.reserve(str.size());
87 for (auto c : str) {
88 res.push_back(c);
89 }
90 return res;
91 }
92
ConvertToString(const EcmaString * s,StringConvertedUsage usage)93 CString ConvertToString(const EcmaString *s, StringConvertedUsage usage)
94 {
95 if (s == nullptr) {
96 return CString("");
97 }
98 if (s->IsUtf16()) {
99 // Should convert utf-16 to utf-8, because uint16_t likely great than maxChar, will convert fail
100 bool modify = (usage != StringConvertedUsage::PRINT);
101 size_t len = base::utf_helper::Utf16ToUtf8Size(s->GetDataUtf16(), s->GetLength(), modify) - 1;
102 CVector<uint8_t> buf(len);
103 len = base::utf_helper::ConvertRegionUtf16ToUtf8(s->GetDataUtf16(), buf.data(), s->GetLength(), len, 0, modify);
104 Span<const uint8_t> sp(buf.data(), len);
105 return ConvertToString(sp);
106 }
107
108 Span<const uint8_t> sp(s->GetDataUtf8(), s->GetLength());
109 return ConvertToString(sp);
110 }
111
ConvertToString(JSTaggedValue key)112 CString ConvertToString(JSTaggedValue key)
113 {
114 ASSERT(key.IsStringOrSymbol());
115 if (key.IsString()) {
116 return ConvertToString(EcmaString::ConstCast(key.GetTaggedObject()));
117 }
118
119 ecmascript::JSTaggedValue desc = JSSymbol::Cast(key.GetTaggedObject())->GetDescription();
120 if (desc.IsUndefined()) {
121 return CString("Symbol()");
122 }
123
124 return ConvertToString(EcmaString::ConstCast(desc.GetTaggedObject()));
125 }
126
CstringConvertToStdString(const CString & str)127 std::string CstringConvertToStdString(const CString &str)
128 {
129 std::string res;
130 res.reserve(str.size());
131 for (auto c : str) {
132 res.push_back(c);
133 }
134 return res;
135 }
136 } // namespace panda::ecmascript
137