BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bdlb_caselessstringviewless.h
Go to the documentation of this file.
1/// @file bdlb_caselessstringviewless.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bdlb_caselessstringviewless.h -*-C++-*-
8#ifndef INCLUDED_BDLB_CASELESSSTRINGVIEWLESS
9#define INCLUDED_BDLB_CASELESSSTRINGVIEWLESS
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bdlb_caselessstringviewless bdlb_caselessstringviewless
15/// @brief Provide a case-insensitive less-than predicate for string views.
16/// @addtogroup bdl
17/// @{
18/// @addtogroup bdlb
19/// @{
20/// @addtogroup bdlb_caselessstringviewless
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bdlb_caselessstringviewless-purpose"> Purpose</a>
25/// * <a href="#bdlb_caselessstringviewless-classes"> Classes </a>
26/// * <a href="#bdlb_caselessstringviewless-description"> Description </a>
27/// * <a href="#bdlb_caselessstringviewless-usage"> Usage </a>
28/// * <a href="#bdlb_caselessstringviewless-example-1-basic-use-of-bdlb-caselessstringviewless"> Example 1: Basic Use of bdlb::CaselessStringViewLess </a>
29///
30/// # Purpose {#bdlb_caselessstringviewless-purpose}
31/// Provide a case-insensitive less-than predicate for string views.
32///
33/// # Classes {#bdlb_caselessstringviewless-classes}
34///
35/// - bdlb::CaselessStringViewLess: a case-insensitive less for @ref string_view s.
36///
37/// @see bsl_map, bsl_set
38///
39/// # Description {#bdlb_caselessstringviewless-description}
40/// This component provides a `struct`,
41/// `bdlb::CaselessStringViewLess`, that defines a functor that compares two
42/// string views using a case-insensitive string comparison. This
43/// lexicographical ordering makes `CaselessStringViewLess` suitable for
44/// supporting `bsl::string`s or `bsl::string_view`s as keys in case-insensitive
45/// ordered associative containers.
46///
47/// Note that using this component to compare keys in a container is less
48/// efficient than converting them all to the same case prior to insertion and
49/// then just doing straight string compares.
50///
51/// ## Usage {#bdlb_caselessstringviewless-usage}
52///
53///
54/// This section illustrates intended use of this component.
55///
56/// ### Example 1: Basic Use of bdlb::CaselessStringViewLess {#bdlb_caselessstringviewless-example-1-basic-use-of-bdlb-caselessstringviewless}
57///
58///
59/// Suppose we need a container to store set of unique strings. The following
60/// code illustrates how to use `bdlb::CaselessStringViewLess` as a comparator
61/// for the standard container `set`, to create a set of unique case-insensitive
62/// string values.
63///
64/// First, we create several strings:
65/// @code
66/// const bsl::string newYork = "NY";
67/// const bsl::string losAngeles = "LA";
68/// const bsl::string newJersey = "NJ";
69/// const bsl::string sanFrancisco = "SF";
70/// const bsl::string anotherNewYork = "ny";
71/// @endcode
72/// Next, we create two containers, one with default comparator and another
73/// using `bdlb::CstringLess` as a comparator:
74/// @code
75/// bsl::set<bsl::string> caseSensitiveSet;
76/// bsl::set<bsl::string, bdlb::CaselessStringViewLess> caseInsensitiveSet;
77/// @endcode
78/// Then, we fill containers with the same contents:
79/// @code
80/// caseSensitiveSet.insert(newYork);
81/// caseSensitiveSet.insert(losAngeles);
82/// caseSensitiveSet.insert(newJersey);
83/// caseSensitiveSet.insert(sanFrancisco);
84/// caseSensitiveSet.insert(anotherNewYork);
85///
86/// caseInsensitiveSet.insert(newYork);
87/// caseInsensitiveSet.insert(losAngeles);
88/// caseInsensitiveSet.insert(newJersey);
89/// caseInsensitiveSet.insert(sanFrancisco);
90/// caseInsensitiveSet.insert(anotherNewYork);
91/// @endcode
92/// Next, we observe that the container created with `CaselessStringViewLess`
93/// (`caseInsensitiveSet`) contains the correct number of unique string values
94/// (4), while the container using the default comparator does not:
95/// @code
96/// assert(5 == caseSensitiveSet.size());
97/// assert(4 == caseInsensitiveSet.size());
98/// @endcode
99/// Now, we observe the members of the case-sensitive set:
100/// @code
101/// assert( caseSensitiveSet.count("NY"));
102/// assert(!caseSensitiveSet.count("nY"));
103/// assert(!caseSensitiveSet.count("Ny"));
104/// assert( caseSensitiveSet.count("ny"));
105///
106/// assert( caseSensitiveSet.count("SF"));
107/// assert(!caseSensitiveSet.count("sF"));
108/// assert(!caseSensitiveSet.count("Sf"));
109/// assert(!caseSensitiveSet.count("sf"));
110/// @endcode
111/// Finally, we observe that we can do case-insensitive access to
112/// `caseInsensiveSet`:
113/// @code
114/// assert( caseInsensitiveSet.count("NY"));
115/// assert( caseInsensitiveSet.count("nY"));
116/// assert( caseInsensitiveSet.count("Ny"));
117/// assert( caseInsensitiveSet.count("ny"));
118///
119/// assert( caseInsensitiveSet.count("LA"));
120/// assert( caseInsensitiveSet.count("lA"));
121/// assert( caseInsensitiveSet.count("La"));
122/// assert( caseInsensitiveSet.count("la"));
123///
124/// assert( caseInsensitiveSet.count("nj"));
125/// assert( caseInsensitiveSet.count("nJ"));
126/// assert( caseInsensitiveSet.count("Nj"));
127/// assert( caseInsensitiveSet.count("NJ"));
128///
129/// assert( caseInsensitiveSet.count("sf"));
130/// assert( caseInsensitiveSet.count("sF"));
131/// assert( caseInsensitiveSet.count("Sf"));
132/// assert( caseInsensitiveSet.count("SF"));
133///
134/// assert(!caseInsensitiveSet.count("GA"));
135/// assert(!caseInsensitiveSet.count("gA"));
136/// assert(!caseInsensitiveSet.count("Ga"));
137/// assert(!caseInsensitiveSet.count("ga"));
138/// @endcode
139/// @}
140/** @} */
141/** @} */
142
143/** @addtogroup bdl
144 * @{
145 */
146/** @addtogroup bdlb
147 * @{
148 */
149/** @addtogroup bdlb_caselessstringviewless
150 * @{
151 */
152
153#include <bdlscm_version.h>
154
155#include <bdlb_stringviewutil.h>
156
160
161#include <bsls_assert.h>
162
163#include <bsl_algorithm.h>
164#include <bsl_cstddef.h>
165#include <bsl_string_view.h>
166
167
168namespace bdlb {
169
170 // =============================
171 // struct CaselessStringViewLess
172 // =============================
173
174/// This `struct` defines an ordering on string views, enabling them for use
175/// as keys in the standard associative containers such as `bsl::map` and
176/// `bsl::set`. Note that this class is an empty POD type.
178
179 // PUBLIC TYPES
182 typedef bool result_type;
183
184 /// Type alias indicating this is a transparent hash functor.
185 typedef void is_transparent;
186
187 // TRAITS
192
193 // CREATORS
194
195 /// Create a `CaselessStringViewLess` object.
197
198 /// Create a `CaselessStringViewLess` object. Note that as
199 /// `CaselessStringViewLess` is an empty (stateless) type, this
200 /// operation has no observable effect.
202 default;
203
204 /// Destroy this object.
206
207 // MANIPULATORS
208
209 /// Assign to this object the value of the specified `rhs` object, and
210 /// return a reference providing modifiable access to this object. Note
211 /// that as `CaselessStringViewLess` is an empty (stateless) type, this
212 /// operation has no observable effect.
214 default;
215
216 // ACCESSORS
217
218 /// Return `true` if the specified `lhs` is lexicographically ordered
219 /// before the specified `rhs`, and `false` otherwise.
220 bool operator()(bsl::string_view lhs, bsl::string_view rhs) const;
221};
222
223// ============================================================================
224// INLINE DEFINITIONS
225// ============================================================================
226
227 // -----------------------------
228 // struct CaselessStringViewLess
229 // -----------------------------
230
231// ACCESSORS
232inline
238
239} // close package namespace
240
241
242#endif
243
244// ----------------------------------------------------------------------------
245// Copyright 2021 Bloomberg Finance L.P.
246//
247// Licensed under the Apache License, Version 2.0 (the "License");
248// you may not use this file except in compliance with the License.
249// You may obtain a copy of the License at
250//
251// http://www.apache.org/licenses/LICENSE-2.0
252//
253// Unless required by applicable law or agreed to in writing, software
254// distributed under the License is distributed on an "AS IS" BASIS,
255// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
256// See the License for the specific language governing permissions and
257// limitations under the License.
258// ----------------------------- END-OF-FILE ----------------------------------
259
260/** @} */
261/** @} */
262/** @} */
Definition bslstl_stringview.h:441
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bdlb_algorithmworkaroundutil.h:74
Definition bdlb_caselessstringviewless.h:177
BSLMF_NESTED_TRAIT_DECLARATION(CaselessStringViewLess, bsl::is_trivially_copyable)
CaselessStringViewLess & operator=(const CaselessStringViewLess &rhs)=default
~CaselessStringViewLess()=default
Destroy this object.
BSLMF_NESTED_TRAIT_DECLARATION(CaselessStringViewLess, bsl::is_trivially_default_constructible)
bsl::string_view first_argument_type
Definition bdlb_caselessstringviewless.h:180
bool result_type
Definition bdlb_caselessstringviewless.h:182
CaselessStringViewLess()=default
Create a CaselessStringViewLess object.
bool operator()(bsl::string_view lhs, bsl::string_view rhs) const
Definition bdlb_caselessstringviewless.h:233
CaselessStringViewLess(const CaselessStringViewLess &original)=default
void is_transparent
Type alias indicating this is a transparent hash functor.
Definition bdlb_caselessstringviewless.h:185
bsl::string_view second_argument_type
Definition bdlb_caselessstringviewless.h:181
static int lowerCaseCmp(const bsl::string_view &lhs, const bsl::string_view &rhs)
Definition bslmf_istriviallycopyable.h:329
Definition bslmf_istriviallydefaultconstructible.h:293