scnlib  0.1.2
FormattedinputformodernC++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
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 namespace scn {
30 
31  namespace detail {
32  inline size_t strlen(const char* s) noexcept
33  {
34  return std::strlen(s);
35  }
36  inline size_t strlen(const wchar_t* s) noexcept
37  {
38  return std::wcslen(s);
39  }
40 
41  inline int memcmp(const char* l, const char* r, size_t n) noexcept
42  {
43  return std::memcmp(l, r, n);
44  }
45  inline int memcmp(const wchar_t* l, const wchar_t* r, size_t n) noexcept
46  {
47  return std::wmemcmp(l, r, n);
48  }
49  } // namespace detail
55  template <typename CharT>
57  public:
58  using value_type = CharT;
60 
61  using pointer = value_type*;
62  using const_pointer = const value_type*;
64  using const_reference = const value_type&;
67  using reverse_iterator = std::reverse_iterator<iterator>;
69  using size_type = std::size_t;
70  using difference_type = std::ptrdiff_t;
72 
74 
75  SCN_CONSTEXPR basic_string_view() noexcept = default;
77  : m_data(s, static_cast<span_index_type>(c))
78  {
79  }
81  : m_data(s, static_cast<span_index_type>(detail::strlen(s)))
82  {
83  }
84  template <size_t N>
85  SCN_CONSTEXPR basic_string_view(const CharT (&s)[N]) : m_data(s, N)
86  {
87  }
88 
90  {
91  return cbegin();
92  }
94  {
95  return m_data.cbegin();
96  }
97  SCN_CONSTEXPR const_iterator end() const noexcept
98  {
99  return cend();
100  }
102  {
103  return m_data.cend();
104  }
105 
107  {
108  return crbegin();
109  }
111  {
112  return m_data.crbegin();
113  }
115  {
116  return crend();
117  }
119  {
120  return m_data.crend();
121  }
122 
124  {
125  return m_data[static_cast<typename span_type::index_type>(pos)];
126  }
128  {
129  SCN_EXPECT(pos < size());
130  return m_data.at(static_cast<typename span_type::index_type>(pos));
131  }
132 
134  {
135  return operator[](0);
136  }
138  {
139  return operator[](size() - 1);
140  }
142  {
143  return m_data.data();
144  }
145 
146  SCN_CONSTEXPR size_type size() const noexcept
147  {
148  return static_cast<size_type>(m_data.size());
149  }
150  SCN_CONSTEXPR size_type length() const noexcept
151  {
152  return size();
153  }
155  {
157  }
158  SCN_NODISCARD SCN_CONSTEXPR bool empty() const noexcept
159  {
160  return size() == 0;
161  }
162 
164  {
165  SCN_EXPECT(n <= size());
166  m_data = m_data.subspan(n);
167  }
169  {
170  SCN_EXPECT(n <= size());
171  m_data = m_data.first(size() - n);
172  }
173 
175  {
176  using std::swap;
177  swap(m_data, v.m_data);
178  }
179 
180  size_type copy(pointer dest, size_type count, size_type pos = 0) const
181  {
182  SCN_EXPECT(pos <= size());
183  auto n = detail::min(count, size() - pos);
184  /* std::copy(data() + pos, n, dest); */
185  std::memcpy(dest, begin() + pos, n * sizeof(value_type));
186  return n;
187  }
189  size_type count = npos) const
190  {
191  SCN_EXPECT(pos <= size());
192  auto n = detail::min(count, size() - pos);
193  return m_data.subspan(pos, n);
194  }
195 
196  int compare(basic_string_view v) const noexcept
197  {
198  auto n = detail::min(size(), v.size());
199  auto cmp = memcmp(data(), v.data(), n);
200  if (cmp == 0) {
201  if (size() == v.size()) {
202  return 0;
203  }
204  if (size() > v.size()) {
205  return 1;
206  }
207  return -1;
208  }
209  return cmp;
210  }
211  int compare(size_type pos1, size_type count1, basic_string_view v) const
212  {
213  return substr(pos1, count1).compare(v);
214  }
215  int compare(size_type pos1,
216  size_type count1,
218  size_type pos2,
219  size_type count2) const
220  {
221  return substr(pos1, count1).compare(v.substr(pos2, count2));
222  }
223  int compare(const_pointer s) const
224  {
225  return compare(basic_string_view(s));
226  }
227  int compare(size_type pos1, size_type count1, const_pointer s) const
228  {
229  return substr(pos1, count1).compare(basic_string_view(s));
230  }
231  int compare(size_type pos1,
232  size_type count1,
233  const_pointer s,
234  size_type count2) const
235  {
236  return substr(pos1, count1).compare(basic_string_view(s, count2));
237  }
238 
239  private:
240  span_type m_data{};
241  };
242 
247 
249 } // namespace scn
250 
251 #endif // SCN_DETAIL_STRING_VIEW_H
std::size_t index_type
Definition: span.h:37
SCN_CONSTEXPR const_iterator cbegin() const noexcept
Definition: span.h:75
reverse_iterator const_reverse_iterator
Definition: string_view.h:68
SCN_CONSTEXPR14 void remove_prefix(size_type n)
Definition: string_view.h:163
SCN_CONSTEXPR T max(T a, T b) noexcept
Definition: util.h:121
#define SCN_END_NAMESPACE
Definition: config.h:376
SCN_CONSTEXPR span< T > first(index_type n) const
Definition: span.h:111
SCN_CONSTEXPR const_reverse_iterator crbegin() const noexcept
Definition: span.h:83
size_t strlen(const wchar_t *s) noexcept
Definition: string_view.h:36
const value_type * const_pointer
Definition: string_view.h:62
int compare(basic_string_view v) const noexcept
Definition: string_view.h:196
SCN_CONSTEXPR const_iterator rbegin() const noexcept
Definition: string_view.h:106
SCN_CONSTEXPR basic_string_view() noexcept=default
SCN_NODISCARD SCN_CONSTEXPR bool empty() const noexcept
Definition: string_view.h:158
SCN_CONSTEXPR const_reference front() const
Definition: string_view.h:133
SCN_CONSTEXPR basic_string_view(const_pointer s)
Definition: string_view.h:80
int memcmp(const char *l, const char *r, size_t n) noexcept
Definition: string_view.h:41
SCN_CONSTEXPR pointer data() const noexcept
Definition: span.h:98
int compare(size_type pos1, size_type count1, const_pointer s, size_type count2) const
Definition: string_view.h:231
static SCN_CONSTEXPR_DECL const size_type npos
Definition: string_view.h:73
size_t strlen(const char *s) noexcept
Definition: string_view.h:32
span< const value_type > span_type
Definition: string_view.h:59
std::reverse_iterator< iterator > reverse_iterator
Definition: string_view.h:67
SCN_CONSTEXPR14 basic_string_view substr(size_type pos=0, size_type count=npos) const
Definition: string_view.h:188
SCN_CONSTEXPR const_iterator end() const noexcept
Definition: string_view.h:97
SCN_CONSTEXPR size_type max_size() const noexcept
Definition: string_view.h:154
SCN_CONSTEXPR14 void swap(small_vector< T, N > &l, small_vector< T, N > &r) noexcept(noexcept(l.swap(r)))
Definition: small_vector.h:851
SCN_CONSTEXPR size_type length() const noexcept
Definition: string_view.h:150
#define SCN_NODISCARD
Definition: config.h:239
SCN_CONSTEXPR const_iterator rend() const noexcept
Definition: string_view.h:114
SCN_CONSTEXPR14 void swap(basic_string_view &v) noexcept
Definition: string_view.h:174
const_pointer const_iterator
Definition: span.h:44
SCN_CONSTEXPR T min(T a, T b) noexcept
Definition: util.h:144
SCN_CONSTEXPR const_reverse_iterator crend() const noexcept
Definition: span.h:87
#define SCN_BEGIN_NAMESPACE
Definition: config.h:375
int compare(const_pointer s) const
Definition: string_view.h:223
typename span_type::const_iterator iterator
Definition: string_view.h:65
const value_type & const_reference
Definition: string_view.h:64
SCN_CONSTEXPR14 const_reference at(size_type pos) const
Definition: string_view.h:127
int memcmp(const wchar_t *l, const wchar_t *r, size_t n) noexcept
Definition: string_view.h:45
SCN_CONSTEXPR const_pointer data() const noexcept
Definition: string_view.h:141
int compare(size_type pos1, size_type count1, basic_string_view v, size_type pos2, size_type count2) const
Definition: string_view.h:215
SCN_CONSTEXPR const_iterator cend() const noexcept
Definition: span.h:79
SCN_CONSTEXPR const_reference operator[](size_type pos) const
Definition: string_view.h:123
SCN_CONSTEXPR const_iterator begin() const noexcept
Definition: string_view.h:89
SCN_CONSTEXPR basic_string_view(const CharT(&s)[N])
Definition: string_view.h:85
size_type copy(pointer dest, size_type count, size_type pos=0) const
Definition: string_view.h:180
SCN_CONSTEXPR size_type size() const noexcept
Definition: string_view.h:146
typename span_type::index_type span_index_type
Definition: string_view.h:71
SCN_CONSTEXPR span< T > subspan(index_type off) const
Definition: span.h:121
int compare(size_type pos1, size_type count1, basic_string_view v) const
Definition: string_view.h:211
SCN_CONSTEXPR const_iterator crbegin() const noexcept
Definition: string_view.h:110
SCN_CONSTEXPR index_type size() const noexcept
Definition: span.h:102
SCN_CONSTEXPR const_iterator cbegin() const noexcept
Definition: string_view.h:93
#define SCN_CONSTEXPR
Definition: config.h:226
A view over a (sub)string.
Definition: string_view.h:56
SCN_CONSTEXPR14 void remove_suffix(size_type n)
Definition: string_view.h:168
SCN_CONSTEXPR const_reference back() const
Definition: string_view.h:137
#define SCN_CONSTEXPR_DECL
Definition: config.h:228
SCN_CONSTEXPR const_iterator crend() const noexcept
Definition: string_view.h:118
int compare(size_type pos1, size_type count1, const_pointer s) const
Definition: string_view.h:227
#define SCN_EXPECT(cond)
Definition: config.h:371
#define SCN_CONSTEXPR14
Definition: config.h:227
SCN_CONSTEXPR const_iterator cend() const noexcept
Definition: string_view.h:101