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 CStringStream = std::basic_stringstream<char, std::char_traits<char>, CAddressAllocator<char>>;
32
33 struct CStringHash {
34 using argument_type = panda::ecmascript::CString;
35 using result_type = std::size_t;
36
operatorCStringHash37 size_t operator()(const CString &str) const noexcept
38 {
39 return std::hash<std::string_view>()(std::string_view(str.data(), str.size()));
40 }
41 };
42
43 constexpr int BASE = 10;
44
45 // PRINT will skip '\0' in utf16 during conversion of utf8
46 enum StringConvertedUsage { PRINT, LOGICOPERATION };
47
48 long CStringToL(const CString &str);
49 int64_t CStringToLL(const CString &str);
50 uint64_t CStringToULL(const CString &str);
51 float CStringToF(const CString &str);
52 double CStringToD(const CString &str);
53
54 CString ConvertToString(const std::string &str);
55 std::string CstringConvertToStdString(const CString &str);
56
57 // '\u0000' is skip according to holdZero
58 CString ConvertToString(const ecmascript::EcmaString *s, StringConvertedUsage usage = StringConvertedUsage::PRINT);
59 CString ConvertToString(ecmascript::JSTaggedValue key);
60
61 template<class T>
FloatToCString(T number)62 std::enable_if_t<std::is_floating_point_v<T>, CString> FloatToCString(T number)
63 {
64 CStringStream strStream;
65 strStream << number;
66 return strStream.str();
67 }
68
69 template<class T>
ToCString(T number)70 std::enable_if_t<std::is_integral_v<T>, CString> ToCString(T number)
71 {
72 if (number == 0) {
73 return CString("0");
74 }
75 static constexpr uint32_t BUFF_SIZE = std::numeric_limits<T>::digits10 + 3; // 3: Reserved for sign bit and '\0'.
76 char buf[BUFF_SIZE];
77 uint32_t position = BUFF_SIZE - 1;
78 buf[position] = '\0';
79 bool IsNeg = number < 0;
80 while (number != 0) {
81 if (IsNeg) {
82 buf[--position] = static_cast<int8_t>('0' - (number % 10)); // 10 : decimal
83 } else {
84 buf[--position] = static_cast<int8_t>('0' + (number % 10)); // 10 : decimal
85 }
86 number /= 10; // 10 : decimal
87 }
88 if (IsNeg) {
89 buf[--position] = '-';
90 }
91 return CString(&buf[position]);
92 }
93 } // namespace panda::ecmascript
94
95 namespace std {
96 template <>
97 struct hash<panda::ecmascript::CString> {
98 using argument_type = panda::ecmascript::CStringHash::argument_type;
99 using result_type = panda::ecmascript::CStringHash::result_type;
100
101 size_t operator()(const panda::ecmascript::CString &str) const noexcept
102 {
103 return panda::ecmascript::CStringHash()(str);
104 }
105 };
106 } // namespace std
107
108 #endif // ECMASCRIPT_MEM_C_STRING_H
109