MiniOB 1
MiniOB is one mini database, helping developers to learn how database works.
载入中...
搜索中...
未找到
string_t.h
1/* Copyright (c) 2021 OceanBase and/or its affiliates. All rights reserved.
2miniob is licensed under Mulan PSL v2.
3You can use this software according to the terms and conditions of the Mulan PSL v2.
4You may obtain a copy of Mulan PSL v2 at:
5 http://license.coscl.org.cn/MulanPSL2
6THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
7EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
8MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
9See the Mulan PSL v2 for more details. */
10
11#pragma once
12
13#include <iostream>
14#include <cstring>
15
16using namespace std;
18{
19public:
20 static constexpr int INLINE_LENGTH = 12;
21
22 string_t() = default;
23
24 explicit string_t(uint32_t len) { value.inlined.length = len; }
25
26 string_t(const char *data, uint32_t len) { init(data, len); }
27
28 ~string_t() { reset(); }
29
30 void init(const char *data, uint32_t len)
31 {
32 value.inlined.length = len;
33 if (is_inlined()) {
34 memset(value.inlined.inlined, 0, INLINE_LENGTH);
35 if (size() == 0) {
36 return;
37 }
38 memcpy(value.inlined.inlined, data, size());
39 } else {
40 value.pointer.ptr = (char *)data;
41 }
42 }
43
44 void reset()
45 {
46 if (is_inlined()) {
47 memset(value.inlined.inlined, 0, INLINE_LENGTH);
48 } else {
49 value.pointer.ptr = nullptr;
50 }
51 value.inlined.length = 0;
52 }
53
54 string_t(const char *data) : string_t(data, strlen(data)) {}
55 string_t(const string &value) : string_t(value.c_str(), value.size()) {}
56
57 bool is_inlined() const { return size() <= INLINE_LENGTH; }
58
59 const char *data() const { return is_inlined() ? value.inlined.inlined : value.pointer.ptr; }
60
61 char *get_data_writeable() const { return is_inlined() ? (char *)value.inlined.inlined : value.pointer.ptr; }
62
63 int size() const { return value.inlined.length; }
64
65 bool empty() const { return value.inlined.length == 0; }
66
67 string get_string() const { return string(data(), size()); }
68
69 bool operator==(const string_t &r) const
70 {
71 if (this->size() != r.size()) {
72 return false;
73 }
74 return (memcmp(this->data(), r.data(), this->size()) == 0);
75 }
76
77 bool operator!=(const string_t &r) const { return !(*this == r); }
78
79 bool operator>(const string_t &r) const
80 {
81 const uint32_t left_length = this->size();
82 const uint32_t right_length = r.size();
83 const uint32_t min_length = std::min<uint32_t>(left_length, right_length);
84
85 auto memcmp_res = memcmp(this->data(), r.data(), min_length);
86 return memcmp_res > 0 || (memcmp_res == 0 && left_length > right_length);
87 }
88 bool operator<(const string_t &r) const { return r > *this; }
89
90 struct Inlined
91 {
92 uint32_t length;
93 char inlined[12];
94 };
95 union
96 {
97 struct
98 {
99 uint32_t length;
100 char *ptr;
101 } pointer;
102 Inlined inlined;
103 } value;
104};
Definition: string_t.h:91
Definition: string_t.h:18