mutable
A Database System for Research and Fast Prototyping
Loading...
Searching...
No Matches
StringView.hpp
Go to the documentation of this file.
1#pragma once
2
4#include <cstdint>
5#include <cstring>
6#include <iostream>
7#include <limits>
8#include <sstream>
9#include <string>
10#include <string_view>
11
12
13namespace m {
14
15
17{
18 private:
19 uintptr_t left_;
20 uintptr_t right_;
21
22 public:
23 StringView(const char *start, std::size_t length)
24 : left_(reinterpret_cast<uintptr_t>(start))
25 , right_((length << 1UL) | 0x1UL)
26 {
28 std::cerr << "Created SV \"" << *this << "\".\n";
29 }
30
31 StringView(const char *start, const char *end) : StringView(start, end - start) { M_insist(end >= start); }
32 StringView(const char *start) : StringView(start, strlen(start)) { }
33 StringView(const std::string &str) : StringView(str.c_str(), str.length()) { }
34
37 : left_(reinterpret_cast<uintptr_t>(left))
38 , right_(reinterpret_cast<uintptr_t>(right))
39 {
41 std::cerr << "Composed SV \"" << *this << "\" of \"" << *left << "\" .. \"" << *right << "\".\n";
42 }
43
44 bool is_flat() const { return right_ & 0x1; }
45 bool is_composed() const { return not is_flat(); }
46
47 const char * data() const { M_insist(is_flat()); return reinterpret_cast<char*>(left_); }
48 std::size_t length() const { M_insist(is_flat()); return right_ >> 1UL; }
49
50 const StringView & left() const { M_insist(is_composed()); return *reinterpret_cast<const StringView*>(left_); }
51 const StringView & right() const { M_insist(is_composed()); return *reinterpret_cast<const StringView*>(right_); }
52
53 int compare(StringView other) const { return str().compare(other.str()); }
54
55 bool operator==(StringView other) const { return compare(other) == 0; }
56 bool operator!=(StringView other) const { return compare(other) != 0; }
57 bool operator< (StringView other) const { return compare(other) < 0; }
58 bool operator> (StringView other) const { return compare(other) > 0; }
59 bool operator<=(StringView other) const { return compare(other) <= 0; }
60 bool operator>=(StringView other) const { return compare(other) >= 0; }
61
62 friend std::ostream & operator<<(std::ostream &out, StringView sv) {
63 if (sv.is_flat())
64 return out << std::string_view(sv.data(), sv.length());
65 else
66 return out << sv.left() << sv.right();
67 }
68
69 friend std::string to_string(StringView sv) {
70 std::ostringstream oss;
71 oss << sv;
72 return oss.str();
73 }
74
75 std::string str() const { return to_string(*this); }
76};
77
78inline StringView operator+(const StringView &left, const StringView &right) {
79 std::cerr << "Concatenate \"" << left << "\" .. \"" << right << "\".\n";
80 return StringView(&left, &right);
81}
82
83}
#define M_insist(...)
Definition: macro.hpp:129
‍mutable namespace
Definition: Backend.hpp:10
Schema operator+(const Schema &left, const Schema &right)
Definition: Schema.hpp:250
StringView(const std::string &str)
Definition: StringView.hpp:33
bool operator==(StringView other) const
Definition: StringView.hpp:55
bool operator<=(StringView other) const
Definition: StringView.hpp:59
bool operator!=(StringView other) const
Definition: StringView.hpp:56
bool operator>(StringView other) const
Definition: StringView.hpp:58
std::size_t length() const
Definition: StringView.hpp:48
std::string str() const
Definition: StringView.hpp:75
int compare(StringView other) const
Definition: StringView.hpp:53
const StringView & left() const
Definition: StringView.hpp:50
bool is_flat() const
Definition: StringView.hpp:44
uintptr_t left_
Definition: StringView.hpp:19
friend std::ostream & operator<<(std::ostream &out, StringView sv)
Definition: StringView.hpp:62
const StringView & right() const
Definition: StringView.hpp:51
StringView(const char *start, std::size_t length)
Definition: StringView.hpp:23
StringView(const char *start)
Definition: StringView.hpp:32
const char * data() const
Definition: StringView.hpp:47
friend std::string to_string(StringView sv)
Definition: StringView.hpp:69
bool is_composed() const
Definition: StringView.hpp:45
StringView(const char *start, const char *end)
Definition: StringView.hpp:31
uintptr_t right_
if the LSB is set, this value denotes the length of the string, appended with a 1-bit
Definition: StringView.hpp:20
bool operator<(StringView other) const
Definition: StringView.hpp:57
StringView(const StringView *left, const StringView *right)
Compose a StringView of two string_views.
Definition: StringView.hpp:36
bool operator>=(StringView other) const
Definition: StringView.hpp:60