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
38 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 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 CString ConvertToString(ecmascript::JSTaggedValue key);
63
64 template<class T>
FloatToCString(T number)65 std::enable_if_t<std::is_floating_point_v<T>, CString> FloatToCString(T number)
66 {
67 CStringStream strStream;
68 strStream << number;
69 return strStream.str();
70 }
71
72 template<class T>
ToCString(T number)73 std::enable_if_t<std::is_integral_v<T>, CString> ToCString(T number)
74 {
75 static constexpr uint32_t BUFF_SIZE = std::numeric_limits<T>::digits10 + 3; // 3: Reserved for sign bit and '\0'.
76 int64_t n = static_cast<int64_t>(number);
77 char buf[BUFF_SIZE];
78 uint32_t position = BUFF_SIZE - 1;
79 buf[position] = '\0';
80 bool isNeg = true;
81 if (n >= 0) {
82 n = -n;
83 isNeg = false;
84 }
85 do {
86 buf[--position] = static_cast<int8_t>('0' - (n % 10)); // 10 : decimal
87 n /= 10; // 10 : decimal
88 } while (n);
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