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

Detailed Description

Outline

Purpose

Provide functions that operate on IndexSpan and string objects.

Classes

See also
bdlb_indexspan

Description

This component provides a struct, IndexSpanStringUtil, that serves as a namespace for utility functions that operate on IndexSpan and string objects. This component is designed to work on several representations of strings, including bsl::string, bsl::string_view, and it is also backwards compatible with bslstl::StringRef. Key operations of this utility include bind, for creating a bsl::string_view from an IndexSpan and a string, and the create methods that provide a way to create IndexSpan objects of a string object (of any kind) using positions defined by iterators and/or positions and/or length.

Usage

This section illustrates intended use of this component.

Example 1: Creating IndexSpan Objects Safely

Suppose that we are creating a parser, and we want the results of the parsing to be stored in IndexSpan objects. The parser will have either a pointer (or "begin" iterator) into the original string input and then another pointer (or iterator) or a length to identify the end of the input. Turning the beginning and ending identifiers into an IndexSpan is a simple calculation, but one that is verbose and potentially error prone. Instead of implementing the calculation ourselves we use the convenience function create from IndexSpanStringUtil.

First, we define a string that we want to parse:

const bsl::string full("James Tiberius Kirk");
Definition bslstl_string.h:1281

Then, we implement the parsing of the first name:

Cit it = bsl::find(full.begin(), full.end(), ' ');
// Error checking omitted since we know the string
full.begin(),
it);
Definition bdlb_indexspan.h:309
const CHAR_TYPE * const_iterator
Definition bslstl_string.h:1311
static IndexSpan create(const bsl::string_view &string, IndexSpan::size_type begin, IndexSpan::size_type length)
Definition bdlb_indexspanstringutil.h:725

Next, we implement the parsing of the middle name, this time using length, rather than an end iterator (demonstrating an alternative create overload provided by IndexSpanStringUtil):

++it; // Skip the space
Cit it2 = bsl::find(it, full.end(), ' ');
if (full.end() != it2) {
middle = bdlb::IndexSpanStringUtil::create(full, it, it2 - it);
it = it2 + 1;
}

Then, we create the IndexSpan for the last name, using two positions:

full,
it - full.begin(),
full.length());
static IndexSpan createFromPositions(const bsl::string_view &string, IndexSpan::size_type begin, IndexSpan::size_type end)
Definition bdlb_indexspanstringutil.h:707

Finally, we verify that the resulting IndexSpan objects correctly describe the parsed names of the full name:

assert(full.substr(first.position(), first.length()) == "James");
assert(full.substr(middle.position(), middle.length()) == "Tiberius");
assert(full.substr(last.position(), last.length()) == "Kirk");
size_type position() const
Return the position attribute.
Definition bdlb_indexspan.h:448
size_type length() const
Return the length attribute.
Definition bdlb_indexspan.h:442

Example 2: Creating String Views

Suppose that we have IndexSpan objects that define the first, middle, and last part of a string that has a full name in it and we want to get actual string views that correspond to those parts of the string. The bind functions of IndexSpanStringUtil provide that functionality. The bind functions return a bsl::string_view into the original string (so the characters of the string are not copied). Note that this example builds on Example 1.

First, we define a string view of the parsed string to show that bind works both on strings and string views:

const bsl::string_view fullView(full);
Definition bslstl_stringview.h:441

Then we demonstrate binding IndexSpan object to that view:

assert(bdlb::IndexSpanStringUtil::bind(fullView, first) == "James");
assert(bdlb::IndexSpanStringUtil::bind(fullView, middle) == "Tiberius");
assert(bdlb::IndexSpanStringUtil::bind(fullView, last) == "Kirk");
static bsl::string_view bind(const bsl::string_view &string, const IndexSpan &span)
Definition bdlb_indexspanstringutil.h:691

Finally we demonstrate binding IndexSpan object to a bsl::string:

assert(bdlb::IndexSpanStringUtil::bind(full, first) == "James");
assert(bdlb::IndexSpanStringUtil::bind(full, middle) == "Tiberius");
assert(bdlb::IndexSpanStringUtil::bind(full, last) == "Kirk");