All Classes Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
trim.hpp
1 // The MIT License (MIT)
2 
3 // Copyright (c) 2012-2014 Danny Y., Rapptz
4 
5 // Permission is hereby granted, free of charge, to any person obtaining a copy of
6 // this software and associated documentation files (the "Software"), to deal in
7 // the Software without restriction, including without limitation the rights to
8 // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9 // the Software, and to permit persons to whom the Software is furnished to do so,
10 // subject to the following conditions:
11 
12 // The above copyright notice and this permission notice shall be included in all
13 // copies or substantial portions of the Software.
14 
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17 // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18 // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19 // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 
22 #ifndef GEARS_STRING_TRIM_HPP
23 #define GEARS_STRING_TRIM_HPP
24 
25 #include "find.hpp"
26 #include "classification.hpp"
27 
28 namespace gears {
29 namespace string {
49 template<typename String, typename UnaryPredicate>
50 inline meta::Unqualified<String> trim_left_if(String str, UnaryPredicate&& pred) {
51  auto pos = find_first_not_of(str, std::forward<UnaryPredicate>(pred));
53  return { str.substr(pos) };
54  }
55  return str;
56 }
57 
77 template<typename String, typename UnaryPredicate>
78 inline meta::Unqualified<String> trim_right_if(String str, UnaryPredicate&& pred) {
79  auto pos = find_last_not_of(str, std::forward<UnaryPredicate>(pred));
81  return { str.substr(0, pos + 1) };
82  }
83  return str;
84 }
85 
103 template<typename String, typename UnaryPredicate>
104 inline meta::Unqualified<String> trim_if(String&& str, UnaryPredicate&& pred) {
105  return trim_left_if(trim_right_if(std::forward<String>(str), pred), pred);
106 }
107 
118 template<typename String>
119 inline meta::Unqualified<String> trim_left(String&& str, const std::locale& loc = std::locale()) {
120  return trim_left_if(std::forward<String>(str), is_space(loc));
121 }
122 
133 template<typename String>
134 inline meta::Unqualified<String> trim_right(String&& str, const std::locale& loc = std::locale()) {
135  return trim_right_if(std::forward<String>(str), is_space(loc));
136 }
137 
148 template<typename String>
149 inline meta::Unqualified<String> trim(String&& str, const std::locale& loc = std::locale()) {
150  return trim_left(trim_right(std::forward<String>(str), loc), loc);
151 }
152 } // string
153 } // gears
154 
155 #endif // GEARS_STRING_TRIM_HPP