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 CS16tring = 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 ecmascript::EcmaString *s,
61 StringConvertedUsage usage = StringConvertedUsage::PRINT, bool cesu8 = false);
62
63 void ConvertAndAppendToString(CString &str, const ecmascript::EcmaString *s,
64 StringConvertedUsage usage = StringConvertedUsage::PRINT, bool cesu8 = false);
65
66 void ConvertQuotedAndAppendToString(CString &str, const ecmascript::EcmaString *s,
67 StringConvertedUsage usage = StringConvertedUsage::PRINT, bool cesu8 = false);
68 CString ConvertToString(ecmascript::JSTaggedValue key);
69
70 template<class T>
FloatToCString(T number)71 std::enable_if_t<std::is_floating_point_v<T>, CString> FloatToCString(T number)
72 {
73 CStringStream strStream;
74 strStream << number;
75 return strStream.str();
76 }
77
78 template<class T>
ToCString(T number)79 std::enable_if_t<std::is_integral_v<T>, CString> ToCString(T number)
80 {
81 static constexpr uint32_t BUFF_SIZE = std::numeric_limits<T>::digits10 + 3; // 3: Reserved for sign bit and '\0'.
82 int64_t n = static_cast<int64_t>(number);
83 char buf[BUFF_SIZE];
84 uint32_t position = BUFF_SIZE - 1;
85 buf[position] = '\0';
86 bool isNeg = true;
87 if (n >= 0) {
88 n = -n;
89 isNeg = false;
90 }
91 do {
92 buf[--position] = static_cast<int8_t>('0' - (n % DEC_BASE));
93 n /= DEC_BASE;
94 } while (n);
95 if (isNeg) {
96 buf[--position] = '-';
97 }
98 return CString(&buf[position]);
99 }
100
101 template<class T>
AppendToCString(CString & str,T number)102 void AppendToCString(CString &str, T number)
103 {
104 static_assert(std::is_integral_v<T>);
105 int64_t n = static_cast<int64_t>(number);
106 uint32_t preSize = str.size();
107 bool isNeg = true;
108 if (n >= 0) {
109 n = -n;
110 isNeg = false;
111 }
112 do {
113 str.push_back(static_cast<int8_t>('0' - (n % DEC_BASE)));
114 n /= DEC_BASE;
115 } while (n);
116 if (isNeg) {
117 str.push_back('-');
118 }
119 std::reverse(str.begin() + preSize, str.end());
120 }
121 } // namespace panda::ecmascript
122
123 namespace std {
124 template <>
125 struct hash<panda::ecmascript::CString> {
126 using argument_type = panda::ecmascript::CStringHash::argument_type;
127 using result_type = panda::ecmascript::CStringHash::result_type;
128
129 size_t operator()(const panda::ecmascript::CString &str) const noexcept
130 {
131 return panda::ecmascript::CStringHash()(str);
132 }
133 };
134 } // namespace std
135
136 #endif // ECMASCRIPT_MEM_C_STRING_H
137