be/src/core/string_view.cpp
Line | Count | Source |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | |
18 | | #include "core/string_view.h" |
19 | | |
20 | | namespace doris { |
21 | | |
22 | 8 | bool StringView::operator==(const StringView& other) const { |
23 | | // Compare lengths and first 4 characters. |
24 | 8 | if (size_and_prefix_as_int64() != other.size_and_prefix_as_int64()) { |
25 | 1 | return false; |
26 | 1 | } |
27 | 7 | if (isInline()) { |
28 | | // The inline part is zeroed at construction, so we can compare |
29 | | // a word at a time if data extends past 'prefix_'. |
30 | 3 | return size_ <= kPrefixSize || inlined_as_int64() == other.inlined_as_int64(); |
31 | 3 | } |
32 | | // Sizes are equal and this is not inline, therefore both are out |
33 | | // of line and have kPrefixSize first in common. |
34 | 4 | return memcmp(value_.data + kPrefixSize, other.value_.data + kPrefixSize, |
35 | 4 | size_ - kPrefixSize) == 0; |
36 | 7 | } |
37 | 1.23k | int32_t StringView::compare(const StringView& other) const { |
38 | 1.23k | if (prefix_as_int() != other.prefix_as_int()) { |
39 | | // The result is decided on prefix. The shorter will be less because the |
40 | | // prefix is padded with zeros. |
41 | 45 | return memcmp(prefix_, other.prefix_, kPrefixSize); |
42 | 45 | } |
43 | 1.19k | int32_t size = std::min(size_, other.size_) - kPrefixSize; |
44 | 1.19k | if (size <= 0) { |
45 | | // One ends within the prefix. |
46 | 955 | return size_ - other.size_; |
47 | 955 | } |
48 | 236 | if (isInline() && other.isInline()) { |
49 | 199 | int32_t result = memcmp(value_.inlined, other.value_.inlined, size); |
50 | 199 | return (result != 0) ? result : size_ - other.size_; |
51 | 199 | } |
52 | 37 | int32_t result = memcmp(data() + kPrefixSize, other.data() + kPrefixSize, size); |
53 | 37 | return (result != 0) ? result : size_ - other.size_; |
54 | 236 | } |
55 | | |
56 | | }; // namespace doris |