BDE 4.14.0 Production release
|
Provide utility functions on bsl::string_view
containers.
This component defines a utility struct
, bdlb::StringViewUtil
, that provides a suite of functions that operate on bsl::string_view
containers.
The bsl::string_view
class provides bsl::string
-like access to an array of bytes that need not be null terminated and that can have non-ASCII values (i.e., [128 .. 255]
). Although a bsl::string_view
object can itself be changed, it cannot change its referent data (the array of bytes). The lifetime of the referent data must exceed that of all bsl::string_view
objects referring to it. Equality comparison of bsl::string_view
objects compares the content of the referent data (not whether or not the object refer to the same array of bytes). See bslstl_stringview for full details.
The table below provides an outline of the functions provided by this component.
Since bsl::string_view
objects know the length of the referent data these utility functions can make certain performance improvements over the classic, similarly named C language functions.
These utilities assume ASCII encoding for character data when doing case conversions and when determining if a character is in the whitespace character set.
Caseless (i.e., case-insensitive) comparisons treat characters in the sequence [a .. z]
as equivalent to the respective characters in the sequence [A .. Z]
. This equivalence matches that of bsl::toupper
.
The following characters are classified as "whitespace":
This classification matches that of bsl::isspace
.
This section illustrates the intended use of this component.
Many applications must normalize user input by removing leading and trailing whitespace characters to obtain the essential text that is the intended input. Naturally, one would prefer to do this as efficiently as possible.
Suppose the response entered by a user is captured in rawInput
below:
First, for this pedagogical example, we copy the contents at rawInput
for later reference:
Then, we create a bsl::string_view
object referring to the raw data. Given a single argument of const char *
, the constructor assumes the data is a null-terminated string and implicitly calculates the length for the reference:
Now, we invoke the bdlb::StringViewUtil::trim
method to find the "Hello,
world!" sequence in rawInput
.
Finally, we observe the results:
Notice that, as expected, the textOfInterest
object refers to the "Hello,
world!" sub-sequence within the rawInput
byte array while the data at rawInput
remains unchanged.