tlx
string_view.hpp
Go to the documentation of this file.
1/*******************************************************************************
2 * tlx/container/string_view.hpp
3 *
4 * A simplified string_view implementation for portability.
5 *
6 * Part of tlx - http://panthema.net/tlx
7 *
8 * Copyright (C) 2016-2019 Timo Bingmann <tb@panthema.net>
9 *
10 * All rights reserved. Published under the Boost Software License, Version 1.0
11 ******************************************************************************/
12
13#ifndef TLX_CONTAINER_STRING_VIEW_HEADER
14#define TLX_CONTAINER_STRING_VIEW_HEADER
15
16#include <algorithm>
17#include <ostream>
18#include <string>
19
21
22namespace tlx {
23
24//! \addtogroup tlx_container
25//! \{
26
27/*!
28 * StringView is a reference to a part of a string, consisting of only a char
29 * pointer and a length. It does not have ownership of the substring and is used
30 * mainly for temporary objects.
31 */
33{
34public:
35 using iterator = const char*;
36
37 //! Default constructor for a StringView. Doesn't initialize anything.
38 StringView() = default;
39
40 /*!
41 * Creates a new StringView, given a const char* and the size.
42 *
43 * \param data pointer to start of data
44 * \param size size of data in bytes.
45 * \return new StringView object.
46 */
47 StringView(const char* data, size_t size) noexcept
48 : data_(data), size_(size) { }
49
50 /*!
51 * Creates a new StringView, given a const iterator to a std::string and the
52 * size.
53 *
54 * \param data iterator to start of data
55 * \param size size of data in character.
56 * \return new StringView object.
57 */
58 StringView(const std::string::const_iterator& data, size_t size) noexcept
59 : data_(&(*data)), size_(size) { }
60
61 /*!
62 * Creates a new reference StringView, given two const iterators to a
63 * std::string.
64 *
65 * \param begin iterator to start of data
66 * \param end iterator to the end of data.
67 * \return new StringView object.
68 */
69 StringView(const std::string::const_iterator& begin,
70 const std::string::const_iterator& end) noexcept
71 : StringView(begin, end - begin) { }
72
73 //! Construct a StringView to the whole std::string.
74 explicit StringView(const std::string& str) noexcept
75 : StringView(str.begin(), str.end()) { }
76
77 //! Returns a pointer to the start of the data.
78 const char * data() const noexcept {
79 return data_;
80 }
81
82 //! Returns a pointer to the beginning of the data.
83 iterator begin() const noexcept {
84 return data_;
85 }
86
87 //! Returns a pointer beyond the end of the data.
88 iterator end() const noexcept {
89 return data_ + size_;
90 }
91
92 //! Returns the size of this StringView
93 size_t size() const noexcept {
94 return size_;
95 }
96
97 //! Equality operator to compare a StringView with another StringView
98 bool operator == (const StringView& other) const noexcept {
99 return size_ == other.size_ &&
100 std::equal(data_, data_ + size_, other.data_);
101 }
102
103 //! Inequality operator to compare a StringView with another StringView
104 bool operator != (const StringView& other) const noexcept {
105 return !(operator == (other));
106 }
107
108 //! Less operator to compare a StringView with another StringView
109 //! lexicographically
110 bool operator < (const StringView& other) const noexcept {
111 return std::lexicographical_compare(
112 data_, data_ + size_, other.data_, other.data_ + other.size_);
113 }
114
115 //! Equality operator to compare a StringView with a std::string
116 bool operator == (const std::string& other) const noexcept {
117 return size_ == other.size() &&
118 std::equal(data_, data_ + size_, other.data());
119 }
120
121 //! Inequality operator to compare a StringView with a std::string
122 bool operator != (const std::string& other) const noexcept {
123 return !(operator == (other));
124 }
125
126 //! Less operator to compare a StringView with a std::string
127 //! lexicographically
128 bool operator < (const std::string& other) const noexcept {
129 return std::lexicographical_compare(
130 data_, data_ + size_, other.data(), other.data() + other.size());
131 }
132
133 //! make StringView ostreamable
134 friend std::ostream& operator << (std::ostream& os, const StringView& sv) {
135 return os.write(sv.data(), sv.size());
136 }
137
138 //! Returns the data of this StringView as a std::string
139 std::string to_string() const {
140 return std::string(data_, size_);
141 }
142
143 // operator std::string () const { return to_string(); }
144
145private:
146 //! pointer to character data
147 const char* data_;
148 //! size of data
149 size_t size_;
150};
151
152//! Equality operator to compare a std::string with a StringView
153static inline
154bool operator == (const std::string& a, const StringView& b) noexcept {
155 return b == a;
156}
157
158//! Inequality operator to compare a std::string with a StringView
159static inline
160bool operator != (const std::string& a, const StringView& b) noexcept {
161 return b != a;
162}
163
164//! Less operator to compare a std::string with a StringView lexicographically
165static inline
166bool operator < (const std::string& a, const StringView& b) noexcept {
167 return std::lexicographical_compare(
168 a.data(), a.data() + a.size(), b.data(), b.data() + b.size());
169}
170
171//! make alias due to STL similarity
173
174//! \}
175
176} // namespace tlx
177
178namespace std {
179template <>
180struct hash<tlx::StringView> {
181 size_t operator () (const tlx::StringView& sv) const {
182 return tlx::hash_djb2(sv.data(), sv.size());
183 }
184};
185
186} // namespace std
187
188#endif // !TLX_CONTAINER_STRING_VIEW_HEADER
189
190/******************************************************************************/
StringView is a reference to a part of a string, consisting of only a char pointer and a length.
Definition: string_view.hpp:33
bool operator==(const StringView &other) const noexcept
Equality operator to compare a StringView with another StringView.
Definition: string_view.hpp:98
const char * data() const noexcept
Returns a pointer to the start of the data.
Definition: string_view.hpp:78
size_t size() const noexcept
Returns the size of this StringView.
Definition: string_view.hpp:93
StringView(const std::string::const_iterator &data, size_t size) noexcept
Creates a new StringView, given a const iterator to a std::string and the size.
Definition: string_view.hpp:58
StringView()=default
Default constructor for a StringView. Doesn't initialize anything.
size_t size_
size of data
const char * data_
pointer to character data
StringView(const char *data, size_t size) noexcept
Creates a new StringView, given a const char* and the size.
Definition: string_view.hpp:47
std::string to_string() const
Returns the data of this StringView as a std::string.
const char * iterator
Definition: string_view.hpp:35
friend std::ostream & operator<<(std::ostream &os, const StringView &sv)
make StringView ostreamable
iterator end() const noexcept
Returns a pointer beyond the end of the data.
Definition: string_view.hpp:88
StringView(const std::string &str) noexcept
Construct a StringView to the whole std::string.
Definition: string_view.hpp:74
StringView(const std::string::const_iterator &begin, const std::string::const_iterator &end) noexcept
Creates a new reference StringView, given two const iterators to a std::string.
Definition: string_view.hpp:69
bool operator<(const StringView &other) const noexcept
Less operator to compare a StringView with another StringView lexicographically.
bool operator!=(const StringView &other) const noexcept
Inequality operator to compare a StringView with another StringView.
iterator begin() const noexcept
Returns a pointer to the beginning of the data.
Definition: string_view.hpp:83
static bool operator<(const std::string &a, const StringView &b) noexcept
Less operator to compare a std::string with a StringView lexicographically.
static bool operator==(const std::string &a, const StringView &b) noexcept
Equality operator to compare a std::string with a StringView.
static bool operator!=(const std::string &a, const StringView &b) noexcept
Inequality operator to compare a std::string with a StringView.
static uint32_t hash_djb2(const unsigned char *str)
Simple, fast, but "insecure" string hash method by Dan Bernstein from http://www.cse....
Definition: hash_djb2.hpp:26
STL namespace.