Gamedev Framework (gf)  0.6.0
A C++11 framework for 2D games
StringRef.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2017 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 
32  /**
33  * @ingroup core
34  * @brief A constant reference to a string and its size
35  *
36  * This class stores a pointer to a string and its size. It can be built
37  * from various inputs: `std::string`, C string.
38  *
39  * @sa gf::ArrayRef, gf::BufferRef
40  */
41  class StringRef {
42  public:
43  /**
44  * @brief Default constructor
45  *
46  * Data is `nullptr` and size is 0.
47  */
48  constexpr StringRef()
49  : m_data(nullptr)
50  , m_size(0)
51  {
52 
53  }
54 
55  /**
56  * @brief Null constructor
57  *
58  * Data is `nullptr` and size is 0.
59  */
60  constexpr StringRef(std::nullptr_t)
61  : m_data(nullptr)
62  , m_size(0)
63  {
64 
65  }
66 
67  /**
68  * @brief Constructor from a pointer and a size
69  *
70  * @param str A pointer to a string
71  * @param size The size of the string
72  */
73  constexpr StringRef(const char *str, std::size_t size)
74  : m_data(str)
75  , m_size(size)
76  {
77 
78  }
79 
80  /**
81  * @brief Constructor from a null-terminated string
82  *
83  * The size is computed with `std::strlen`.
84  *
85  * @param str A null-terminated string
86  */
87  StringRef(const char *str)
88  : m_data(str)
89  , m_size(std::strlen(str))
90  {
91 
92  }
93 
94  /**
95  * @brief Constructor from a `std::string`
96  *
97  * @param str A C++ string
98  */
99  StringRef(const std::string& str)
100  : m_data(str.data())
101  , m_size(str.size())
102  {
103 
104  }
105 
106  /**
107  * @brief Get a pointer to the string
108  *
109  * @returns A pointer to the beginning of the string
110  */
111  constexpr const char *getData() const noexcept {
112  return m_data;
113  }
114 
115  /**
116  * @brief Get the size of the string
117  *
118  * @returns The size of the string
119  */
120  constexpr std::size_t getSize() const noexcept {
121  return m_size;
122  }
123 
124  /**
125  * @brief Check if the string is empty
126  *
127  * An empty string is a string with size 0. So the null string is empty.
128  *
129  * @returns True if the string is empty
130  */
131  constexpr bool isEmpty() const noexcept {
132  return m_size == 0;
133  }
134 
135  /**
136  * @brief Get an iterator to the beginning of the string
137  *
138  * @returns A pointer to the first character
139  *
140  * @sa end()
141  */
142  constexpr const char *begin() const noexcept {
143  return m_data;
144  }
145 
146  /**
147  * @brief Get an iterator past the end of the string
148  *
149  * @returns A pointer past the last character
150  *
151  * @sa begin()
152  */
153  constexpr const char *end() const noexcept {
154  return m_data + m_size;
155  }
156 
157  /**
158  * @brief Get a character at a given index
159  *
160  * No verification is done on the index.
161  *
162  * @param index The index of character
163  * @returns The character at the given index
164  */
165  constexpr char operator[](std::size_t index) const {
166  return m_data[index];
167  }
168 
169  private:
170  const char *m_data;
171  std::size_t m_size;
172  };
173 
174 
175 #ifndef DOXYGEN_SHOULD_SKIP_THIS
176 }
177 #endif
178 }
179 
180 #endif // GF_STRING_REF_H
constexpr StringRef()
Default constructor.
Definition: StringRef.h:48
StringRef(const std::string &str)
Constructor from a std::string
Definition: StringRef.h:99
constexpr const char * getData() const noexcept
Get a pointer to the string.
Definition: StringRef.h:111
constexpr bool isEmpty() const noexcept
Check if the string is empty.
Definition: StringRef.h:131
The namespace for gf classes.
Definition: Action.h:34
constexpr StringRef(std::nullptr_t)
Null constructor.
Definition: StringRef.h:60
constexpr const char * begin() const noexcept
Get an iterator to the beginning of the string.
Definition: StringRef.h:142
constexpr const char * end() const noexcept
Get an iterator past the end of the string.
Definition: StringRef.h:153
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:165
constexpr std::size_t getSize() const noexcept
Get the size of the string.
Definition: StringRef.h:120
StringRef(const char *str)
Constructor from a null-terminated string.
Definition: StringRef.h:87
constexpr StringRef(const char *str, std::size_t size)
Constructor from a pointer and a size.
Definition: StringRef.h:73