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