LIBEUNOMIA
読み取り中…
検索中…
一致する文字列を見つけられません
utility.h
[詳解]
1/*
2 * Copyright 2021 oZ/acy (名賀月晃嗣) <acy@hiemalis.org>
3 *
4 * Redistribution and use in source and binary forms,
5 * with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
17 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 *
27 */
39#ifndef INCLUDE_GUARD_EUNOMIA_UTILITY_H
40#define INCLUDE_GUARD_EUNOMIA_UTILITY_H
41
42#include <cctype>
43#include <cwctype>
44#include <string>
45#include <vector>
46
47namespace eunomia
48{
49
51
58inline
59std::string lower(const std::string& str)
60{
61 std::string res = str;
62 for (auto& c : res) {
63 c = std::tolower((unsigned char)c);
64 }
65 return res;
66}
67
75inline
76std::wstring lower(const std::wstring& str)
77{
78 std::wstring res = str;
79 for (auto& c : res) {
80 c = std::towlower(c);
81 }
82 return res;
83}
84
85
87
95template<class C, class T, class A>
96inline
97std::vector<std::basic_string<C, T, A>>
99 const std::basic_string<C, T, A>& str, const std::basic_string<C, T, A>& dlm)
100{
101 std::vector<std::basic_string<C, T, A>> rvec;
102 typename std::basic_string<C, T, A>::size_type f = 0;
103
104 while (f < str.size()) {
105 auto b = str.find_first_of(dlm, f);
106 if (b == std::basic_string<C, T, A>::npos) {
107 b = str.size();
108 }
109 if (f < b) {
110 auto s = str.substr(f, b - f);
111 rvec.push_back(s);
112 }
113 f = b + 1;
114 }
115
116 return rvec;
117}
118
119
128template<class C, class T, class A>
129inline
130std::vector<std::basic_string<C, T, A>>
131split(const std::basic_string<C, T, A>& str, const C* dlm)
132{
133 std::vector<std::basic_string<C, T, A>> rvec;
134 typename std::basic_string<C, T, A>::size_type f = 0;
135
136 while (f < str.size()) {
137 auto b = str.find_first_of(dlm, f);
138 if (b == std::basic_string<C, T, A>::npos) {
139 b = str.size();
140 }
141 if (f < b) {
142 auto s = str.substr(f, b - f);
143 rvec.push_back(s);
144 }
145 f = b + 1;
146 }
147
148 return rvec;
149}
150
151
160template<class C, class T, class A>
161inline
162std::vector<std::basic_string<C, T, A>>
163split(const std::basic_string<C, T, A>& str, C dlm)
164{
165 std::vector<std::basic_string<C, T, A>> rvec;
166 typename std::basic_string<C, T, A>::size_type f = 0;
167
168 while (f < str.size()) {
169 auto b = str.find_first_of(dlm, f);
170 if (b == std::basic_string<C, T, A>::npos) {
171 b = str.size();
172 }
173 if (f < b) {
174 auto s = str.substr(f, b - f);
175 rvec.push_back(s);
176 }
177 f = b + 1;
178 }
179
180 return rvec;
181}
182
183
185
193template<class C, class T, class A>
194inline
195std::basic_string<C, T, A>
197 const std::basic_string<C, T, A>& str,
198 const std::basic_string<C, T, A>& removed)
199{
200 auto l = str.find_first_not_of(removed);
201
202 if (l == std::basic_string<C, T, A>::npos)
203 return std::string("");
204
205 auto r = str.find_last_not_of(removed);
206 return str.substr(l, r - l + 1);
207}
208
209
218template<class C, class T, class A>
219inline
220std::basic_string<C, T, A>
221trim(const std::basic_string<C, T, A>& str, const C* removed)
222{
223 auto l = str.find_first_not_of(removed);
224
225 if (l == std::basic_string<C, T, A>::npos)
226 return std::string("");
227
228 auto r = str.find_last_not_of(removed);
229 return str.substr(l, r - l + 1);
230}
231
232
241template<class C, class T, class A>
242inline
243std::basic_string<C, T, A>
245 const std::basic_string<C, T, A>& str,
246 const std::basic_string<C, T, A>& removed)
247{
248 auto l = str.find_first_not_of(removed);
249
250 if (l == std::basic_string<C, T, A>::npos)
251 return std::string("");
252 else
253 return str.substr(l, str.size() - l);
254}
255
256
265template<class C, class T, class A>
266inline
267std::basic_string<C, T, A>
268ltrim(const std::basic_string<C, T, A>& str, const C* removed)
269{
270 auto l = str.find_first_not_of(removed);
271
272 if (l == std::basic_string<C, T, A>::npos)
273 return std::string("");
274 else
275 return str.substr(l, str.size() - l);
276}
277
278
287template<class C, class T, class A>
288inline
289std::basic_string<C, T, A>
291 const std::basic_string<C, T, A>& str,
292 const std::basic_string<C, T, A>& removed)
293{
294 auto r = str.find_last_not_of(removed);
295 if (r == std::basic_string<C, T, A>::npos)
296 return std::string("");
297 else
298 return str.substr(0, r + 1);
299}
300
301
310template<class C, class T, class A>
311inline
312std::basic_string<C, T, A>
313rtrim(const std::basic_string<C, T, A>& str, const C* removed)
314{
315 auto r = str.find_last_not_of(removed);
316 if (r == std::basic_string<C, T, A>::npos)
317 return std::string("");
318 else
319 return str.substr(0, r + 1);
320}
321
322
323
324
325
326}// end of namespace eunomia
327
328
329#endif // INCLUDE_GUARD_EUNOMIA_UTILITY_H
畫像バッファ基底クラステンプレート
Definition imagebuffer.h:84
Definition colour.h:49
std::basic_string< C, T, A > trim(const std::basic_string< C, T, A > &str, const std::basic_string< C, T, A > &removed)
文字列の先頭と末尾からの指定文字の除去
Definition utility.h:196
std::vector< std::basic_string< C, T, A > > split(const std::basic_string< C, T, A > &str, const std::basic_string< C, T, A > &dlm)
文字列分割
Definition utility.h:98
std::string lower(const std::string &str)
小文字文字列化
Definition utility.h:59
std::basic_string< C, T, A > rtrim(const std::basic_string< C, T, A > &str, const std::basic_string< C, T, A > &removed)
文字列の末尾からの指定文字の除去
Definition utility.h:290
std::basic_string< C, T, A > ltrim(const std::basic_string< C, T, A > &str, const std::basic_string< C, T, A > &removed)
文字列の先頭からの指定文字の除去
Definition utility.h:244