MPD  0.20.6
StringView.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2013-2015 Max Kellermann <max.kellermann@gmail.com>
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * - Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
11  * - Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the
14  * distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
19  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
20  * FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
27  * OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #ifndef STRING_VIEW_HXX
31 #define STRING_VIEW_HXX
32 
33 #include "ConstBuffer.hxx"
34 
35 #include <string.h>
36 
37 struct StringView : ConstBuffer<char> {
38  StringView() = default;
39 
40  constexpr StringView(pointer_type _data, size_type _size)
41  :ConstBuffer<char>(_data, _size) {}
42 
43  constexpr StringView(pointer_type _begin, pointer_type _end)
44  :ConstBuffer<char>(_begin, _end - _begin) {}
45 
47  :ConstBuffer<char>(_data,
48  _data != nullptr ? strlen(_data) : 0) {}
49 
50  constexpr StringView(std::nullptr_t n)
51  :ConstBuffer<char>(n) {}
52 
53  static constexpr StringView Empty() {
54  return StringView("", size_t(0));
55  }
56 
57  template<size_t n>
58  static constexpr StringView Literal(const char (&_data)[n]) {
59  static_assert(n > 0, "");
60  return {_data, n - 1};
61  }
62 
63  static constexpr StringView Literal() {
64  return StringView("", size_t(0));
65  }
66 
67  void SetEmpty() {
68  data = "";
69  size = 0;
70  }
71 
72  gcc_pure
73  pointer_type Find(char ch) const {
74  return (pointer_type)memchr(data, ch, size);
75  }
76 
77  StringView &operator=(std::nullptr_t) {
78  data = nullptr;
79  size = 0;
80  return *this;
81  }
82 
84  data = _data;
85  size = _data != nullptr ? strlen(_data) : 0;
86  return *this;
87  }
88 
89  gcc_pure
90  bool StartsWith(StringView needle) const {
91  return size >= needle.size &&
92  memcmp(data, needle.data, needle.size) == 0;
93  }
94 
95  gcc_pure
96  bool Equals(StringView other) const {
97  return size == other.size &&
98  memcmp(data, other.data, size) == 0;
99  }
100 
101  template<size_t n>
102  bool EqualsLiteral(const char (&other)[n]) const {
103  return Equals(Literal(other));
104  }
105 
106  gcc_pure
107  bool EqualsIgnoreCase(StringView other) const {
108  return size == other.size &&
109  strncasecmp(data, other.data, size) == 0;
110  }
111 
112  template<size_t n>
113  bool EqualsLiteralIgnoreCase(const char (&other)[n]) const {
114  return EqualsIgnoreCase(Literal(other));
115  }
116 
120  void StripLeft();
121 
125  void StripRight();
126 
127  void Strip() {
128  StripLeft();
129  StripRight();
130  }
131 };
132 
133 #endif
gcc_pure pointer_type Find(char ch) const
Definition: StringView.hxx:73
void SetEmpty()
Definition: StringView.hxx:67
void Strip()
Definition: StringView.hxx:127
static constexpr StringView Literal(const char(&_data)[n])
Definition: StringView.hxx:58
static constexpr StringView Literal()
Definition: StringView.hxx:63
bool EqualsLiteralIgnoreCase(const char(&other)[n]) const
Definition: StringView.hxx:113
StringView & operator=(pointer_type _data)
Definition: StringView.hxx:83
constexpr StringView(pointer_type _data, size_type _size)
Definition: StringView.hxx:40
void StripRight()
Skip all whitespace at the end.
StringView & operator=(std::nullptr_t)
Definition: StringView.hxx:77
const char * pointer_type
Definition: ConstBuffer.hxx:91
StringView()=default
gcc_pure bool StartsWith(StringView needle) const
Definition: StringView.hxx:90
gcc_pure bool Equals(StringView other) const
Definition: StringView.hxx:96
bool EqualsLiteral(const char(&other)[n]) const
Definition: StringView.hxx:102
gcc_pure bool EqualsIgnoreCase(StringView other) const
Definition: StringView.hxx:107
pointer_type data
Definition: ConstBuffer.hxx:96
static constexpr StringView Empty()
Definition: StringView.hxx:53
StringView(pointer_type _data)
Definition: StringView.hxx:46
A reference to a memory area that is read-only.
Definition: FlacPcm.hxx:29
#define gcc_pure
Definition: Compiler.h:116
void StripLeft()
Skip all whitespace at the beginning.
constexpr StringView(std::nullptr_t n)
Definition: StringView.hxx:50
constexpr StringView(pointer_type _begin, pointer_type _end)
Definition: StringView.hxx:43