1 | // Boost string_algo library compare.hpp header file -------------------------// |
---|
2 | |
---|
3 | // Copyright Pavol Droba 2002-2006. Use, modification and |
---|
4 | // distribution is subject to the Boost Software License, Version |
---|
5 | // 1.0. (See accompanying file LICENSE_1_0.txt or copy at |
---|
6 | // http://www.boost.org/LICENSE_1_0.txt) |
---|
7 | |
---|
8 | // See http://www.boost.org for updates, documentation, and revision history. |
---|
9 | |
---|
10 | #ifndef BOOST_STRING_COMPARE_HPP |
---|
11 | #define BOOST_STRING_COMPARE_HPP |
---|
12 | |
---|
13 | #include <boost/algorithm/string/config.hpp> |
---|
14 | #include <locale> |
---|
15 | |
---|
16 | /*! \file |
---|
17 | Defines element comparison predicates. Many algorithms in this library can |
---|
18 | take an additional argument with a predicate used to compare elements. |
---|
19 | This makes it possible, for instance, to have case insensitive versions |
---|
20 | of the algorithms. |
---|
21 | */ |
---|
22 | |
---|
23 | namespace boost { |
---|
24 | namespace algorithm { |
---|
25 | |
---|
26 | // is_equal functor -----------------------------------------------// |
---|
27 | |
---|
28 | //! is_equal functor |
---|
29 | /*! |
---|
30 | Standard STL equal_to only handle comparison between arguments |
---|
31 | of the same type. This is a less restrictive version which wraps operator ==. |
---|
32 | */ |
---|
33 | struct is_equal |
---|
34 | { |
---|
35 | //! Function operator |
---|
36 | /*! |
---|
37 | Compare two operands for equality |
---|
38 | */ |
---|
39 | template< typename T1, typename T2 > |
---|
40 | bool operator()( const T1& Arg1, const T2& Arg2 ) const |
---|
41 | { |
---|
42 | return Arg1==Arg2; |
---|
43 | } |
---|
44 | }; |
---|
45 | |
---|
46 | //! case insensitive version of is_equal |
---|
47 | /*! |
---|
48 | Case insensitive comparison predicate. Comparison is done using |
---|
49 | specified locales. |
---|
50 | */ |
---|
51 | struct is_iequal |
---|
52 | { |
---|
53 | //! Constructor |
---|
54 | /*! |
---|
55 | \param Loc locales used for comparison |
---|
56 | */ |
---|
57 | is_iequal( const std::locale& Loc=std::locale() ) : |
---|
58 | m_Loc( Loc ) {} |
---|
59 | |
---|
60 | //! Function operator |
---|
61 | /*! |
---|
62 | Compare two operands. Case is ignored. |
---|
63 | */ |
---|
64 | template< typename T1, typename T2 > |
---|
65 | bool operator()( const T1& Arg1, const T2& Arg2 ) const |
---|
66 | { |
---|
67 | #if defined(__BORLANDC__) && (__BORLANDC__ >= 0x560) && (__BORLANDC__ <= 0x564) && !defined(_USE_OLD_RW_STL) |
---|
68 | return std::toupper(Arg1)==std::toupper(Arg2); |
---|
69 | #else |
---|
70 | return std::toupper<T1>(Arg1,m_Loc)==std::toupper<T2>(Arg2,m_Loc); |
---|
71 | #endif |
---|
72 | } |
---|
73 | |
---|
74 | private: |
---|
75 | std::locale m_Loc; |
---|
76 | }; |
---|
77 | |
---|
78 | // is_less functor -----------------------------------------------// |
---|
79 | |
---|
80 | //! is_less functor |
---|
81 | /*! |
---|
82 | Convenient version of standard std::less. Operation is templated, therefore it is |
---|
83 | not required to specify the exact types upon the construction |
---|
84 | */ |
---|
85 | struct is_less |
---|
86 | { |
---|
87 | //! Functor operation |
---|
88 | /*! |
---|
89 | Compare two operands using > operator |
---|
90 | */ |
---|
91 | template< typename T1, typename T2 > |
---|
92 | bool operator()( const T1& Arg1, const T2& Arg2 ) const |
---|
93 | { |
---|
94 | return Arg1<Arg2; |
---|
95 | } |
---|
96 | }; |
---|
97 | |
---|
98 | |
---|
99 | //! case insensitive version of is_less |
---|
100 | /*! |
---|
101 | Case insensitive comparison predicate. Comparison is done using |
---|
102 | specified locales. |
---|
103 | */ |
---|
104 | struct is_iless |
---|
105 | { |
---|
106 | //! Constructor |
---|
107 | /*! |
---|
108 | \param Loc locales used for comparison |
---|
109 | */ |
---|
110 | is_iless( const std::locale& Loc=std::locale() ) : |
---|
111 | m_Loc( Loc ) {} |
---|
112 | |
---|
113 | //! Function operator |
---|
114 | /*! |
---|
115 | Compare two operands. Case is ignored. |
---|
116 | */ |
---|
117 | template< typename T1, typename T2 > |
---|
118 | bool operator()( const T1& Arg1, const T2& Arg2 ) const |
---|
119 | { |
---|
120 | #if defined(__BORLANDC__) && (__BORLANDC__ >= 0x560) && (__BORLANDC__ <= 0x564) && !defined(_USE_OLD_RW_STL) |
---|
121 | return std::toupper(Arg1)<std::toupper(Arg2); |
---|
122 | #else |
---|
123 | return std::toupper<T1>(Arg1,m_Loc)<std::toupper<T2>(Arg2,m_Loc); |
---|
124 | #endif |
---|
125 | } |
---|
126 | |
---|
127 | private: |
---|
128 | std::locale m_Loc; |
---|
129 | }; |
---|
130 | |
---|
131 | // is_not_greater functor -----------------------------------------------// |
---|
132 | |
---|
133 | //! is_not_greater functor |
---|
134 | /*! |
---|
135 | Convenient version of standard std::not_greater_to. Operation is templated, therefore it is |
---|
136 | not required to specify the exact types upon the construction |
---|
137 | */ |
---|
138 | struct is_not_greater |
---|
139 | { |
---|
140 | //! Functor operation |
---|
141 | /*! |
---|
142 | Compare two operands using > operator |
---|
143 | */ |
---|
144 | template< typename T1, typename T2 > |
---|
145 | bool operator()( const T1& Arg1, const T2& Arg2 ) const |
---|
146 | { |
---|
147 | return Arg1<=Arg2; |
---|
148 | } |
---|
149 | }; |
---|
150 | |
---|
151 | |
---|
152 | //! case insensitive version of is_not_greater |
---|
153 | /*! |
---|
154 | Case insensitive comparison predicate. Comparison is done using |
---|
155 | specified locales. |
---|
156 | */ |
---|
157 | struct is_not_igreater |
---|
158 | { |
---|
159 | //! Constructor |
---|
160 | /*! |
---|
161 | \param Loc locales used for comparison |
---|
162 | */ |
---|
163 | is_not_igreater( const std::locale& Loc=std::locale() ) : |
---|
164 | m_Loc( Loc ) {} |
---|
165 | |
---|
166 | //! Function operator |
---|
167 | /*! |
---|
168 | Compare two operands. Case is ignored. |
---|
169 | */ |
---|
170 | template< typename T1, typename T2 > |
---|
171 | bool operator()( const T1& Arg1, const T2& Arg2 ) const |
---|
172 | { |
---|
173 | #if defined(__BORLANDC__) && (__BORLANDC__ >= 0x560) && (__BORLANDC__ <= 0x564) && !defined(_USE_OLD_RW_STL) |
---|
174 | return std::toupper(Arg1)<=std::toupper(Arg2); |
---|
175 | #else |
---|
176 | return std::toupper<T1>(Arg1,m_Loc)<=std::toupper<T2>(Arg2,m_Loc); |
---|
177 | #endif |
---|
178 | } |
---|
179 | |
---|
180 | private: |
---|
181 | std::locale m_Loc; |
---|
182 | }; |
---|
183 | |
---|
184 | |
---|
185 | } // namespace algorithm |
---|
186 | |
---|
187 | // pull names to the boost namespace |
---|
188 | using algorithm::is_equal; |
---|
189 | using algorithm::is_iequal; |
---|
190 | using algorithm::is_less; |
---|
191 | using algorithm::is_iless; |
---|
192 | using algorithm::is_not_greater; |
---|
193 | using algorithm::is_not_igreater; |
---|
194 | |
---|
195 | } // namespace boost |
---|
196 | |
---|
197 | |
---|
198 | #endif // BOOST_STRING_COMPARE_HPP |
---|