Flow 1.0.1
Flow project: Public API.
Public Types | Public Member Functions | List of all members
flow::util::Basic_string_view< Ch, Traits > Class Template Reference

Essentially alias for a C++17-conforming string-view class template, which is a very lightweight std::string-like representation of a character sequence already in memory. More...

#include <string_view.hpp>

Inherits std::basic_string_view< Char >.

Public Types

using Base = std::basic_string_view< Ch, Traits >
 Short-hand for the base. We add no data of our own in this subclass, just a handful of APIs.
 

Public Member Functions

constexpr Basic_string_view () noexcept
 Constructs null view: identical to Base API.
 
constexpr Basic_string_view (Ch const *s, size_t count)
 Constructs view to string at given start location and length: identical to Base API. More...
 
constexpr Basic_string_view (Ch const *s)
 Constructs view to given NUL-terminated string: identical to Base API. More...
 
constexpr Basic_string_view (const Basic_string_view &s) noexcept
 Boring copy constructor: identical to Base API. More...
 
constexpr Basic_string_view (Base s)
 Identical to copy constructor but converts from a vanilla Base. More...
 
 Basic_string_view (const std::basic_string< Ch, Traits > &s)
 Constructs view directly into a std::basic_string (e.g., std::string). More...
 
constexpr Basic_string_viewoperator= (const Basic_string_view &s) noexcept
 Boring copy assignment: identical to Base API. More...
 
bool starts_with (Basic_string_view needle) const
 Equivalent to C++20 basic_string_view::starts_with() which is lacking in C++17 but present in C++20 and previously-used-in-Flow Boost equivalent string_view. More...
 
bool starts_with (Ch const *needle) const
 Equivalent to C++20 basic_string_view::starts_with() which is lacking in C++17 but present in C++20 and previously-used-in-Flow Boost equivalent string_view. More...
 
bool starts_with (Ch needle) const
 Equivalent to C++20 basic_string_view::starts_with() which is lacking in C++17 but present in C++20 and previously-used-in-Flow Boost equivalent string_view. More...
 
bool ends_with (Basic_string_view needle) const
 Equivalent to C++20 basic_string_view::ends_with() which is lacking in C++17 but present in C++20 and previously-used-in-Flow Boost equivalent string_view. More...
 
bool ends_with (Ch const *needle) const
 Equivalent to C++20 basic_string_view::ends_with() which is lacking in C++17 but present in C++20 and previously-used-in-Flow Boost equivalent string_view. More...
 
bool ends_with (Ch needle) const
 Equivalent to C++20 basic_string_view::ends_with() which is lacking in C++17 but present in C++20 and previously-used-in-Flow Boost equivalent string_view. More...
 

Detailed Description

template<typename Ch, typename Traits = std::char_traits<Ch>>
class flow::util::Basic_string_view< Ch, Traits >

Essentially alias for a C++17-conforming string-view class template, which is a very lightweight std::string-like representation of a character sequence already in memory.

As of March 2022 the alias is formally to std::basic_string_view (unlike in the past, when that was not available – until C++17). However the alias remains to, at least, avoid breaking user code – by providing the String_view related alias. In addition this API-only subclass adds starts_with() and ends_with() which implement the common-sense C++20 basic_string_view operations starts_with() and ends_width(). These existed in the Boost basic_string_view that, pre-C++17, used to be the String_view alias target (when Ch is char). The downgrade due to their lack of inclusion in C++17 (which inaugurated *string_view in C++ STL) is unfortunate; but these replacements are fine.

See also
String_view, which is to std::string_view what the present template is to std::basic_string_view. I.e., when working with char string sequences, which is typical, use String_view.

Rationale

Why is this even a thing? Why not use std::basic_string_view and std::string_view all over? Answer: It is mostly historic. Flow was C++14 until ~3/2022. std::string_view was added in C++17. Before then, String_view aliased to boost::string_view (another possibility would have been boost::string_ref – same API – long story), because it was available for C++14. Now that we're on C++17, it's better to use the std thing, especially since many STL (and Boost, nowadays) APIs take std::string_view directly. Hence the alias changed as planned all along.

However the type alias remains. Pragmatically: much code uses String_view, even outside Flow, and there's no harm in an alias, particularly since we add the nice starts_with() and ends_with(). The alias was actively useful before; now removing it is a breaking change.

Implementation/API note

It would have been ideal to define the various constructors by simply inheriting all of std::basic_string_view ctors via the single statement using Base::Base. However, at least with our gcc, it appears to cause some issue in propagating constexprness of these constructors (which is effectively used, at least, by the flow::log macros to make certain optimizations possible): compile errors. To avoid this and various conversion issues, a number of constructors are explicitly (re)defined.

Constructor & Destructor Documentation

◆ Basic_string_view() [1/5]

template<typename Ch , typename Traits >
constexpr flow::util::Basic_string_view< Ch, Traits >::Basic_string_view ( Ch const *  s,
size_t  count 
)
constexpr

Constructs view to string at given start location and length: identical to Base API.

Parameters
sSee Base API.
countSee Base API.

◆ Basic_string_view() [2/5]

template<typename Ch , typename Traits >
constexpr flow::util::Basic_string_view< Ch, Traits >::Basic_string_view ( Ch const *  s)
constexpr

Constructs view to given NUL-terminated string: identical to Base API.

Parameters
sSee Base API.

◆ Basic_string_view() [3/5]

template<typename Ch , typename Traits >
constexpr flow::util::Basic_string_view< Ch, Traits >::Basic_string_view ( const Basic_string_view< Ch, Traits > &  s)
constexprdefaultnoexcept

Boring copy constructor: identical to Base API.

Parameters
sSee Base API.

◆ Basic_string_view() [4/5]

template<typename Ch , typename Traits >
constexpr flow::util::Basic_string_view< Ch, Traits >::Basic_string_view ( Base  s)
constexpr

Identical to copy constructor but converts from a vanilla Base.

Parameters
sSee copy constructor.

◆ Basic_string_view() [5/5]

template<typename Ch , typename Traits >
flow::util::Basic_string_view< Ch, Traits >::Basic_string_view ( const std::basic_string< Ch, Traits > &  s)

Constructs view directly into a std::basic_string (e.g., std::string).

basic_string has a conversion operator to basic_string_view but technically not to our slight embellishment thereof, Basic_string_view.

Parameters
sSelf-explanatory.

Member Function Documentation

◆ ends_with() [1/3]

template<typename Ch , typename Traits >
bool flow::util::Basic_string_view< Ch, Traits >::ends_with ( Basic_string_view< Ch, Traits >  needle) const

Equivalent to C++20 basic_string_view::ends_with() which is lacking in C++17 but present in C++20 and previously-used-in-Flow Boost equivalent string_view.

Parameters
needlePossible postfix within *this.
Returns
Whether *this contains needle as postfix.

◆ ends_with() [2/3]

template<typename Ch , typename Traits >
bool flow::util::Basic_string_view< Ch, Traits >::ends_with ( Ch const *  needle) const

Equivalent to C++20 basic_string_view::ends_with() which is lacking in C++17 but present in C++20 and previously-used-in-Flow Boost equivalent string_view.

Parameters
needlePossible postfix within *this.
Returns
Whether *this contains needle as postfix.

◆ ends_with() [3/3]

template<typename Ch , typename Traits >
bool flow::util::Basic_string_view< Ch, Traits >::ends_with ( Ch  needle) const

Equivalent to C++20 basic_string_view::ends_with() which is lacking in C++17 but present in C++20 and previously-used-in-Flow Boost equivalent string_view.

Parameters
needlePossible postfix within *this.
Returns
Whether *this contains needle as postfix.

◆ operator=()

template<typename Ch , typename Traits >
constexpr Basic_string_view< Ch, Traits > & flow::util::Basic_string_view< Ch, Traits >::operator= ( const Basic_string_view< Ch, Traits > &  s)
constexprdefaultnoexcept

Boring copy assignment: identical to Base API.

Parameters
sSee Base API.
Returns
See Base API.

◆ starts_with() [1/3]

template<typename Ch , typename Traits >
bool flow::util::Basic_string_view< Ch, Traits >::starts_with ( Basic_string_view< Ch, Traits >  needle) const

Equivalent to C++20 basic_string_view::starts_with() which is lacking in C++17 but present in C++20 and previously-used-in-Flow Boost equivalent string_view.

Parameters
needlePossible prefix within *this.
Returns
Whether *this contains needle as prefix.

◆ starts_with() [2/3]

template<typename Ch , typename Traits >
bool flow::util::Basic_string_view< Ch, Traits >::starts_with ( Ch const *  needle) const

Equivalent to C++20 basic_string_view::starts_with() which is lacking in C++17 but present in C++20 and previously-used-in-Flow Boost equivalent string_view.

Parameters
needlePossible prefix within *this.
Returns
Whether *this contains needle as prefix.

◆ starts_with() [3/3]

template<typename Ch , typename Traits >
bool flow::util::Basic_string_view< Ch, Traits >::starts_with ( Ch  needle) const

Equivalent to C++20 basic_string_view::starts_with() which is lacking in C++17 but present in C++20 and previously-used-in-Flow Boost equivalent string_view.

Parameters
needlePossible prefix within *this.
Returns
Whether *this contains needle as prefix.

The documentation for this class was generated from the following file: