1#pragma once
2
3#include <Core/Types.h>
4#include <Parsers/TokenIterator.h>
5#include <map>
6#include <memory>
7#include <Common/SipHash.h>
8
9
10namespace DB
11{
12
13struct StringRange
14{
15 const char * first = nullptr;
16 const char * second = nullptr;
17
18 StringRange() = default;
19 StringRange(const char * begin, const char * end) : first(begin), second(end) {}
20 explicit StringRange(TokenIterator token) : first(token->begin), second(token->end) {}
21
22 StringRange(TokenIterator token_begin, TokenIterator token_end)
23 {
24 /// Empty range.
25 if (token_begin == token_end)
26 {
27 first = token_begin->begin;
28 second = token_begin->begin;
29 return;
30 }
31
32 TokenIterator token_last = token_end;
33 --token_last;
34
35 first = token_begin->begin;
36 second = token_last->end;
37 }
38};
39
40using StringPtr = std::shared_ptr<String>;
41
42
43inline String toString(const StringRange & range)
44{
45 return range.first ? String(range.first, range.second) : String();
46}
47
48/// Hashes only the values of pointers in StringRange. Is used with StringRangePointersEqualTo comparator.
49struct StringRangePointersHash
50{
51 UInt64 operator()(const StringRange & range) const
52 {
53 SipHash hash;
54 hash.update(range.first);
55 hash.update(range.second);
56 return hash.get64();
57 }
58};
59
60/// Ranges are equal only when they point to the same memory region.
61/// It may be used when it's enough to compare substrings by their position in the same string.
62struct StringRangePointersEqualTo
63{
64 constexpr bool operator()(const StringRange &lhs, const StringRange &rhs) const
65 {
66 return std::tie(lhs.first, lhs.second) == std::tie(rhs.first, rhs.second);
67 }
68};
69
70}
71
72