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 #ifndef ECMASCRIPT_MEM_C_STRING_H
17 #define ECMASCRIPT_MEM_C_STRING_H
18
19 #include <sstream>
20 #include <string>
21 #include <string_view>
22
23 #include "ecmascript/common.h"
24 #include "ecmascript/mem/caddress_allocator.h"
25
26 namespace panda::ecmascript {
27 class EcmaString;
28 class JSTaggedValue;
29
30 using CString = std::basic_string<char, std::char_traits<char>, CAddressAllocator<char>>;
31 using C16String = std::basic_string<char16_t, std::char_traits<char16_t>, CAddressAllocator<char16_t>>;
32 using CStringStream = std::basic_stringstream<char, std::char_traits<char>, CAddressAllocator<char>>;
33
34 struct CStringHash {
35 using argument_type = panda::ecmascript::CString;
36 using result_type = std::size_t;
37
operatorCStringHash38 size_t operator()(const CString &str) const noexcept
39 {
40 return std::hash<std::string_view>()(std::string_view(str.data(), str.size()));
41 }
42 };
43
44 constexpr int DEC_BASE = 10;
45
46 // PRINT will skip '\0' in utf16 during conversion of utf8
47 enum StringConvertedUsage { PRINT, LOGICOPERATION };
48
49 long CStringToL(const CString &str);
50 int64_t CStringToLL(const CString &str);
51 uint64_t CStringToULL(const CString &str);
52 float CStringToF(const CString &str);
53 double CStringToD(const CString &str);
54
55 CString ConvertToString(const std::string &str);
56 std::string PUBLIC_API ConvertToStdString(const CString &str);
57
58 // '\u0000' is skip according to holdZero
59 // cesu8 means non-BMP1 codepoints should encode as 1 utf8 string
60 CString PUBLIC_API ConvertToString(const JSThread *thread, const ecmascript::EcmaString *s,
61 StringConvertedUsage usage = StringConvertedUsage::PRINT, bool cesu8 = false);
62
63 #if ENABLE_NEXT_OPTIMIZATION
64 template <typename DstType>
65 bool AppendSpecialDouble(DstType &str, double d);
66 template <typename DstType>
67 void AppendDoubleToString(DstType &str, double d);
68 template <typename DstType>
69 void AppendIntToCString(DstType &str, int number);
70 template <typename DstType>
71 void ConvertNumberToCStringAndAppend(DstType &str, JSTaggedValue num);
72 void ConvertQuotedAndAppendToCString(const JSThread *thread, CString &str, const EcmaString *s);
73 void ConvertQuotedAndAppendToC16String(const JSThread *thread, C16String &str, const EcmaString *s);
74 #endif
75
76 CString ConvertToString(const JSThread *thread, ecmascript::JSTaggedValue key);
77
78 // append char to CString to C16String.
79 // char16_t to CString needs to be converted, which is time-consuming. So this way is not allowed.
80 template <typename DstType>
AppendString(DstType & dst,const char * src,uint32_t len)81 inline void AppendString(DstType &dst, const char *src, uint32_t len)
82 {
83 if constexpr (sizeof(typename DstType::value_type) == 1) {
84 dst.append(src, len);
85 } else {
86 for (uint32_t i = 0; i < len; ++i) {
87 dst += static_cast<char16_t>(src[i]);
88 }
89 }
90 }
91
92 template <typename DstType>
AppendString(DstType & dst,const char * src)93 inline void AppendString(DstType &dst, const char *src)
94 {
95 if constexpr (sizeof(typename DstType::value_type) == 1) {
96 dst.append(src);
97 } else {
98 while (*src != '\0') {
99 dst += static_cast<char16_t>(*src);
100 ++src;
101 }
102 }
103 }
104
105 template <typename DstType, typename SrcType>
AppendChar(DstType & dst,const SrcType src)106 inline void AppendChar(DstType &dst, const SrcType src)
107 {
108 auto c = static_cast<typename DstType::value_type>(src);
109 dst += c;
110 }
111
112 template <typename DstType, typename SrcType>
AppendCString(DstType & dst,SrcType & src)113 inline void AppendCString(DstType &dst, SrcType &src)
114 {
115 static_assert(sizeof(typename DstType::value_type) >= sizeof(typename SrcType::value_type));
116 if constexpr (sizeof(typename DstType::value_type) == 1) {
117 dst += src;
118 } else {
119 if constexpr (sizeof(typename SrcType::value_type) == 1) {
120 for (const auto &c : src) {
121 dst += static_cast<char16_t>(c);
122 }
123 } else {
124 dst += src;
125 }
126 }
127 }
128
129 template<class T>
FloatToCString(T number)130 std::enable_if_t<std::is_floating_point_v<T>, CString> FloatToCString(T number)
131 {
132 CStringStream strStream;
133 strStream << number;
134 return strStream.str();
135 }
136
137 template<class T>
ToCString(T number)138 std::enable_if_t<std::is_integral_v<T>, CString> ToCString(T number)
139 {
140 static constexpr uint32_t BUFF_SIZE = std::numeric_limits<T>::digits10 + 3; // 3: Reserved for sign bit and '\0'.
141 int64_t n = static_cast<int64_t>(number);
142 char buf[BUFF_SIZE];
143 uint32_t position = BUFF_SIZE - 1;
144 buf[position] = '\0';
145 bool isNeg = true;
146 if (n >= 0) {
147 n = -n;
148 isNeg = false;
149 }
150 do {
151 buf[--position] = static_cast<int8_t>('0' - (n % DEC_BASE));
152 n /= DEC_BASE;
153 } while (n);
154 if (isNeg) {
155 buf[--position] = '-';
156 }
157 return CString(&buf[position]);
158 }
159 } // namespace panda::ecmascript
160
161 namespace std {
162 template <>
163 struct hash<panda::ecmascript::CString> {
164 using argument_type = panda::ecmascript::CStringHash::argument_type;
165 using result_type = panda::ecmascript::CStringHash::result_type;
166
167 size_t operator()(const panda::ecmascript::CString &str) const noexcept
168 {
169 return panda::ecmascript::CStringHash()(str);
170 }
171 };
172 } // namespace std
173
174 #endif // ECMASCRIPT_MEM_C_STRING_H
175