BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bdlb_caselessstringviewless

Detailed Description

Outline

Purpose

Provide a case-insensitive less-than predicate for string views.

Classes

See also
bsl_map, bsl_set

Description

This component provides a struct, bdlb::CaselessStringViewLess, that defines a functor that compares two string views using a case-insensitive string comparison. This lexicographical ordering makes CaselessStringViewLess suitable for supporting bsl::strings or bsl::string_views as keys in case-insensitive ordered associative containers.

Note that using this component to compare keys in a container is less efficient than converting them all to the same case prior to insertion and then just doing straight string compares.

Usage

This section illustrates intended use of this component.

Example 1: Basic Use of bdlb::CaselessStringViewLess

Suppose we need a container to store set of unique strings. The following code illustrates how to use bdlb::CaselessStringViewLess as a comparator for the standard container set, to create a set of unique case-insensitive string values.

First, we create several strings:

const bsl::string newYork = "NY";
const bsl::string losAngeles = "LA";
const bsl::string newJersey = "NJ";
const bsl::string sanFrancisco = "SF";
const bsl::string anotherNewYork = "ny";
Definition bslstl_string.h:1281

Next, we create two containers, one with default comparator and another using bdlb::CstringLess as a comparator:

bsl::set<bsl::string> caseSensitiveSet;
Definition bslstl_set.h:657

Then, we fill containers with the same contents:

caseSensitiveSet.insert(newYork);
caseSensitiveSet.insert(losAngeles);
caseSensitiveSet.insert(newJersey);
caseSensitiveSet.insert(sanFrancisco);
caseSensitiveSet.insert(anotherNewYork);
caseInsensitiveSet.insert(newYork);
caseInsensitiveSet.insert(losAngeles);
caseInsensitiveSet.insert(newJersey);
caseInsensitiveSet.insert(sanFrancisco);
caseInsensitiveSet.insert(anotherNewYork);
pair< iterator, bool > insert(const value_type &value)
Definition bslstl_set.h:2326

Next, we observe that the container created with CaselessStringViewLess (caseInsensitiveSet) contains the correct number of unique string values (4), while the container using the default comparator does not:

assert(5 == caseSensitiveSet.size());
assert(4 == caseInsensitiveSet.size());
size_type size() const BSLS_KEYWORD_NOEXCEPT
Return the number of elements in this set.
Definition bslstl_set.h:2725

Now, we observe the members of the case-sensitive set:

assert( caseSensitiveSet.count("NY"));
assert(!caseSensitiveSet.count("nY"));
assert(!caseSensitiveSet.count("Ny"));
assert( caseSensitiveSet.count("ny"));
assert( caseSensitiveSet.count("SF"));
assert(!caseSensitiveSet.count("sF"));
assert(!caseSensitiveSet.count("Sf"));
assert(!caseSensitiveSet.count("sf"));
size_type count(const key_type &key) const
Definition bslstl_set.h:1476

Finally, we observe that we can do case-insensitive access to caseInsensiveSet:

assert( caseInsensitiveSet.count("NY"));
assert( caseInsensitiveSet.count("nY"));
assert( caseInsensitiveSet.count("Ny"));
assert( caseInsensitiveSet.count("ny"));
assert( caseInsensitiveSet.count("LA"));
assert( caseInsensitiveSet.count("lA"));
assert( caseInsensitiveSet.count("La"));
assert( caseInsensitiveSet.count("la"));
assert( caseInsensitiveSet.count("nj"));
assert( caseInsensitiveSet.count("nJ"));
assert( caseInsensitiveSet.count("Nj"));
assert( caseInsensitiveSet.count("NJ"));
assert( caseInsensitiveSet.count("sf"));
assert( caseInsensitiveSet.count("sF"));
assert( caseInsensitiveSet.count("Sf"));
assert( caseInsensitiveSet.count("SF"));
assert(!caseInsensitiveSet.count("GA"));
assert(!caseInsensitiveSet.count("gA"));
assert(!caseInsensitiveSet.count("Ga"));
assert(!caseInsensitiveSet.count("ga"));