BDE 4.39.x 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 `bsl::set`.
176///
177/// \note Note that this class is an empty POD type.
178///
179/// See @ref bdlb_caselessstringviewless
181
182 // PUBLIC TYPES
185 typedef bool result_type;
186
187 /// Type alias indicating this is a transparent hash functor.
188 typedef void is_transparent;
189
190 // TRAITS
195
196 // CREATORS
197
198 /// Create a `CaselessStringViewLess` object.
200
201 /// Create a `CaselessStringViewLess` object.
202 /// \note Note that as
203 /// `CaselessStringViewLess` is an empty (stateless) type, this
204 /// operation has no observable effect.
206 default;
207
208 /// Destroy this object.
210
211 // MANIPULATORS
212
213 /// Assign to this object the value of the specified `rhs` object, and
214 /// return a reference providing modifiable access to this object.
215 ///
216 /// \note Note that as `CaselessStringViewLess` is an empty (stateless) type, this
217 /// operation has no observable effect.
219 default;
220
221 // ACCESSORS
222
223 /// Return `true` if the specified `lhs` is lexicographically ordered
224 /// before the specified `rhs`, and `false` otherwise.
225 bool operator()(bsl::string_view lhs, bsl::string_view rhs) const;
226};
227
228// ============================================================================
229// INLINE DEFINITIONS
230// ============================================================================
231
232 // -----------------------------
233 // struct CaselessStringViewLess
234 // -----------------------------
235
236// ACCESSORS
237inline
243
244} // close package namespace
245
246
247#endif
248
249// ----------------------------------------------------------------------------
250// Copyright 2021 Bloomberg Finance L.P.
251//
252// Licensed under the Apache License, Version 2.0 (the "License");
253// you may not use this file except in compliance with the License.
254// You may obtain a copy of the License at
255//
256// http://www.apache.org/licenses/LICENSE-2.0
257//
258// Unless required by applicable law or agreed to in writing, software
259// distributed under the License is distributed on an "AS IS" BASIS,
260// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
261// See the License for the specific language governing permissions and
262// limitations under the License.
263// ----------------------------- END-OF-FILE ----------------------------------
264
265/** @} */
266/** @} */
267/** @} */
Definition bslstl_stringview.h:471
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition bdlb_algorithmworkaroundutil.h:74
Definition bdlb_caselessstringviewless.h:180
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:183
bool result_type
Definition bdlb_caselessstringviewless.h:185
CaselessStringViewLess()=default
Create a CaselessStringViewLess object.
bool operator()(bsl::string_view lhs, bsl::string_view rhs) const
Definition bdlb_caselessstringviewless.h:238
CaselessStringViewLess(const CaselessStringViewLess &original)=default
void is_transparent
Type alias indicating this is a transparent hash functor.
Definition bdlb_caselessstringviewless.h:188
bsl::string_view second_argument_type
Definition bdlb_caselessstringviewless.h:184
static int lowerCaseCmp(const bsl::string_view &lhs, const bsl::string_view &rhs)
Definition bslmf_istriviallycopyable.h:324
Definition bslmf_istriviallydefaultconstructible.h:296