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