1 /**
2 * Copyright (c) 2021-2022 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
ConvertToString(Span<const uint8_t> sp)62 PandaString ConvertToString(Span<const uint8_t> sp)
63 {
64 PandaString res;
65 res.reserve(sp.size());
66
67 for (auto c : sp) {
68 res.push_back(c);
69 }
70
71 return res;
72 }
73
74 // NB! the following function need additional mem allocation, donnot use when unnecessary!
ConvertToString(const std::string & str)75 PandaString ConvertToString(const std::string &str)
76 {
77 PandaString res;
78 res.reserve(str.size());
79 for (auto c : str) {
80 res.push_back(c);
81 }
82 return res;
83 }
84
ConvertToString(coretypes::String * s)85 PandaString ConvertToString(coretypes::String *s)
86 {
87 ASSERT(s != nullptr);
88 if (s->IsUtf16()) {
89 // Should convert utf-16 to utf-8, because uint16_t likely great than MAX_CHAR, will convert fail
90 size_t len = utf::Utf16ToMUtf8Size(s->GetDataUtf16(), s->GetUtf16Length()) - 1;
91 PandaVector<uint8_t> buf(len);
92 len = utf::ConvertRegionUtf16ToMUtf8(s->GetDataUtf16(), buf.data(), s->GetUtf16Length(), len, 0);
93 Span<const uint8_t> sp(buf.data(), len);
94 return ConvertToString(sp);
95 }
96
97 Span<const uint8_t> sp(s->GetDataMUtf8(), s->GetLength());
98 return ConvertToString(sp);
99 }
100
101 } // namespace panda
102