Provide a case-insensitive less-than predicate for string views.
More...
Namespaces |
namespace | bdlb |
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::string
s or bsl::string_view
s 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";
Next, we create two containers, one with default comparator and another using bdlb::CstringLess
as a comparator: 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);
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());
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"));
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"));