Files
ark_js_runtime/ecmascript/mem/c_string.cpp
T
wengchangcheng 0b4981525d refactor stl container
1. using runtime container instead of stl container
2. change debugger line amd column type to int32_t

issue: https://gitee.com/openharmony/ark_js_runtime/issues/I50NHW

Signed-off-by: wengchangcheng <wengchangcheng@huawei.com>
Change-Id: I0eb4651f17c6f6894f11de1ba904bcbe83a57db7
2022-04-06 16:05:22 +08:00

143 lines
4.3 KiB
C++

/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "ecmascript/mem/c_string.h"
#include <cmath>
#include <cstdlib>
#include "ecmascript/ecma_string-inl.h"
#include "ecmascript/js_symbol.h"
#include "ecmascript/mem/c_containers.h"
#include "libpandabase/macros.h"
namespace panda::ecmascript {
long CStringToL(const CString &str)
{
[[maybe_unused]] char *endPtr = nullptr;
int64_t result = std::strtol(str.c_str(), &endPtr, BASE);
ASSERT(!(result == 0 && str.c_str() == endPtr) && "CString argument is not long int");
return result;
}
int64_t CStringToLL(const CString &str)
{
[[maybe_unused]] char *endPtr = nullptr;
int64_t result = std::strtoll(str.c_str(), &endPtr, BASE);
ASSERT(!(result == 0 && str.c_str() == endPtr) && "CString argument is not long long int");
return result;
}
uint64_t CStringToULL(const CString &str)
{
[[maybe_unused]] char *endPtr = nullptr;
uint64_t result = std::strtoull(str.c_str(), &endPtr, BASE);
ASSERT(!(result == 0 && str.c_str() == endPtr) && "CString argument is not unsigned long long int");
return result;
}
float CStringToF(const CString &str)
{
[[maybe_unused]] char *endPtr = nullptr;
float result = std::strtof(str.c_str(), &endPtr);
ASSERT(result != HUGE_VALF && "CString argument is not float");
ASSERT(!(result == 0 && str.c_str() == endPtr) && "CString argument is not float");
return result;
}
double CStringToD(const CString &str)
{
[[maybe_unused]] char *endPtr = nullptr;
double result = std::strtod(str.c_str(), &endPtr);
ASSERT(result != HUGE_VALF && "CString argument is not double");
ASSERT(!(result == 0 && str.c_str() == endPtr) && "CString argument is not double");
return result;
}
template<class T>
CString ConvertToString(T sp)
{
CString res;
res.reserve(sp.size());
// Also support ascii that great than 127, so using unsigned char here
constexpr size_t maxChar = std::numeric_limits<unsigned char>::max();
for (const auto &c : sp) {
if (c > maxChar) {
return "";
}
res.push_back(c);
}
return res;
}
// NB! the following function need additional mem allocation, don't use when unnecessary!
CString ConvertToString(const std::string &str)
{
CString res;
res.reserve(str.size());
for (auto c : str) {
res.push_back(c);
}
return res;
}
CString ConvertToString(const EcmaString *s, StringConvertedUsage usage)
{
if (s == nullptr) {
return CString("");
}
if (s->IsUtf16()) {
// Should convert utf-16 to utf-8, because uint16_t likely great than maxChar, will convert fail
bool modify = (usage != StringConvertedUsage::PRINT);
size_t len = base::utf_helper::Utf16ToUtf8Size(s->GetDataUtf16(), s->GetLength(), modify) - 1;
CVector<uint8_t> buf(len);
len = base::utf_helper::ConvertRegionUtf16ToUtf8(s->GetDataUtf16(), buf.data(), s->GetLength(), len, 0, modify);
Span<const uint8_t> sp(buf.data(), len);
return ConvertToString(sp);
}
Span<const uint8_t> sp(s->GetDataUtf8(), s->GetLength());
return ConvertToString(sp);
}
CString ConvertToString(JSTaggedValue key)
{
ASSERT(key.IsStringOrSymbol());
if (key.IsString()) {
return ConvertToString(EcmaString::ConstCast(key.GetTaggedObject()));
}
ecmascript::JSTaggedValue desc = JSSymbol::Cast(key.GetTaggedObject())->GetDescription();
if (desc.IsUndefined()) {
return CString("Symbol()");
}
return ConvertToString(EcmaString::ConstCast(desc.GetTaggedObject()));
}
std::string CstringConvertToStdString(const CString &str)
{
std::string res;
res.reserve(str.size());
for (auto c : str) {
res.push_back(c);
}
return res;
}
} // namespace panda::ecmascript