Gamedev Framework (gf)  0.17.0
A C++14 framework for 2D games
StringRef.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2019 Julien Bernard
4  *
5  * This software is provided 'as-is', without any express or implied
6  * warranty. In no event will the authors be held liable for any damages
7  * arising from the use of this software.
8  *
9  * Permission is granted to anyone to use this software for any purpose,
10  * including commercial applications, and to alter it and redistribute it
11  * freely, subject to the following restrictions:
12  *
13  * 1. The origin of this software must not be misrepresented; you must not
14  * claim that you wrote the original software. If you use this software
15  * in a product, an acknowledgment in the product documentation would be
16  * appreciated but is not required.
17  * 2. Altered source versions must be plainly marked as such, and must not be
18  * misrepresented as being the original software.
19  * 3. This notice may not be removed or altered from any source distribution.
20  */
21 #ifndef GF_STRING_REF_H
22 #define GF_STRING_REF_H
23 
24 #include <cstring>
25 #include <string>
26 
27 namespace gf {
28 #ifndef DOXYGEN_SHOULD_SKIP_THIS
29 inline namespace v1 {
30 #endif
31 
41  class StringRef {
42  public:
43  using iterator = const char*;
44  using const_iterator = const char*;
45 
51  constexpr StringRef()
52  : m_data(nullptr)
53  , m_size(0)
54  {
55 
56  }
57 
63  constexpr StringRef(std::nullptr_t)
64  : m_data(nullptr)
65  , m_size(0)
66  {
67 
68  }
69 
76  constexpr StringRef(const char *str, std::size_t size)
77  : m_data(str)
78  , m_size(size)
79  {
80 
81  }
82 
90  StringRef(const char *str)
91  : m_data(str)
92  , m_size(std::char_traits<char>::length(str))
93  {
94 
95  }
96 
102  StringRef(const std::string& str)
103  : m_data(str.data())
104  , m_size(str.size())
105  {
106 
107  }
108 
115  constexpr StringRef(const char *b, const char *e)
116  : m_data(b)
117  , m_size(e - b)
118  {
119 
120  }
121 
127  constexpr const char *getData() const noexcept {
128  return m_data;
129  }
130 
136  constexpr std::size_t getSize() const noexcept {
137  return m_size;
138  }
139 
145  constexpr int getISize() const noexcept {
146  return static_cast<int>(m_size);
147  }
148 
156  constexpr bool isEmpty() const noexcept {
157  return m_size == 0;
158  }
159 
167  constexpr const_iterator begin() const noexcept {
168  return m_data;
169  }
170 
178  constexpr const_iterator end() const noexcept {
179  return m_data + m_size;
180  }
181 
190  constexpr char operator[](std::size_t index) const {
191  return m_data[index];
192  }
193 
199  std::string toString() const {
200  if (m_size == 0 || m_data == nullptr) {
201  return "";
202  }
203 
204  return std::string(m_data, m_size);
205  }
206 
207  private:
208  const char *m_data;
209  std::size_t m_size;
210  };
211 
212 
213 #ifndef DOXYGEN_SHOULD_SKIP_THIS
214 }
215 #endif
216 }
217 
218 #endif // GF_STRING_REF_H
constexpr StringRef()
Default constructor.
Definition: StringRef.h:51
StringRef(const std::string &str)
Constructor from a std::string
Definition: StringRef.h:102
constexpr const char * getData() const noexcept
Get a pointer to the string.
Definition: StringRef.h:127
constexpr const_iterator begin() const noexcept
Get an iterator to the beginning of the string.
Definition: StringRef.h:167
constexpr bool isEmpty() const noexcept
Check if the string is empty.
Definition: StringRef.h:156
STL namespace.
constexpr const_iterator end() const noexcept
Get an iterator past the end of the string.
Definition: StringRef.h:178
const char * const_iterator
Definition: StringRef.h:44
constexpr int getISize() const noexcept
Get the size of the string as an int.
Definition: StringRef.h:145
The namespace for gf classes.
Definition: Action.h:35
constexpr StringRef(std::nullptr_t)
Null constructor.
Definition: StringRef.h:63
std::string toString() const
Get a std::string.
Definition: StringRef.h:199
A constant reference to a string and its size.
Definition: StringRef.h:41
constexpr char operator[](std::size_t index) const
Get a character at a given index.
Definition: StringRef.h:190
constexpr StringRef(const char *b, const char *e)
Constructor from two iterators.
Definition: StringRef.h:115
constexpr std::size_t getSize() const noexcept
Get the size of the string.
Definition: StringRef.h:136
const char * iterator
Definition: StringRef.h:43
StringRef(const char *str)
Constructor from a null-terminated string.
Definition: StringRef.h:90
constexpr StringRef(const char *str, std::size_t size)
Constructor from a pointer and a size.
Definition: StringRef.h:76