• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * Copyright (c) 2025 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 #ifndef COMMON_INTERFACES_OBJECTS_STRING_LINE_STRING_INL_H
17 #define COMMON_INTERFACES_OBJECTS_STRING_LINE_STRING_INL_H
18 
19 #include "common_components/base/globals.h"
20 #include "common_components/common_runtime/hooks.h"
21 #include "common_interfaces/objects/string/base_string_declare.h"
22 #include "common_interfaces/objects/string/line_string.h"
23 
24 namespace common {
ComputeSizeUtf8(uint32_t utf8Len)25 inline size_t LineString::ComputeSizeUtf8(uint32_t utf8Len)
26 {
27     return DATA_OFFSET + utf8Len;
28 }
29 
ComputeSizeUtf16(uint32_t utf16Len)30 inline size_t LineString::ComputeSizeUtf16(uint32_t utf16Len)
31 {
32     return DATA_OFFSET + utf16Len * sizeof(uint16_t);
33 }
34 
ObjectSize(BaseString * str)35 inline size_t LineString::ObjectSize(BaseString *str)
36 {
37     uint32_t length = str->GetLength();
38     return str->IsUtf16() ? ComputeSizeUtf16(length) : ComputeSizeUtf8(length);
39 }
40 
DataSize(BaseString * str)41 inline size_t LineString::DataSize(BaseString *str)
42 {
43     uint32_t length = str->GetLength();
44     return str->IsUtf16() ? length * sizeof(uint16_t) : length;
45 }
46 
47 template <bool verify>
Get(int32_t index)48 uint16_t LineString::Get(int32_t index) const
49 {
50     int32_t length = static_cast<int32_t>(GetLength());
51     if (verify) {
52         if ((index < 0) || (index >= length)) {
53             return 0;
54         }
55     }
56     if (!IsUtf16()) {
57         Span<const uint8_t> sp(GetDataUtf8(), length);
58         return sp[index];
59     }
60     Span<const uint16_t> sp(GetDataUtf16(), length);
61     return sp[index];
62 }
63 
Set(uint32_t index,uint16_t src)64 inline void LineString::Set(uint32_t index, uint16_t src)
65 {
66     DCHECK_CC(index < GetLength());
67     if (IsUtf8()) {
68         // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
69         *(reinterpret_cast<uint8_t *>(GetData()) + index) = static_cast<uint8_t>(src);
70     } else {
71         // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
72         *(GetData() + index) = src;
73     }
74 }
75 
Trim(uint32_t newLength)76 inline void LineString::Trim(uint32_t newLength)
77 {
78     ASSERT(IsLineString());
79     uint32_t oldLength = GetLength();
80     ASSERT(oldLength >= newLength);
81     if (newLength == oldLength) return;
82     bool isUtf8 = IsUtf8();
83     size_t sizeNew = isUtf8 ?
84         LineString::ComputeSizeUtf8(newLength): LineString::ComputeSizeUtf16(newLength);
85     size_t sizeOld = isUtf8 ?
86         LineString::ComputeSizeUtf8(oldLength): LineString::ComputeSizeUtf16(oldLength);
87     sizeNew = common::AlignUp(sizeNew, ALIGNMENT_8_BYTES);
88     sizeOld = common::AlignUp(sizeOld, ALIGNMENT_8_BYTES);
89     size_t trimBytes = sizeOld - sizeNew;
90     if (trimBytes > 0) {
91         uintptr_t newEndAddr = ToUintPtr(this) + sizeNew;
92         ASSERT_PRINT((newEndAddr % ALIGNMENT_8_BYTES) == 0, "Alignment failed");
93         FillFreeObject(reinterpret_cast<void*>(newEndAddr), trimBytes);
94     }
95     InitLengthAndFlags(newLength, isUtf8);
96 }
97 }
98 #endif //COMMON_INTERFACES_OBJECTS_STRING_LINE_STRING_INL_H