Outline
Purpose
Provide a standard-compliant basic_string_view
class template.
Classes
Canonical header: bsl_string_view.h
- See also
- ISO C++ Standard, bdlb_stringviewutil
Description
This component defines a single class template bsl::basic_string_view
and aliases for ordinary and wide character specializations bsl::string_view
and bsl::wstring_view
implementing standard containers, std::string_view
and std::wstring_view
, that can refer to a constant contiguous sequence of char-like objects with the first element of the sequence at position zero.
An instantiation of basic_string_view
is a value-semantic type whose salient attribute is the sequence of characters it represents. The basic_string_view
class
is parameterized by the character type, CHAR_TYPE
and that character type's traits, CHAR_TRAITS
. The traits for each character type provide functions that assign, compare, and copy a sequence of those characters.
A basic_string_view
meets the requirements of a sequential container with random access iterators as specified in the [basic.string_view] section of the C++ standard [24.4]. The basic_string_view
implemented here adheres to the C++17 standard, except that it does not have template specializations std::u16string_view
and std::u32string_view
. Note that if compiler supports C++17 standard, then stl
implementation of basic_string_view
is used.
Lexicographical Comparisons
Two basic_string_view
s lhs
and rhs
are lexicographically compared by first determining N
, the smaller of the lengths of lhs
and rhs
, and comparing characters at each position between 0 and N - 1
, using CHAR_TRAITS::compare
in lexicographical fashion. If CHAR_TRAITS::compare
determines that string_views are non-equal (smaller or larger), then this is the result. Otherwise, the lengths of the string_views are compared and the shorter string_view is declared the smaller. Lexicographical comparison returns equality only when both string_views have the same length and the same character value in each respective position.
Operations
This section describes the run-time complexity of operations on instances of basic_string_view
:
Legend
------
'V' - the 'CHAR_TYPE' template parameter type of the
'basic_string_view'
'a', 'b' - two distinct objects of type 'basic_string_view<V>'
'k' - an integral number
'p' - a pointer defining a sequence of 'CHAR_TYPE' characters
+----------------------------------------------+--------------------------+
| Operation | Complexity |
|==============================================+==========================|
| basic_string_view<V> a (default construction)| O[1] |
|----------------------------------------------+--------------------------|
| basic_string_view<V> a(b) (copy construction)| O[1] |
|----------------------------------------------+--------------------------|
| basic_string_view<V> a(p) | O[n] |
|----------------------------------------------+--------------------------|
| basic_string_view<V> a(p, k) | O[1] |
|----------------------------------------------+--------------------------|
| a.~basic_string_view<V>() (destruction) | O[1] |
|----------------------------------------------+--------------------------|
| a.begin(), a.end(), | O[1] |
| a.cbegin(), a.cend(), | |
| a.rbegin(), a.rend(), | |
| a.crbegin(), a.crend() | |
|----------------------------------------------+--------------------------|
| a.size() | O[1] |
|----------------------------------------------+--------------------------|
| a.max_size() | O[1] |
|----------------------------------------------+--------------------------|
| a.remove_prefix(k) | O[1] |
| a.remove_suffix(k) | |
|----------------------------------------------+--------------------------|
| a[k] | O[1] |
|----------------------------------------------+--------------------------|
| a.at(k) | O[1] |
|----------------------------------------------+--------------------------|
| a.front() | O[1] |
|----------------------------------------------+--------------------------|
| a.back() | O[1] |
|----------------------------------------------+--------------------------|
| a.swap(b), swap(a, b) | O[1] |
|----------------------------------------------+--------------------------|
| a = b; (assignment) | O[1] |
|----------------------------------------------+--------------------------|
| a == b, a != b | O[n] |
|----------------------------------------------+--------------------------|
| a < b, a <= b, a > b, a >= b | O[n] |
+----------------------------------------------+--------------------------+
User-defined literals
The user-defined literal operators are declared for the bsl::string_view
and bsl::wstring_view
types. The ud-suffix _sv
is chosen to distinguish between the bsl
-string_view's user-defined literal operators and the std
-string_view's user-defined literal operator ""sv
introduced in the C++14 standard and implemented in the standard library provided by the compiler vendor. Note that the bsl
-string_view's operator "" _sv
, unlike the std
-string_view's operator ""sv
, can be used in a client's code if the compiler supports the C++11 standard. Also note that if the compiler supports the C++17 standard then the std
-string_view's operator ""sv
can be used to initialize a bsl
-string_view as follows:
using namespace std::string_view_literals;
Definition bslstl_stringview.h:441
Also note that bsl
-string_view's user-defined literal operators are declared in the bsl::literals::string_view_literals
namespace, where literals
and string_view_literals
are inline namespaces. Access to these operators can be gained with either using namespace bsl::literals
, using namespace bsl::string_view_literals
or using namespace bsl::literals::string_view_literals
. But we recommend using namespace bsl::string_view_literals
to minimize the scope of the using declaration:
using namespace bsl::string_view_literals;
Usage
In this section we show intended use of this component.
Example 1: Basic Syntax
The bsl::string_view
can be used as a lightweight replacement of the bsl::string
, unless you need to modify the content. It takes up no more space and doesn't allocate memory:
const char *LONG_STRING = "0123456789012345678901234567890123456789"
"0123456789012345678901234567890123456789";
assert(sfa.numBytesInUse() >= svfa.numBytesInUse());
assert(0 < ssa.numBytesInUse());
assert(0 == da.numBytesInUse());
Definition bslstl_string.h:1281
Definition bslma_defaultallocatorguard.h:186
Definition bslma_testallocator.h:384
At the same time it supports all most used access
operations of the bsl::string
, using the same interfaces:
assert(STR.
at(15) == SV.
at(15));
assert(STR.
find(
"345") == SV.
find(
"345"));
BSLS_KEYWORD_CONSTEXPR_CPP14 size_type find(basic_string_view subview, size_type position=0) const BSLS_KEYWORD_NOEXCEPT
Definition bslstl_stringview.h:2028
BSLS_KEYWORD_CONSTEXPR_CPP14 size_type find_last_not_of(basic_string_view subview, size_type position=npos) const BSLS_KEYWORD_NOEXCEPT
Definition bslstl_stringview.h:2354
BSLS_KEYWORD_CONSTEXPR size_type length() const BSLS_KEYWORD_NOEXCEPT
Return the length of this view.
Definition bslstl_stringview.h:1685
BSLS_KEYWORD_CONSTEXPR_CPP14 const_reference front() const
Definition bslstl_stringview.h:1738
BSLS_KEYWORD_CONSTEXPR_CPP17 int compare(basic_string_view other) const BSLS_KEYWORD_NOEXCEPT
Definition bslstl_stringview.h:1818
BSLS_KEYWORD_CONSTEXPR_CPP14 const_reference at(size_type position) const
Definition bslstl_stringview.h:1724
BSLS_KEYWORD_CONSTEXPR bool empty() const BSLS_KEYWORD_NOEXCEPT
Return true if this view has length 0, and false otherwise.
Definition bslstl_stringview.h:1703
size_type length() const BSLS_KEYWORD_NOEXCEPT
Definition bslstl_string.h:6601
int compare(const basic_string &other) const BSLS_KEYWORD_NOEXCEPT
Definition bslstl_string.h:7273
CHAR_TYPE & front()
Definition bslstl_string.h:5502
size_type find(const basic_string &substring, size_type position=0) const BSLS_KEYWORD_NOEXCEPT
Definition bslstl_string.h:6732
bool empty() const BSLS_KEYWORD_NOEXCEPT
Return true if this string has length 0, and false otherwise.
Definition bslstl_string.h:6631
size_type find_last_not_of(const basic_string &characterString, size_type position=npos) const BSLS_KEYWORD_NOEXCEPT
Definition bslstl_string.h:7118
reference at(size_type position)
Definition bslstl_string.h:5487
However, using the bsl::string_view
, you need to be especially attentive to the lifetime of the source character string, since the component explicitly refers to it:
assert(LONG_STRING != STR.
data());
assert(LONG_STRING == SV.
data());
sfa.deleteObject(sPtr);
svfa.deleteObject(svPtr);
BSLS_KEYWORD_CONSTEXPR const_pointer data() const BSLS_KEYWORD_NOEXCEPT
Definition bslstl_stringview.h:1760
CHAR_TYPE * data() BSLS_KEYWORD_NOEXCEPT
Definition bslstl_string.h:6477
◆ BSLSTL_STRINGVIEW_IDENTITY_USE_WRAPPER
#define BSLSTL_STRINGVIEW_IDENTITY_USE_WRAPPER 0 |