fix string trim

Description
  fix string trim of empty string.
Issue:
  #I5B6S0:fix string trim

Signed-off-by: xliu <liuxin259@huawei.com>
Change-Id: Ia1b6274252536ecc8ffd62d2459f8ab6f5ba8335
This commit is contained in:
xliu 2022-06-07 19:58:20 +08:00
parent 267f0a20ac
commit 930d433784
4 changed files with 22 additions and 20 deletions

View File

@ -1543,14 +1543,14 @@ JSTaggedValue BuiltinsString::Trim(EcmaRuntimeCallInfo *argv)
Span<const uint8_t> data(reinterpret_cast<const uint8_t *>(thisHandle->GetData()), thisLen);
uint32_t start = base::StringHelper::GetStart(data, thisLen);
uint32_t end = base::StringHelper::GetEnd(data, start, thisLen);
EcmaString *res = EcmaString::FastSubUtf8String(thread->GetEcmaVM(), thisHandle, start, end - start + 1);
EcmaString *res = EcmaString::FastSubUtf8String(thread->GetEcmaVM(), thisHandle, start, end + 1 - start);
return JSTaggedValue(res);
}
Span<const uint16_t> data(thisHandle->GetData(), thisLen);
uint32_t start = base::StringHelper::GetStart(data, thisLen);
uint32_t end = base::StringHelper::GetEnd(data, start, thisLen);
EcmaString *res = EcmaString::FastSubUtf16String(thread->GetEcmaVM(), thisHandle, start, end - start + 1);
EcmaString *res = EcmaString::FastSubUtf16String(thread->GetEcmaVM(), thisHandle, start, end + 1 - start);
return JSTaggedValue(res);
}

View File

@ -192,13 +192,16 @@ void EcmaString::WriteData(char src, uint32_t start)
/* static */
EcmaString *EcmaString::FastSubUtf8String(const EcmaVM *vm, const JSHandle<EcmaString> &src, uint32_t start,
uint32_t utf16Len)
uint32_t length)
{
auto string = AllocStringObject(utf16Len, true, vm);
if (length == 0) {
return *vm->GetFactory()->GetEmptyString();
}
auto string = AllocStringObject(length, true, vm);
// NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
Span<uint8_t> dst(string->GetDataUtf8Writable(), utf16Len);
Span<const uint8_t> source(src->GetDataUtf8() + start, utf16Len);
EcmaString::StringCopy(dst, utf16Len, source, utf16Len);
Span<uint8_t> dst(string->GetDataUtf8Writable(), length);
Span<const uint8_t> source(src->GetDataUtf8() + start, length);
EcmaString::StringCopy(dst, length, source, length);
ASSERT_PRINT(CanBeCompressed(string), "canBeCompresse does not match the real value!");
return string;
@ -206,18 +209,21 @@ EcmaString *EcmaString::FastSubUtf8String(const EcmaVM *vm, const JSHandle<EcmaS
/* static */
EcmaString *EcmaString::FastSubUtf16String(const EcmaVM *vm, const JSHandle<EcmaString> &src, uint32_t start,
uint32_t utf16Len)
uint32_t length)
{
bool canBeCompressed = CanBeCompressed(src->GetDataUtf16() + start, utf16Len);
auto string = AllocStringObject(utf16Len, canBeCompressed, vm);
if (length == 0) {
return *vm->GetFactory()->GetEmptyString();
}
bool canBeCompressed = CanBeCompressed(src->GetDataUtf16() + start, length);
auto string = AllocStringObject(length, canBeCompressed, vm);
if (canBeCompressed) {
// NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
CopyUtf16AsUtf8(src->GetDataUtf16() + start, string->GetDataUtf8Writable(), utf16Len);
CopyUtf16AsUtf8(src->GetDataUtf16() + start, string->GetDataUtf8Writable(), length);
} else {
uint32_t len = utf16Len * (sizeof(uint16_t) / sizeof(uint8_t));
uint32_t len = length * (sizeof(uint16_t) / sizeof(uint8_t));
// NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
Span<uint16_t> dst(string->GetDataUtf16Writable(), utf16Len);
Span<const uint16_t> source(src->GetDataUtf16() + start, utf16Len);
Span<uint16_t> dst(string->GetDataUtf16Writable(), length);
Span<const uint16_t> source(src->GetDataUtf16() + start, length);
EcmaString::StringCopy(dst, len, source, len);
}
ASSERT_PRINT(canBeCompressed == CanBeCompressed(string), "canBeCompresse does not match the real value!");

View File

@ -80,10 +80,6 @@ EcmaString *EcmaString::Concat(const JSHandle<EcmaString> &str1Handle, const JSH
EcmaString *EcmaString::FastSubString(const JSHandle<EcmaString> &src, uint32_t start, uint32_t utf16Len,
const EcmaVM *vm)
{
if (utf16Len == 0) {
return vm->GetFactory()->GetEmptyString().GetObject<EcmaString>();
}
if (src->IsUtf8()) {
return FastSubUtf8String(vm, src, start, utf16Len);
}

View File

@ -315,9 +315,9 @@ public:
static constexpr size_t DATA_OFFSET = SIZE; // DATA_OFFSET equal to Empty String size
static inline EcmaString *FastSubUtf8String(const EcmaVM *vm, const JSHandle<EcmaString> &src, uint32_t start,
uint32_t utf16Len);
uint32_t length);
static inline EcmaString *FastSubUtf16String(const EcmaVM *vm, const JSHandle<EcmaString> &src, uint32_t start,
uint32_t utf16Len);
uint32_t length);
private:
void SetLength(uint32_t length, bool compressed = false)
{