scnlib  0.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
string_view.h
Go to the documentation of this file.
1 // Copyright 2017-2019 Elias Kosunen
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 // This file is a part of scnlib:
16 // https://github.com/eliaskosunen/scnlib
17 
18 #ifndef SCN_DETAIL_STRING_VIEW_H
19 #define SCN_DETAIL_STRING_VIEW_H
20 
21 #include "span.h"
22 #include "util.h"
23 
24 #include <cstring>
25 #include <cwchar>
26 #include <limits>
27 
28 #if SCN_HAS_STRING_VIEW
29 #include <string_view>
30 #endif
31 
32 namespace scn {
34 
35  namespace detail {
36  inline size_t strlen(const char* s) noexcept
37  {
38  return std::strlen(s);
39  }
40  inline size_t strlen(const wchar_t* s) noexcept
41  {
42  return std::wcslen(s);
43  }
44 
45  inline int memcmp(const char* l, const char* r, size_t n) noexcept
46  {
47  return std::memcmp(l, r, n);
48  }
49  inline int memcmp(const wchar_t* l, const wchar_t* r, size_t n) noexcept
50  {
51  return std::wmemcmp(l, r, n);
52  }
53  } // namespace detail
59  template <typename CharT>
61  public:
62  using value_type = CharT;
64 
65  using pointer = value_type*;
66  using const_pointer = const value_type*;
68  using const_reference = const value_type&;
71  using reverse_iterator = std::reverse_iterator<iterator>;
73  using size_type = std::size_t;
74  using difference_type = std::ptrdiff_t;
76 
77  static constexpr const size_type npos = size_type(-1);
78 
79  constexpr basic_string_view() noexcept = default;
81  : m_data(s, static_cast<span_index_type>(c))
82  {
83  }
85  : m_data(s, static_cast<span_index_type>(detail::strlen(s)))
86  {
87  }
88  template <size_t N>
89  constexpr basic_string_view(const CharT (&s)[N]) : m_data(s, N)
90  {
91  }
93  : m_data(first, last)
94  {
95  }
96 #if SCN_HAS_STRING_VIEW
97  constexpr basic_string_view(std::basic_string_view<value_type> str)
98  : m_data(str.data(), str.size())
99  {
100  }
101 #endif
102 
103  constexpr const_iterator begin() const noexcept
104  {
105  return cbegin();
106  }
107  constexpr const_iterator cbegin() const noexcept
108  {
109  return m_data.cbegin();
110  }
111  constexpr const_iterator end() const noexcept
112  {
113  return cend();
114  }
115  constexpr const_iterator cend() const noexcept
116  {
117  return m_data.cend();
118  }
119 
120  constexpr const_iterator rbegin() const noexcept
121  {
122  return crbegin();
123  }
124  constexpr const_iterator crbegin() const noexcept
125  {
126  return m_data.crbegin();
127  }
128  constexpr const_iterator rend() const noexcept
129  {
130  return crend();
131  }
132  constexpr const_iterator crend() const noexcept
133  {
134  return m_data.crend();
135  }
136 
137  constexpr const_reference operator[](size_type pos) const
138  {
139  return m_data[static_cast<typename span_type::index_type>(pos)];
140  }
142  {
143  SCN_EXPECT(pos < size());
144  return m_data.at(static_cast<typename span_type::index_type>(pos));
145  }
146 
147  constexpr const_reference front() const
148  {
149  return operator[](0);
150  }
151  constexpr const_reference back() const
152  {
153  return operator[](size() - 1);
154  }
155  constexpr const_pointer data() const noexcept
156  {
157  return m_data.data();
158  }
159 
160  constexpr size_type size() const noexcept
161  {
162  return static_cast<size_type>(m_data.size());
163  }
164  constexpr size_type length() const noexcept
165  {
166  return size();
167  }
168  constexpr size_type max_size() const noexcept
169  {
171  }
172  SCN_NODISCARD constexpr bool empty() const noexcept
173  {
174  return size() == 0;
175  }
176 
178  {
179  SCN_EXPECT(n <= size());
180  m_data = m_data.subspan(n);
181  }
183  {
184  SCN_EXPECT(n <= size());
185  m_data = m_data.first(size() - n);
186  }
187 
189  {
190  using std::swap;
191  swap(m_data, v.m_data);
192  }
193 
194  size_type copy(pointer dest, size_type count, size_type pos = 0) const
195  {
196  SCN_EXPECT(pos <= size());
197  auto n = detail::min(count, size() - pos);
198  /* std::copy(data() + pos, n, dest); */
199  std::memcpy(dest, begin() + pos, n * sizeof(value_type));
200  return n;
201  }
203  size_type count = npos) const
204  {
205  SCN_EXPECT(pos <= size());
206  auto n = detail::min(count, size() - pos);
207  return m_data.subspan(pos, n);
208  }
209 
210  int compare(basic_string_view v) const noexcept
211  {
212  auto n = detail::min(size(), v.size());
213  auto cmp = memcmp(data(), v.data(), n);
214  if (cmp == 0) {
215  if (size() == v.size()) {
216  return 0;
217  }
218  if (size() > v.size()) {
219  return 1;
220  }
221  return -1;
222  }
223  return cmp;
224  }
225  int compare(size_type pos1, size_type count1, basic_string_view v) const
226  {
227  return substr(pos1, count1).compare(v);
228  }
229  int compare(size_type pos1,
230  size_type count1,
232  size_type pos2,
233  size_type count2) const
234  {
235  return substr(pos1, count1).compare(v.substr(pos2, count2));
236  }
237  int compare(const_pointer s) const
238  {
239  return compare(basic_string_view(s));
240  }
241  int compare(size_type pos1, size_type count1, const_pointer s) const
242  {
243  return substr(pos1, count1).compare(basic_string_view(s));
244  }
245  int compare(size_type pos1,
246  size_type count1,
247  const_pointer s,
248  size_type count2) const
249  {
250  return substr(pos1, count1).compare(basic_string_view(s, count2));
251  }
252 
254  {
255  return {s.data(), s.size()};
256  }
258  {
259  return {s.data(), s.size()};
260  }
261 
262 #if SCN_HAS_STRING_VIEW
263  operator std::basic_string_view<value_type>() const noexcept
264  {
265  return {m_data.data(), m_data.size()};
266  }
267 #endif
268 
269  private:
270  span_type m_data{};
271  };
272 
277 
279 } // namespace scn
280 
281 #endif // SCN_DETAIL_STRING_VIEW_H
std::size_t index_type
Definition: span.h:229
basic_string_view< value_type > make_view(span< const value_type > s)
Definition: string_view.h:257
constexpr const_reference front() const
Definition: string_view.h:147
constexpr const_iterator begin() const noexcept
Definition: string_view.h:103
constexpr const_iterator cbegin() const noexcept
Definition: span.h:288
reverse_iterator const_reverse_iterator
Definition: string_view.h:72
SCN_CONSTEXPR14 void remove_prefix(size_type n)
Definition: string_view.h:177
constexpr const_iterator crend() const noexcept
Definition: string_view.h:132
constexpr const_pointer data() const noexcept
Definition: string_view.h:155
#define SCN_END_NAMESPACE
Definition: config.h:401
constexpr const_reverse_iterator crbegin() const noexcept
Definition: span.h:296
size_t strlen(const wchar_t *s) noexcept
Definition: string_view.h:40
constexpr size_type max_size() const noexcept
Definition: string_view.h:168
const value_type * const_pointer
Definition: string_view.h:66
constexpr const_iterator cend() const noexcept
Definition: span.h:292
int compare(basic_string_view v) const noexcept
Definition: string_view.h:210
SCN_NODISCARD constexpr bool empty() const noexcept
Definition: string_view.h:172
constexpr const_iterator rend() const noexcept
Definition: string_view.h:128
constexpr basic_string_view(const_pointer s)
Definition: string_view.h:84
basic_string_view< value_type > make_view(span< value_type > s)
Definition: string_view.h:253
constexpr const_reverse_iterator crend() const noexcept
Definition: span.h:300
constexpr span< T > subspan(index_type off) const
Definition: span.h:334
constexpr T max(T a, T b) noexcept
Definition: util.h:509
constexpr const_reference back() const
Definition: string_view.h:151
constexpr const_reference operator[](size_type pos) const
Definition: string_view.h:137
int memcmp(const char *l, const char *r, size_t n) noexcept
Definition: string_view.h:45
int compare(size_type pos1, size_type count1, const_pointer s, size_type count2) const
Definition: string_view.h:245
size_t strlen(const char *s) noexcept
Definition: string_view.h:36
span< const value_type > span_type
Definition: string_view.h:63
constexpr const_iterator end() const noexcept
Definition: string_view.h:111
constexpr const_iterator cend() const noexcept
Definition: string_view.h:115
constexpr basic_string_view(const_pointer first, const_pointer last)
Definition: string_view.h:92
std::reverse_iterator< iterator > reverse_iterator
Definition: string_view.h:71
SCN_CONSTEXPR14 basic_string_view substr(size_type pos=0, size_type count=npos) const
Definition: string_view.h:202
constexpr size_type length() const noexcept
Definition: string_view.h:164
constexpr span< T > first(index_type n) const
Definition: span.h:324
constexpr T min(T a, T b) noexcept
Definition: util.h:532
constexpr const_iterator cbegin() const noexcept
Definition: string_view.h:107
SCN_CONSTEXPR14 void swap(small_vector< T, N > &l, small_vector< T, N > &r) noexcept(noexcept(l.swap(r)))
Definition: small_vector.h:898
#define SCN_NODISCARD
Definition: config.h:259
SCN_CONSTEXPR14 void swap(basic_string_view &v) noexcept
Definition: string_view.h:188
const_pointer const_iterator
Definition: span.h:240
#define SCN_BEGIN_NAMESPACE
Definition: config.h:400
constexpr basic_string_view() noexcept=default
int compare(const_pointer s) const
Definition: string_view.h:237
typename span_type::const_iterator iterator
Definition: string_view.h:69
const value_type & const_reference
Definition: string_view.h:68
SCN_CONSTEXPR14 const_reference at(size_type pos) const
Definition: string_view.h:141
int memcmp(const wchar_t *l, const wchar_t *r, size_t n) noexcept
Definition: string_view.h:49
constexpr pointer data() const noexcept
Definition: span.h:311
int compare(size_type pos1, size_type count1, basic_string_view v, size_type pos2, size_type count2) const
Definition: string_view.h:229
size_type copy(pointer dest, size_type count, size_type pos=0) const
Definition: string_view.h:194
constexpr size_type size() const noexcept
Definition: string_view.h:160
typename span_type::index_type span_index_type
Definition: string_view.h:75
constexpr const_iterator crbegin() const noexcept
Definition: string_view.h:124
int compare(size_type pos1, size_type count1, basic_string_view v) const
Definition: string_view.h:225
constexpr basic_string_view(const CharT(&s)[N])
Definition: string_view.h:89
constexpr index_type size() const noexcept
Definition: span.h:315
static constexpr const size_type npos
Definition: string_view.h:77
A view over a (sub)string.
Definition: string_view.h:60
constexpr const_iterator rbegin() const noexcept
Definition: string_view.h:120
SCN_CONSTEXPR14 void remove_suffix(size_type n)
Definition: string_view.h:182
int compare(size_type pos1, size_type count1, const_pointer s) const
Definition: string_view.h:241
#define SCN_EXPECT(cond)
Definition: config.h:396
#define SCN_CONSTEXPR14
Definition: config.h:240