TTKMusicPlayer  4.2.0.0
TTKMusicPlayer imitates Kugou UI, the music player uses of qmmp core library based on Qt for windows and linux
ttkstringview.h
Go to the documentation of this file.
1 #ifndef TTKSTRINGVIEW_H
2 #define TTKSTRINGVIEW_H
3 
4 /***************************************************************************
5  * This file is part of the TTK Library Module project
6  * Copyright (C) 2015 - 2025 Greedysky Studio
7 
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License as published by
10  * the Free Software Foundation; either version 3 of the License, or
11  * (at your option) any later version.
12 
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU Lesser General Public License for more details.
17 
18  * You should have received a copy of the GNU Lesser General Public License along
19  * with this program; If not, see <http://www.gnu.org/licenses/>.
20  ***************************************************************************/
21 
22 #include <string>
23 #include "ttkmoduleexport.h"
24 
28 template<typename _CharT, typename _Traits = std::char_traits<_CharT>>
30 {
31 public:
32  using traits_type = _Traits;
33  using value_type = std::basic_string<_CharT>;
34  using pointer = _CharT*;
35  using const_pointer = const _CharT*;
36  using reference = _CharT&;
37  using const_reference = const _CharT&;
38  using const_iterator = const _CharT*;
40  using const_reverse_iterator = std::reverse_iterator<const_iterator>;
42  using size_type = size_t;
43  static constexpr size_type npos = size_type(-1);
44 
45 public:
46  constexpr TTKBaseStringView() noexcept
47  : m_data(nullptr),
48  m_length(0)
49  {
50 
51  }
52 
53  template<size_type T>
54  constexpr TTKBaseStringView(const _CharT(&data)[T])
55  : m_data(data),
56  m_length(T - 1)
57  {
58 
59  }
60 
61  explicit constexpr TTKBaseStringView(const _CharT *data, const size_type length)
62  : m_data(data),
63  m_length(length)
64  {
65 
66  }
67 
68  constexpr TTKBaseStringView(const _CharT *data)
69  : m_data(data),
71  {
72 
73  }
74 
75  constexpr TTKBaseStringView(const TTKBaseStringView &) noexcept = default;
77  : m_data(s.data()),
78  m_length(s.length())
79  {
80 
81  }
82 
83  constexpr TTKBaseStringView& operator=(const TTKBaseStringView &sv) noexcept
84  {
85  m_data = sv.data();
86  m_length = sv.size();
87  return *this;
88  }
89 
90  explicit constexpr operator value_type() const
91  {
92  return value_type(m_data, m_length);
93  }
94 
95  constexpr const_iterator begin() const noexcept { return m_data; }
96  constexpr const_iterator end() const noexcept { return m_data + m_length; }
97  constexpr const_iterator cbegin() const noexcept { return m_data; }
98  constexpr const_iterator cend() const noexcept { return m_data + m_length; }
99  constexpr const_reverse_iterator rbegin() const noexcept { return const_reverse_iterator(end()); }
100  constexpr const_reverse_iterator rend() const noexcept{ return const_reverse_iterator(begin()); }
101  constexpr const_reverse_iterator crbegin() const noexcept { return const_reverse_iterator(end()); }
102  constexpr const_reverse_iterator crend() const noexcept { return const_reverse_iterator(begin()); }
103  constexpr size_type size() const noexcept { return m_length; }
104  constexpr size_type length() const noexcept { return m_length; }
105  constexpr bool empty() const noexcept { return m_length == 0; }
106  constexpr const _CharT& operator[](size_type o) const { return m_data[o]; }
107 
108  const _CharT& at(size_type o) const
109  {
110  if(o >= m_length)
111  {
112  throw std::out_of_range("o exceeds length");
113  }
114  return m_data[o];
115  }
116 
117  constexpr const _CharT& front() const noexcept { return m_data[0]; }
118  constexpr const _CharT& back() const noexcept { return m_data[m_length - 1]; }
119  constexpr const _CharT *data() const noexcept { return m_data; }
120 
121  constexpr TTKBaseStringView substr(size_type a) const noexcept
122  {
123  return a >= m_length ? TTKBaseStringView() : TTKBaseStringView(m_data + a, m_length - a);
124  }
125 
126  constexpr TTKBaseStringView substr(size_type a, size_type b) const noexcept
127  {
128  return a >= m_length ? TTKBaseStringView() : TTKBaseStringView(m_data + a, a + b > m_length ? m_length - a : b);
129  }
130 
131  constexpr size_type find(const _CharT c) const noexcept
132  {
133  size_type l = 0;
134  while(l < m_length)
135  {
136  if(m_data[l] == c)
137  {
138  return l;
139  }
140 
141  ++l;
142  }
144  }
145 
146  constexpr size_type find(const value_type &c) const noexcept
147  {
148  size_type l = 0, a = 0;
149  while(l < m_length)
150  {
151  if(m_data[l] != c[a])
152  {
153  a = 0;
154  }
155 
156  ++l;
157  ++a;
158 
159  if(a == c.length())
160  {
161  return l - c.length();
162  }
163  }
165  }
166 
167  constexpr size_type find(const TTKBaseStringView &sv) const noexcept
168  {
169  size_type l = 0, a = 0;
170  while(l < m_length)
171  {
172  if(m_data[l] != sv[a])
173  {
174  a = 0;
175  }
176 
177  ++l;
178  ++a;
179 
180  if(a == sv.size())
181  {
182  return l - sv.size();
183  }
184  }
186  }
187 
188  constexpr size_type find(const _CharT *c) const noexcept
189  {
190  size_type l = 0, sl = strlen(c), a = 0;
191  while(l < m_length)
192  {
193  if(m_data[l] != c[a])
194  {
195  a = 0;
196  }
197 
198  ++l;
199  ++a;
200 
201  if(a == sl)
202  {
203  return l - sl;
204  }
205  }
207  }
208 
209  constexpr size_type find(const _CharT *c, size_type sl) const noexcept
210  {
211  size_type l = 0, a = 0;
212  while(l < m_length)
213  {
214  if(m_data[l] != c[a])
215  {
216  a = 0;
217  }
218 
219  ++l;
220  ++a;
221 
222  if(a == sl)
223  {
224  return l - sl;
225  }
226  }
228  }
229 
230  constexpr size_type rfind(const _CharT c) const noexcept
231  {
232  size_type s = m_length;
233  while(0 < s)
234  {
235  if(m_data[s] == c)
236  {
237  return s;
238  }
239 
240  --s;
241  }
243  }
244 
245  constexpr size_type rfind(const _CharT *s) const noexcept
246  {
247  const size_type m = strlen(s);
248  if(m == 1)
249  {
250  return rfind(*s);
251  }
252 
253  const size_type n = size();
254  if(n < m)
255  {
257  }
258 
259  size_type c[256] = { 0 };
260  for(size_type i = m; i > 0; --i)
261  {
262  c[int(s[i - 1])] = i;
263  }
264 
265  for(size_type j = n - m;;)
266  {
267  if(memcmp(s, m_data + j, m) == 0)
268  {
269  return j;
270  }
271 
272  if(j == 0)
273  {
275  }
276 
277  size_type x = c[int(m_data[j - 1])];
278  if(x == 0)
279  {
280  x = m + 1;
281  }
282 
283  if(j < x)
284  {
286  }
287 
288  j -= x;
289  }
290  }
291 
292  constexpr size_type find_first_of(const value_type &s, size_type l = 0) const noexcept
293  {
294  size_type a = 0;
295  while(l < m_length)
296  {
297  while(s[a])
298  {
299  if(m_data[l] == s[a])
300  {
301  return l;
302  }
303 
304  ++a;
305  }
306 
307  ++l;
308  a = 0;
309  }
311  }
312 
313  constexpr size_type find_first_of(const _CharT *s, size_type l = 0) const noexcept
314  {
315  size_type a = 0;
316  while(l < m_length)
317  {
318  while(s[a])
319  {
320  if(m_data[l] == s[a])
321  {
322  return l;
323  }
324 
325  ++a;
326  }
327 
328  ++l;
329  a = 0;
330  }
332  }
333 
334  constexpr size_type find_first_of(_CharT c, size_type l = 0) const noexcept
335  {
336  while(l < m_length)
337  {
338  if(m_data[l] == c)
339  {
340  return l;
341  }
342 
343  ++l;
344  }
346  }
347 
348  constexpr size_type find_first_not_of(const value_type &s) const
349  {
350  if(empty())
351  {
353  }
354 
355  const size_type r = strspn(data(), s.data());
356  return m_data[r] ? r : TTKBaseStringView::npos;
357  }
358 
359  constexpr size_type find_first_not_of(const value_type &s, size_type o) const
360  {
361  if(size() <= o)
362  {
364  }
365 
366  const size_type r = strspn(data() + o, s.data()) + o;
367  return m_data[r] ? r : TTKBaseStringView::npos;
368  }
369 
370  constexpr size_type find_first_not_of(const _CharT *s) const
371  {
372  if(empty())
373  {
375  }
376 
377  const size_type r = strspn(this->data(), s);
378  return m_data[r] ? r : TTKBaseStringView::npos;
379  }
380 
381  constexpr size_type find_first_not_of(const _CharT *s, size_type o) const
382  {
383  if(size() <= o)
384  {
386  }
387 
388  const size_type r = strspn(this->data() + o, s) + o;
389  return m_data[r] ? r : TTKBaseStringView::npos;
390  }
391 
392  constexpr size_type find_first_not_of(_CharT c, size_type o = 0) const
393  {
394  if(size() <= o)
395  {
397  }
398 
399  const _CharT s[2] = { c, '\0' };
400  const size_type r = strspn(data() + o, s) + o;
401  return m_data[r] ? r : TTKBaseStringView::npos;
402  }
403 
404  [[nodiscard]] constexpr int compare(const TTKBaseStringView &sv) const noexcept
405  {
406  const int rc = traits_type::compare(m_data, sv.m_data, (std::min)(m_length, sv.m_length));
407  return rc != 0 ? rc : (m_length == sv.m_length ? 0 : m_length < sv.m_length ? -1 : 1);
408  }
409 
410  [[nodiscard]] constexpr int compare(const _CharT *data) const noexcept
411  {
412  const size_type l = traits_type::length(data);
413  const int rc = traits_type::compare(m_data, data, (std::min)(m_length, l));
414  return rc != 0 ? rc : (m_length == l ? 0 : m_length < l ? -1 : 1);
415  }
416 
417  [[nodiscard]] constexpr int compare(const value_type &s) const noexcept
418  {
419  const int rc = traits_type::compare(m_data, s.data(), (std::min)(m_length, s.length()));
420  return rc != 0 ? rc : (m_length == s.length() ? 0 : m_length < s.length() ? -1 : 1);
421  }
422 
423  friend std::basic_ostream<_CharT>& operator<<(std::basic_ostream<_CharT> &os, const TTKBaseStringView &sv)
424  {
425  os.write(sv.m_data, sv.m_length);
426  return os;
427  }
428 
430  {
431  s.append(sv.m_data, sv.m_length);
432  return s;
433  }
434 
435  static TTKBaseStringView split(const _CharT *&cur, const _CharT *line_end, _CharT split_char)
436  {
437  const _CharT *start = cur;
438  while(start < (line_end - 1) && *start == split_char)
439  {
440  ++start;
441  }
442 
443  const char *end = start + 1;
444  while(end < (line_end - 1) && *end != split_char)
445  {
446  ++end;
447  }
448 
449  cur = end + 1;
450  return TTKBaseStringView(start, cur - start - (*end == split_char ? 1 : 0));
451  }
452 
453  inline value_type& operator<<(value_type &s) { s.append(data(), size()); return s; }
454  // ==
455  inline bool operator==(const TTKBaseStringView &other) noexcept { return compare(other) == 0; }
456  inline bool operator==(const value_type &other) noexcept { return compare(other) == 0; }
457  inline bool operator==(const _CharT *other) noexcept { return compare(other) == 0; }
458  // !=
459  inline bool operator!=(const TTKBaseStringView &other) noexcept { return compare(other) != 0; }
460  inline bool operator!=(const value_type &other) noexcept { return compare(other) != 0; }
461  inline bool operator!=(const _CharT *other) noexcept { return compare(other) != 0; }
462  // <=
463  inline bool operator<=(const TTKBaseStringView &other) noexcept { return compare(other) <= 0; }
464  inline bool operator<=(const value_type &other) noexcept { return compare(other) <= 0; }
465  inline bool operator<=(const _CharT *other) noexcept { return compare(other) <= 0; }
466  // <
467  inline bool operator<(const TTKBaseStringView &other) noexcept { return compare(other) < 0; }
468  inline bool operator<(const value_type &other) noexcept { return compare(other) < 0; }
469  inline bool operator<(const _CharT *other) noexcept { return compare(other) < 0; }
470  // >=
471  inline bool operator>=(const TTKBaseStringView &other) noexcept { return compare(other) >= 0; }
472  inline bool operator>=(const value_type &other) noexcept { return compare(other) >= 0; }
473  inline bool operator>=(const _CharT *other) noexcept { return compare(other) >= 0; }
474  // >
475  inline bool operator>(const TTKBaseStringView &other) noexcept { return compare(other) > 0; }
476  inline bool operator>(const value_type &other) noexcept { return compare(other) > 0; }
477  inline bool operator>(const _CharT *other) noexcept { return compare(other) > 0; }
478 
479 private:
482 
483 };
484 
485 //
490 
491 //
492 template <>
493 struct std::hash<TTKStringView>
494 {
495  [[nodiscard]] size_t operator()(const TTKStringView sv) const noexcept
496  {
497 #ifdef _MSVC_LANG
498  return std::_Hash_array_representation(sv.data(), sv.size());
499 #else
500  return std::_Hash_impl::hash(sv.data(), sv.size());
501 #endif
502  }
503 };
504 //
505 template <>
506 struct std::hash<TTKWStringView>
507 {
508  [[nodiscard]] size_t operator()(const TTKWStringView sv) const noexcept
509  {
510 #ifdef _MSVC_LANG
511  return std::_Hash_array_representation(sv.data(), sv.size());
512 #else
513  return std::_Hash_impl::hash(sv.data(), sv.size());
514 #endif
515  }
516 };
517 //
518 template <>
519 struct std::hash<TTKU16StringView>
520 {
521  [[nodiscard]] size_t operator()(const TTKU16StringView sv) const noexcept
522  {
523 #ifdef _MSVC_LANG
524  return std::_Hash_array_representation(sv.data(), sv.size());
525 #else
526  return std::_Hash_impl::hash(sv.data(), sv.size());
527 #endif
528  }
529 };
530 //
531 template <>
532 struct std::hash<TTKU32StringView>
533 {
534  [[nodiscard]] size_t operator()(const TTKU32StringView sv) const noexcept
535  {
536 #ifdef _MSVC_LANG
537  return std::_Hash_array_representation(sv.data(), sv.size());
538 #else
539  return std::_Hash_impl::hash(sv.data(), sv.size());
540 #endif
541  }
542 };
543 
544 
548 namespace literals
549 {
550  namespace string_view_literals
551  {
552  inline constexpr TTKStringView operator""sv(const char* s, size_t l) noexcept
553  {
554  return TTKStringView(s, l);
555  }
556 
557  inline constexpr TTKWStringView operator""sv(const wchar_t* s, size_t l) noexcept
558  {
559  return TTKWStringView(s, l);
560  }
561 
562  inline constexpr TTKU16StringView operator""sv(const char16_t* s, size_t l) noexcept
563  {
564  return TTKU16StringView(s, l);
565  }
566 
567  inline constexpr TTKU32StringView operator""sv(const char32_t* s, size_t l) noexcept
568  {
569  return TTKU32StringView(s, l);
570  }
571  }
572 }
573 
574 
575 // compatiblity for std string_view
576 namespace std
577 {
578 #if !TTK_HAS_CXX17
583 #endif
584 }
585 
586 #endif // TTKSTRINGVIEW_H
constexpr TTKBaseStringView substr(size_type a, size_type b) const noexcept
constexpr TTKBaseStringView(const _CharT(&data)[T])
Definition: ttkstringview.h:54
constexpr const _CharT * data() const noexcept
#define T(v)
Definition: http_parser.c:237
bool operator>(const value_type &other) noexcept
constexpr const_reverse_iterator rend() const noexcept
constexpr int compare(const _CharT *data) const noexcept
size_t operator()(const TTKU32StringView sv) const noexcept
constexpr const _Tp & min(const _Tp &a, const _Tp &b) noexcept
Definition: ttkcompat.h:27
constexpr TTKBaseStringView substr(size_type a) const noexcept
constexpr const_iterator cbegin() const noexcept
Definition: ttkstringview.h:97
constexpr size_type find_first_of(const _CharT *s, size_type l=0) const noexcept
const_reverse_iterator reverse_iterator
Definition: ttkstringview.h:41
constexpr size_type length() const noexcept
constexpr TTKBaseStringView(const _CharT *data, const size_type length)
Definition: ttkstringview.h:61
const _CharT & at(size_type o) const
constexpr const_reverse_iterator rbegin() const noexcept
Definition: ttkstringview.h:99
constexpr size_type find_first_of(_CharT c, size_type l=0) const noexcept
constexpr size_type find(const _CharT c) const noexcept
constexpr size_type find(const _CharT *c, size_type sl) const noexcept
constexpr size_type find_first_not_of(const _CharT *s, size_type o) const
const _CharT & const_reference
Definition: ttkstringview.h:37
constexpr size_type rfind(const _CharT *s) const noexcept
Definition: ttkcompat.h:39
typedef int(ZCALLBACK *close_file_func) OF((voidpf opaque
bool operator==(const TTKBaseStringView &other) noexcept
TTKBaseStringView< wchar_t > TTKWStringView
constexpr const_iterator begin() const noexcept
Definition: ttkstringview.h:95
constexpr TTKBaseStringView() noexcept
Definition: ttkstringview.h:46
The class of the string view modules.
Definition: ttkstringview.h:29
constexpr const _CharT & front() const noexcept
constexpr TTKBaseStringView(const _CharT *data)
Definition: ttkstringview.h:68
const _CharT * const_pointer
Definition: ttkstringview.h:35
const_pointer m_data
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition: ttkstringview.h:40
constexpr size_type find_first_not_of(const value_type &s, size_type o) const
bool operator==(const _CharT *other) noexcept
bool operator<=(const value_type &other) noexcept
static constexpr size_type npos
Definition: ttkstringview.h:43
constexpr const_iterator end() const noexcept
Definition: ttkstringview.h:96
constexpr int compare(const value_type &s) const noexcept
size_t operator()(const TTKU16StringView sv) const noexcept
constexpr size_type find_first_not_of(const _CharT *s) const
constexpr size_type find(const value_type &c) const noexcept
bool operator!=(const TTKBaseStringView &other) noexcept
bool operator!=(const _CharT *other) noexcept
bool operator>=(const TTKBaseStringView &other) noexcept
constexpr size_type find(const TTKBaseStringView &sv) const noexcept
bool operator<(const value_type &other) noexcept
const _CharT * const_iterator
Definition: ttkstringview.h:38
constexpr size_type size() const noexcept
constexpr const_reverse_iterator crend() const noexcept
value_type & operator<<(value_type &s)
TTKBaseStringView< char > TTKStringView
constexpr const_iterator cend() const noexcept
Definition: ttkstringview.h:98
constexpr bool empty() const noexcept
size_t operator()(const TTKStringView sv) const noexcept
bool operator!=(const value_type &other) noexcept
TTKBaseStringView< char16_t > TTKU16StringView
bool operator<=(const TTKBaseStringView &other) noexcept
std::basic_string< _CharT > value_type
Definition: ttkstringview.h:33
The namespace of the string_view literals.
constexpr size_type find(const _CharT *c) const noexcept
constexpr size_type find_first_not_of(_CharT c, size_type o=0) const
constexpr int compare(const TTKBaseStringView &sv) const noexcept
bool operator>(const TTKBaseStringView &other) noexcept
constexpr const _CharT & operator[](size_type o) const
constexpr TTKBaseStringView & operator=(const TTKBaseStringView &sv) noexcept
Definition: ttkstringview.h:83
bool operator>(const _CharT *other) noexcept
bool operator>=(const _CharT *other) noexcept
constexpr size_type find_first_of(const value_type &s, size_type l=0) const noexcept
constexpr size_type find_first_not_of(const value_type &s) const
#define const
Definition: zconf.h:233
bool operator>=(const value_type &other) noexcept
friend value_type & operator+=(value_type &s, const TTKBaseStringView &sv)
bool operator<=(const _CharT *other) noexcept
TTKBaseStringView< char32_t > TTKU32StringView
const_iterator iterator
Definition: ttkstringview.h:39
bool operator<(const _CharT *other) noexcept
constexpr const _CharT & back() const noexcept
bool operator==(const value_type &other) noexcept
bool operator<(const TTKBaseStringView &other) noexcept
static TTKBaseStringView split(const _CharT *&cur, const _CharT *line_end, _CharT split_char)
constexpr const_reverse_iterator crbegin() const noexcept
constexpr size_type rfind(const _CharT c) const noexcept
size_t operator()(const TTKWStringView sv) const noexcept