aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Parsers/StringRange.h
blob: 3c5959fd0a314a31b25bd7b3f460ac576a47063e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#pragma once

#include <base/types.h>
#include <Parsers/TokenIterator.h>
#include <map>
#include <memory>
#include <Common/SipHash.h>


namespace DB
{

struct StringRange
{
    const char * first = nullptr;
    const char * second = nullptr;

    StringRange() = default;
    StringRange(const char * begin, const char * end) : first(begin), second(end) {}
    explicit StringRange(TokenIterator token) : first(token->begin), second(token->end) {}

    StringRange(TokenIterator token_begin, TokenIterator token_end)
    {
        /// Empty range.
        if (token_begin == token_end)
        {
            first = token_begin->begin;
            second = token_begin->begin;
            return;
        }

        TokenIterator token_last = token_end;
        --token_last;

        first = token_begin->begin;
        second = token_last->end;
    }
};

using StringPtr = std::shared_ptr<String>;


inline String toString(const StringRange & range)
{
    return range.first ? String(range.first, range.second) : String();
}

/// Hashes only the values of pointers in StringRange. Is used with StringRangePointersEqualTo comparator.
struct StringRangePointersHash
{
    UInt64 operator()(const StringRange & range) const
    {
        SipHash hash;
        hash.update(range.first);
        hash.update(range.second);
        return hash.get64();
    }
};

/// Ranges are equal only when they point to the same memory region.
/// It may be used when it's enough to compare substrings by their position in the same string.
struct StringRangePointersEqualTo
{
    constexpr bool operator()(const StringRange &lhs, const StringRange &rhs) const
    {
        return std::tie(lhs.first, lhs.second) == std::tie(rhs.first, rhs.second);
    }
};

}