• 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_SLICED_STRING_H
17 #define COMMON_INTERFACES_OBJECTS_STRING_SLICED_STRING_H
18 
19 #include "common_interfaces/objects/string/base_string_declare.h"
20 
21 namespace common {
22 /*
23  +-----------------------------+ <-- offset 0
24  |      BaseObject fields      |
25  +-----------------------------+
26  | LengthAndFlags (uint32_t)   |
27  +-----------------------------+
28  | RawHashcode (uint32_t)      |
29  +-----------------------------+ <-- offset = BaseString::SIZE
30  | Parent (BaseString *)       | <-- PARENT_OFFSET
31  +-----------------------------+
32  | StartIndexAndFlags (uint32_t) | <-- STARTINDEX_AND_FLAGS_OFFSET
33  +-----------------------------+ <-- SIZE
34 */
35 /*
36  +-------------------------------+
37  | StartIndexAndFlags (uint32_t) |
38  +-------------------------------+
39  Bit layout:
40    [0]         : HasBackingStoreBit         (1 bit)
41    [1]         : ReserveBit                 (1 bit)
42    [2 - 31]    : StartIndexBits             (30 bits)
43  */
44 // The substrings of another string use SlicedString to describe.
45 class SlicedString : public BaseString {
46 public:
47     BASE_CAST_CHECK(SlicedString, IsSlicedString);
48     NO_MOVE_SEMANTIC_CC(SlicedString);
49     NO_COPY_SEMANTIC_CC(SlicedString);
50     static constexpr uint32_t MIN_SLICED_STRING_LENGTH = 13;
51     static constexpr size_t PARENT_OFFSET = BaseString::SIZE;
52     static constexpr uint32_t START_INDEX_BITS_NUM = 30U;
53     using HasBackingStoreBit = BitField<bool, 0>;                                 // 1
54     using ReserveBit = HasBackingStoreBit::NextFlag;                              // 1
55     using StartIndexBits = ReserveBit::NextField<uint32_t, START_INDEX_BITS_NUM>; // 30
56     static_assert(StartIndexBits::START_BIT + StartIndexBits::SIZE == sizeof(uint32_t) * BITS_PER_BYTE,
57                   "StartIndexBits does not match the field size");
58     static_assert(StartIndexBits::SIZE == LengthBits::SIZE, "The size of startIndex should be same with Length");
59 
60     POINTER_FIELD(Parent, PARENT_OFFSET, STARTINDEX_AND_FLAGS_OFFSET);
61     PRIMITIVE_FIELD(StartIndexAndFlags, uint32_t, STARTINDEX_AND_FLAGS_OFFSET, SIZE);
62 
63     uint32_t GetStartIndex() const;
64 
65     void SetStartIndex(uint32_t startIndex);
66 
67     bool GetHasBackingStore() const;
68 
69     void SetHasBackingStore(bool hasBackingStore);
70 
71     // Minimum length for a sliced string
72     template <bool verify = true, typename ReadBarrier>
73     uint16_t Get(ReadBarrier &&readBarrier, int32_t index) const;
74 };
75 }
76 #endif //COMMON_INTERFACES_OBJECTS_STRING_SLICED_STRING_H