1 /*
2 * Copyright 2017 WebAssembly Community Group participants
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "src/string-view.h"
18
19 #include <algorithm>
20 #include <limits>
21
22 namespace wabt {
23
remove_prefix(size_type n)24 void string_view::remove_prefix(size_type n) {
25 assert(n <= size_);
26 data_ += n;
27 size_ -= n;
28 }
29
remove_suffix(size_type n)30 void string_view::remove_suffix(size_type n) {
31 assert(n <= size_);
32 size_ -= n;
33 }
34
swap(string_view & s)35 void string_view::swap(string_view& s) noexcept {
36 std::swap(data_, s.data_);
37 std::swap(size_, s.size_);
38 }
39
operator std::string() const40 string_view::operator std::string() const {
41 return std::string(data_, size_);
42 }
43
to_string() const44 std::string string_view::to_string() const {
45 return std::string(data_, size_);
46 }
47
max_size() const48 constexpr string_view::size_type string_view::max_size() const noexcept {
49 return std::numeric_limits<size_type>::max();
50 }
51
copy(char * s,size_type n,size_type pos) const52 string_view::size_type string_view::copy(char* s,
53 size_type n,
54 size_type pos) const {
55 assert(pos <= size_);
56 size_t count = std::min(n, size_ - pos);
57 traits_type::copy(s, data_ + pos, count);
58 return count;
59 }
60
substr(size_type pos,size_type n) const61 string_view string_view::substr(size_type pos, size_type n) const {
62 assert(pos <= size_);
63 size_t count = std::min(n, size_ - pos);
64 return string_view(data_ + pos, count);
65 }
66
compare(string_view s) const67 int string_view::compare(string_view s) const noexcept {
68 size_type rlen = std::min(size_, s.size_);
69 int result = traits_type::compare(data_, s.data_, rlen);
70 if (result != 0 || size_ == s.size_) {
71 return result;
72 }
73 return size_ < s.size_ ? -1 : 1;
74 }
75
compare(size_type pos1,size_type n1,string_view s) const76 int string_view::compare(size_type pos1, size_type n1, string_view s) const {
77 return substr(pos1, n1).compare(s);
78 }
79
compare(size_type pos1,size_type n1,string_view s,size_type pos2,size_type n2) const80 int string_view::compare(size_type pos1,
81 size_type n1,
82 string_view s,
83 size_type pos2,
84 size_type n2) const {
85 return substr(pos1, n1).compare(s.substr(pos2, n2));
86 }
87
compare(const char * s) const88 int string_view::compare(const char* s) const {
89 return compare(string_view(s));
90 }
91
compare(size_type pos1,size_type n1,const char * s) const92 int string_view::compare(size_type pos1, size_type n1, const char* s) const {
93 return substr(pos1, n1).compare(string_view(s));
94 }
95
compare(size_type pos1,size_type n1,const char * s,size_type n2) const96 int string_view::compare(size_type pos1,
97 size_type n1,
98 const char* s,
99 size_type n2) const {
100 return substr(pos1, n1).compare(string_view(s, n2));
101 }
102
find(string_view s,size_type pos) const103 string_view::size_type string_view::find(string_view s, size_type pos) const
104 noexcept {
105 pos = std::min(pos, size_);
106 const_iterator iter = std::search(begin() + pos, end(), s.begin(), s.end());
107 return iter == end() ? npos : iter - begin();
108 }
109
find(char c,size_type pos) const110 string_view::size_type string_view::find(char c, size_type pos) const noexcept {
111 return find(string_view(&c, 1), pos);
112 }
113
find(const char * s,size_type pos,size_type n) const114 string_view::size_type string_view::find(const char* s,
115 size_type pos,
116 size_type n) const {
117 return find(string_view(s, n), pos);
118 }
119
find(const char * s,size_type pos) const120 string_view::size_type string_view::find(const char* s, size_type pos) const {
121 return find(string_view(s), pos);
122 }
123
rfind(string_view s,size_type pos) const124 string_view::size_type string_view::rfind(string_view s, size_type pos) const
125 noexcept {
126 pos = std::min(std::min(pos, size_ - s.size_) + s.size_, size_);
127 reverse_iterator iter = std::search(reverse_iterator(begin() + pos), rend(),
128 s.rbegin(), s.rend());
129 return iter == rend() ? npos : (rend() - iter - s.size_);
130 }
131
rfind(char c,size_type pos) const132 string_view::size_type string_view::rfind(char c, size_type pos) const
133 noexcept {
134 return rfind(string_view(&c, 1), pos);
135 }
136
rfind(const char * s,size_type pos,size_type n) const137 string_view::size_type string_view::rfind(const char* s,
138 size_type pos,
139 size_type n) const {
140 return rfind(string_view(s, n), pos);
141 }
142
rfind(const char * s,size_type pos) const143 string_view::size_type string_view::rfind(const char* s, size_type pos) const {
144 return rfind(string_view(s), pos);
145 }
146
find_first_of(string_view s,size_type pos) const147 string_view::size_type string_view::find_first_of(string_view s,
148 size_type pos) const
149 noexcept {
150 pos = std::min(pos, size_);
151 const_iterator iter =
152 std::find_first_of(begin() + pos, end(), s.begin(), s.end());
153 return iter == end() ? npos : iter - begin();
154 }
155
find_first_of(char c,size_type pos) const156 string_view::size_type string_view::find_first_of(char c, size_type pos) const
157 noexcept {
158 return find_first_of(string_view(&c, 1), pos);
159 }
160
find_first_of(const char * s,size_type pos,size_type n) const161 string_view::size_type string_view::find_first_of(const char* s,
162 size_type pos,
163 size_type n) const {
164 return find_first_of(string_view(s, n), pos);
165 }
166
find_first_of(const char * s,size_type pos) const167 string_view::size_type string_view::find_first_of(const char* s,
168 size_type pos) const {
169 return find_first_of(string_view(s), pos);
170 }
171
find_last_of(string_view s,size_type pos) const172 string_view::size_type string_view::find_last_of(string_view s,
173 size_type pos) const noexcept {
174 pos = std::min(pos, size_ - 1);
175 reverse_iterator iter = std::find_first_of(
176 reverse_iterator(begin() + (pos + 1)), rend(), s.begin(), s.end());
177 return iter == rend() ? npos : (rend() - iter - 1);
178 }
179
find_last_of(char c,size_type pos) const180 string_view::size_type string_view::find_last_of(char c, size_type pos) const
181 noexcept {
182 return find_last_of(string_view(&c, 1), pos);
183 }
184
find_last_of(const char * s,size_type pos,size_type n) const185 string_view::size_type string_view::find_last_of(const char* s,
186 size_type pos,
187 size_type n) const {
188 return find_last_of(string_view(s, n), pos);
189 }
190
find_last_of(const char * s,size_type pos) const191 string_view::size_type string_view::find_last_of(const char* s,
192 size_type pos) const {
193 return find_last_of(string_view(s), pos);
194 }
195
196 } // namespace wabt
197