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_SLICED_STRING_INL_H
17 #define COMMON_INTERFACES_OBJECTS_STRING_SLICED_STRING_INL_H
18
19 #include "common_interfaces/objects/string/base_string_declare.h"
20 #include "common_interfaces/objects/string/sliced_string.h"
21
22 namespace common {
GetStartIndex()23 inline uint32_t SlicedString::GetStartIndex() const
24 {
25 uint32_t bits = GetStartIndexAndFlags();
26 return StartIndexBits::Decode(bits);
27 }
28
SetStartIndex(uint32_t startIndex)29 inline void SlicedString::SetStartIndex(uint32_t startIndex)
30 {
31 DCHECK_CC(startIndex <= SlicedString::MAX_STRING_LENGTH);
32 uint32_t bits = GetStartIndexAndFlags();
33 uint32_t newVal = StartIndexBits::Update(bits, startIndex);
34 SetStartIndexAndFlags(newVal);
35 }
36
GetHasBackingStore()37 inline bool SlicedString::GetHasBackingStore() const
38 {
39 uint32_t bits = GetStartIndexAndFlags();
40 return HasBackingStoreBit::Decode(bits);
41 }
42
SetHasBackingStore(bool hasBackingStore)43 inline void SlicedString::SetHasBackingStore(bool hasBackingStore)
44 {
45 uint32_t bits = GetStartIndexAndFlags();
46 uint32_t newVal = HasBackingStoreBit::Update(bits, hasBackingStore);
47 SetStartIndexAndFlags(newVal);
48 }
49
50 // Minimum length for a sliced string
51 template <bool verify, typename ReadBarrier>
Get(ReadBarrier && readBarrier,int32_t index)52 uint16_t SlicedString::Get(ReadBarrier &&readBarrier, int32_t index) const
53 {
54 int32_t length = static_cast<int32_t>(GetLength());
55 if (verify) {
56 if ((index < 0) || (index >= length)) {
57 return 0;
58 }
59 }
60 BaseString *parent = BaseString::Cast(GetParent<BaseObject *>(std::forward<ReadBarrier>(readBarrier)));
61 DCHECK_CC(parent->IsLineString());
62 if (parent->IsUtf8()) {
63 Span<const uint8_t> sp(parent->GetDataUtf8() + GetStartIndex(), length);
64 return sp[index];
65 }
66 Span<const uint16_t> sp(parent->GetDataUtf16() + GetStartIndex(), length);
67 return sp[index];
68 }
69 }
70 #endif //COMMON_INTERFACES_OBJECTS_STRING_SLICED_STRING_INL_H