• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "ecmascript/mem/c_string.h"
17 
18 
19 #include "ecmascript/ecma_string-inl.h"
20 #include "ecmascript/js_symbol.h"
21 
22 namespace panda::ecmascript {
CStringToL(const CString & str)23 long CStringToL(const CString &str)
24 {
25     char *endPtr = nullptr;
26     int64_t result = std::strtol(str.c_str(), &endPtr, DEC_BASE);
27     ASSERT(!(result == 0 && str.c_str() == endPtr) && "CString argument is not long int");
28     return result;
29 }
30 
CStringToLL(const CString & str)31 int64_t CStringToLL(const CString &str)
32 {
33     char *endPtr = nullptr;
34     int64_t result = std::strtoll(str.c_str(), &endPtr, DEC_BASE);
35     ASSERT(!(result == 0 && str.c_str() == endPtr) && "CString argument is not long long int");
36     return result;
37 }
38 
CStringToULL(const CString & str)39 uint64_t CStringToULL(const CString &str)
40 {
41     char *endPtr = nullptr;
42     uint64_t result = std::strtoull(str.c_str(), &endPtr, DEC_BASE);
43     ASSERT(!(result == 0 && str.c_str() == endPtr) && "CString argument is not unsigned long long int");
44     return result;
45 }
46 
CStringToF(const CString & str)47 float CStringToF(const CString &str)
48 {
49     char *endPtr = nullptr;
50     float result = std::strtof(str.c_str(), &endPtr);
51     ASSERT(result != HUGE_VALF && "CString argument is not float");
52     ASSERT(!(result == 0 && str.c_str() == endPtr) && "CString argument is not float");
53     return result;
54 }
55 
CStringToD(const CString & str)56 double CStringToD(const CString &str)
57 {
58     char *endPtr = nullptr;
59     double result = std::strtod(str.c_str(), &endPtr);
60     ASSERT(result != HUGE_VALF && "CString argument is not double");
61     ASSERT(!(result == 0 && str.c_str() == endPtr) && "CString argument is not double");
62     return result;
63 }
64 
65 template<class T>
ConvertToString(T sp)66 CString ConvertToString(T sp)
67 {
68     CString res;
69     res.reserve(sp.size());
70 
71     // Also support ascii that great than 127, so using unsigned char here
72     constexpr size_t maxChar = std::numeric_limits<unsigned char>::max();
73 
74     for (const auto &c : sp) {
75         if (c > maxChar) {
76             return "";
77         }
78         res.push_back(c);
79     }
80 
81     return res;
82 }
83 
84 // NB! the following function need additional mem allocation, don't use when unnecessary!
ConvertToString(const std::string & str)85 CString ConvertToString(const std::string &str)
86 {
87     CString res;
88     res.reserve(str.size());
89     for (auto c : str) {
90         res.push_back(c);
91     }
92     return res;
93 }
94 
ConvertToString(const EcmaString * s,StringConvertedUsage usage,bool cesu8)95 CString ConvertToString(const EcmaString *s, StringConvertedUsage usage, bool cesu8)
96 {
97     if (s == nullptr) {
98         return CString("");
99     }
100     return EcmaStringAccessor(const_cast<EcmaString *>(s)).ToCString(usage, cesu8);
101 }
102 
ConvertAndAppendToString(CString & str,const EcmaString * s,StringConvertedUsage usage,bool cesu8)103 void ConvertAndAppendToString(CString &str, const EcmaString *s, StringConvertedUsage usage, bool cesu8)
104 {
105     if (s == nullptr) {
106         return;
107     }
108     EcmaStringAccessor(const_cast<EcmaString *>(s)).AppendToCString(str, usage, cesu8);
109 }
110 
ConvertQuotedAndAppendToString(CString & str,const EcmaString * s,StringConvertedUsage usage,bool cesu8)111 void ConvertQuotedAndAppendToString(CString &str, const EcmaString *s, StringConvertedUsage usage, bool cesu8)
112 {
113     if (s == nullptr) {
114         str += "\"\"";
115         return;
116     }
117     EcmaStringAccessor(const_cast<EcmaString *>(s)).AppendQuotedStringToCString(str, usage, cesu8);
118 }
119 
ConvertToString(JSTaggedValue key)120 CString ConvertToString(JSTaggedValue key)
121 {
122     ASSERT(key.IsStringOrSymbol());
123     if (key.IsString()) {
124         return ConvertToString(EcmaString::ConstCast(key.GetTaggedObject()));
125     }
126 
127     ecmascript::JSTaggedValue desc = JSSymbol::Cast(key.GetTaggedObject())->GetDescription();
128     if (desc.IsUndefined()) {
129         return CString("Symbol()");
130     }
131 
132     return ConvertToString(EcmaString::ConstCast(desc.GetTaggedObject()));
133 }
134 
ConvertToStdString(const CString & str)135 std::string ConvertToStdString(const CString &str)
136 {
137     std::string res;
138     res.reserve(str.size());
139     for (auto c : str) {
140         res.push_back(c);
141     }
142     return res;
143 }
144 }  // namespace panda::ecmascript
145