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