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_TREE_STRING_INL_H
17 #define COMMON_INTERFACES_OBJECTS_STRING_TREE_STRING_INL_H
18
19 #include "common_interfaces/objects/string/base_string_declare.h"
20 #include "common_interfaces/objects/string/tree_string.h"
21
22 namespace common {
23 template <typename ReadBarrier>
IsFlat(ReadBarrier && readBarrier)24 bool TreeString::IsFlat(ReadBarrier &&readBarrier) const
25 {
26 auto strSecond = BaseString::Cast(GetSecond<BaseObject *>(std::forward<ReadBarrier>(readBarrier)));
27 return strSecond->GetLength() == 0;
28 }
29
30 template <bool verify, typename ReadBarrier>
Get(ReadBarrier && readBarrier,int32_t index)31 uint16_t TreeString::Get(ReadBarrier &&readBarrier, int32_t index) const
32 {
33 int32_t length = static_cast<int32_t>(GetLength());
34 if (verify) {
35 if ((index < 0) || (index >= length)) {
36 return 0;
37 }
38 }
39
40 if (IsFlat(std::forward<ReadBarrier>(readBarrier))) {
41 BaseString *first = BaseString::Cast(GetFirst<BaseObject *>(std::forward<ReadBarrier>(readBarrier)));
42 return first->At<verify>(std::forward<ReadBarrier>(readBarrier), index);
43 }
44 BaseString *string = const_cast<TreeString *>(this);
45 while (true) {
46 if (string->IsTreeString()) {
47 BaseString *first = BaseString::Cast(
48 TreeString::Cast(string)->GetFirst<BaseObject *>(std::forward<ReadBarrier>(readBarrier)));
49 if (static_cast<int32_t>(first->GetLength()) > index) {
50 string = first;
51 } else {
52 index -= static_cast<int32_t>(first->GetLength());
53 string = BaseString::Cast(
54 TreeString::Cast(string)->GetSecond<BaseObject *>(std::forward<ReadBarrier>(readBarrier)));
55 }
56 } else {
57 return string->At<verify>(std::forward<ReadBarrier>(readBarrier), index);
58 }
59 }
60 UNREACHABLE();
61 }
62 }
63 #endif //COMMON_INTERFACES_OBJECTS_STRING_TREE_STRING_INL_H