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 "runtime/include/mem/panda_string.h"
17
18 #include <cmath>
19 #include <cstdlib>
20
21 #include "libpandabase/macros.h"
22 #include "runtime/include/coretypes/string.h"
23
24 namespace panda {
25
26 static constexpr int BASE = 10;
27
PandaStringToLL(const PandaString & str)28 int64_t PandaStringToLL(const PandaString &str)
29 {
30 [[maybe_unused]] char *end_ptr = nullptr;
31 int64_t result = std::strtoll(str.c_str(), &end_ptr, BASE);
32 ASSERT(!(result == 0 && str.c_str() == end_ptr) && "PandaString argument is not long long int");
33 return result;
34 }
35
PandaStringToULL(const PandaString & str)36 uint64_t PandaStringToULL(const PandaString &str)
37 {
38 [[maybe_unused]] char *end_ptr = nullptr;
39 uint64_t result = std::strtoull(str.c_str(), &end_ptr, BASE);
40 ASSERT(!(result == 0 && str.c_str() == end_ptr) && "PandaString argument is not unsigned long long int");
41 return result;
42 }
43
PandaStringToF(const PandaString & str)44 float PandaStringToF(const PandaString &str)
45 {
46 [[maybe_unused]] char *end_ptr = nullptr;
47 float result = std::strtof(str.c_str(), &end_ptr);
48 ASSERT(result != HUGE_VALF && "PandaString argument is not float");
49 ASSERT(!(result == 0 && str.c_str() == end_ptr) && "PandaString argument is not float");
50 return result;
51 }
52
PandaStringToD(const PandaString & str)53 double PandaStringToD(const PandaString &str)
54 {
55 [[maybe_unused]] char *end_ptr = nullptr;
56 double result = std::strtod(str.c_str(), &end_ptr);
57 ASSERT(result != HUGE_VALF && "PandaString argument is not double");
58 ASSERT(!(result == 0 && str.c_str() == end_ptr) && "PandaString argument is not double");
59 return result;
60 }
61
62 template <class T>
ConvertToString(T sp)63 PandaString ConvertToString(T sp)
64 {
65 PandaString res;
66 res.reserve(sp.size());
67
68 // Also support ascii that great than 127, so using unsigned char here
69 constexpr size_t MAX_CHAR = std::numeric_limits<unsigned char>::max();
70
71 for (auto c : sp) {
72 if (c > MAX_CHAR) {
73 return "";
74 }
75 res.push_back(c);
76 }
77
78 return res;
79 }
80
81 // NB! the following function need additional mem allocation, donnot use when unnecessary!
ConvertToString(const std::string & str)82 PandaString ConvertToString(const std::string &str)
83 {
84 PandaString res;
85 res.reserve(str.size());
86 for (auto c : str) {
87 res.push_back(c);
88 }
89 return res;
90 }
91
ConvertToString(coretypes::String * s)92 PandaString ConvertToString(coretypes::String *s)
93 {
94 ASSERT(s != nullptr);
95 if (s->IsUtf16()) {
96 // Should convert utf-16 to utf-8, because uint16_t likely greater than MAX_CHAR, will convert fail
97 size_t len = utf::Utf16ToMUtf8Size(s->GetDataUtf16(), s->GetUtf16Length()) - 1;
98 PandaVector<uint8_t> buf(len);
99 len = utf::ConvertRegionUtf16ToMUtf8(s->GetDataUtf16(), buf.data(), s->GetUtf16Length(), len, 0);
100 Span<const uint8_t> sp(buf.data(), len);
101 return ConvertToString(sp);
102 }
103
104 Span<const uint8_t> sp(s->GetDataMUtf8(), s->GetLength());
105 return ConvertToString(sp);
106 }
107
108 } // namespace panda
109