BDE 4.14.0 Production release
|
Provide utility functions on bslstl::StringRef
-erenced strings.
bslstl::StringRef
stringsThis component defines a utility struct
, bdlb::StringRefUtil
, that provides a suite of functions that operate on bslstl::StringRef
references to string data.
The bslstl::StringRef
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 bslstl::StringRef
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 bslstl::StringRef
objects referring to it. Equality comparison of bslstl::StringRef
objects compares the content of the referent data (not whether or not the object refer to the same array of bytes). See bslstl_stringref for full details.
The table below provides an outline of the functions provided by this component.
Since bslstl::StringRef
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 bslstl::StringRef
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::StringRefUtil::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.