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 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 ConvertToStdString(const CString &str);
57
58 // '\u0000' is skip according to holdZero
59 CString ConvertToString(const ecmascript::EcmaString *s, StringConvertedUsage usage = StringConvertedUsage::PRINT);
60 CString ConvertToString(ecmascript::JSTaggedValue key);
61
62 template<class T>
FloatToCString(T number)63 std::enable_if_t<std::is_floating_point_v<T>, CString> FloatToCString(T number)
64 {
65 CStringStream strStream;
66 strStream << number;
67 return strStream.str();
68 }
69
70 template<class T>
ToCString(T number)71 std::enable_if_t<std::is_integral_v<T>, CString> ToCString(T number)
72 {
73 if (number == 0) {
74 return CString("0");
75 }
76 static constexpr uint32_t BUFF_SIZE = std::numeric_limits<T>::digits10 + 3; // 3: Reserved for sign bit and '\0'.
77 char buf[BUFF_SIZE];
78 uint32_t position = BUFF_SIZE - 1;
79 buf[position] = '\0';
80 bool isNeg = number < 0;
81 while (number != 0) {
82 if (isNeg) {
83 buf[--position] = static_cast<int8_t>('0' - (number % 10)); // 10 : decimal
84 } else {
85 buf[--position] = static_cast<int8_t>('0' + (number % 10)); // 10 : decimal
86 }
87 number /= 10; // 10 : decimal
88 }
89 if (isNeg) {
90 buf[--position] = '-';
91 }
92 return CString(&buf[position]);
93 }
94 } // namespace panda::ecmascript
95
96 namespace std {
97 template <>
98 struct hash<panda::ecmascript::CString> {
99 using argument_type = panda::ecmascript::CStringHash::argument_type;
100 using result_type = panda::ecmascript::CStringHash::result_type;
101
102 size_t operator()(const panda::ecmascript::CString &str) const noexcept
103 {
104 return panda::ecmascript::CStringHash()(str);
105 }
106 };
107 } // namespace std
108
109 #endif // ECMASCRIPT_MEM_C_STRING_H
110