Template Struct basic_substring

Nested Relationships

Nested Types

Struct Documentation

template<class C>
struct basic_substring

a non-owning string-view, consisting of a character pointer and a length.

See also

to_substr()

See also

to_csubstr()

Note

The pointer is explicitly restricted.

Note

Because of a C++ limitation, there cannot coexist overloads for constructing from a char[N] and a char*; the latter will always be chosen by the compiler. To construct an object of this type, call to_substr() or to_csubstr(). For a more detailed explanation on why the overloads cannot coexist, see http://cplusplus.bordoon.com/specializeForCharacterArrays.html

Types

enum [anonymous]

Values:

enumerator npos
enumerator NONE
using CC = typename std::add_const<C>::type

CC=const char.

using NCC_ = typename std::remove_const<C>::type

NCC_=non const char.

using ro_substr = basic_substring<CC>
using rw_substr = basic_substring<NCC_>
using char_type = C
using size_type = size_t
using iterator = C*
using const_iterator = CC*
inline operator ro_substr() const

convert automatically to substring of const C

Splitting methods

using split_proxy = split_proxy_impl
inline bool next_split (C sep, size_t *C4_RESTRICT start_pos, basic_substring *C4_RESTRICT out) const

returns true if the string has not been exhausted yet, meaning it’s ok to call next_split() again. When no instance of sep exists in the string, returns the full string. When the input is an empty string, the output string is the empty string.

inline split_proxy split(C sep, size_t start_pos = 0) const

a view into the splits

inline basic_substring pop_right(C sep = C('/'), bool skip_empty = false) const

pop right: return the first split from the right. Use gpop_left() to get the reciprocal part.

inline basic_substring pop_left(C sep = C('/'), bool skip_empty = false) const

return the first split from the left. Use gpop_right() to get the reciprocal part.

inline basic_substring gpop_left(C sep = C('/'), bool skip_empty = false) const

greedy pop left. eg, csubstr(“a/b/c”).gpop_left(‘/’)=”c”

inline basic_substring gpop_right(C sep = C('/'), bool skip_empty = false) const

greedy pop right. eg, csubstr(“a/b/c”).gpop_right(‘/’)=”a”

Content-modification methods (only for non-const C)

size_t ifirst = 0
inline  C4_REQUIRE_RW (void) toupper()

convert the string to upper-case

Note

this method requires that the string memory is writeable and is SFINAEd out for const C

inline  C4_REQUIRE_RW (void) tolower()

convert the string to lower-case

Note

this method requires that the string memory is writeable and is SFINAEd out for const C

inline  C4_REQUIRE_RW (void) fill(C val)

fill the entire contents with the given val

Note

this method requires that the string memory is writeable and is SFINAEd out for const C

C4_REQUIRE_RW (void) copy_from(ro_substr that

set the current substring to a copy of the given csubstr

Note

this method requires that the string memory is writeable and is SFINAEd out for const C

Default construction and assignment

inline constexpr basic_substring()
constexpr basic_substring(basic_substring const&) = default
constexpr basic_substring(basic_substring&&) = default
inline constexpr basic_substring(std::nullptr_t)
basic_substring &operator=(basic_substring const&) = default
basic_substring &operator=(basic_substring&&) = default
inline basic_substring &operator=(std::nullptr_t)

Construction and assignment from characters with the same type

template<size_t N>
inline constexpr basic_substring(C (&s_)[N]) noexcept

the overload for receiving a single C* pointer will always hide the array[N] overload. So it is disabled. If you want to construct a substr from a single pointer containing a C-style string, you can call c4::to_substr()/c4to_csubstr().

See also

c4::to_substr()

See also

c4::to_csubstr()

inline basic_substring(C *s_, size_t len_)
inline basic_substring(C *beg_, C *end_)
template<size_t N>
inline basic_substring &operator=(C (&s_)[N])
template<size_t N>
inline void assign(C (&s_)[N])

the overload for receiving a single C* pointer will always hide the array[N] overload. So it is disabled. If you want to construct a substr from a single pointer containing a C-style string, you can call c4::to_substr()/c4to_csubstr().

See also

c4::to_substr()

See also

c4::to_csubstr()

inline void assign(C *s_, size_t len_)
inline void assign(C *beg_, C *end_)
inline void clear()

Construction from non-const characters

template<size_t N, class U = NCC_> inline explicit  basic_substring (C4_NC2C(U)(&s_)[N])

only available when the char type is const

template<class U = NCC_> inline  basic_substring (C4_NC2C(U) *s_, size_t len_)

only available when the char type is const

template<class U = NCC_> inline  basic_substring (C4_NC2C(U) *beg_, C4_NC2C(U) *end_)

only available when the char type is const

template<size_t N, class U = NCC_> inline void assign (C4_NC2C(U)(&s_)[N])

only available when the char type is const

template<class U = NCC_> inline void assign (C4_NC2C(U) *s_, size_t len_)

only available when the char type is const

template<class U = NCC_> inline void assign (C4_NC2C(U) *beg_, C4_NC2C(U) *end_)

only available when the char type is const

template<size_t N, class U = NCC_> inline basic_substring & operator= (C4_NC2C(U)(&s_)[N])

only available when the char type is const

Standard accessor methods

inline C4_ALWAYS_INLINE C4_PURE bool has_str () const noexcept
inline C4_ALWAYS_INLINE C4_PURE bool empty () const noexcept
inline C4_ALWAYS_INLINE C4_PURE bool not_empty () const noexcept
inline C4_ALWAYS_INLINE C4_PURE size_t size () const noexcept
inline C4_ALWAYS_INLINE C4_PURE iterator begin () noexcept
inline C4_ALWAYS_INLINE C4_PURE iterator end () noexcept
inline C4_ALWAYS_INLINE C4_PURE const_iterator begin () const noexcept
inline C4_ALWAYS_INLINE C4_PURE const_iterator end () const noexcept
inline C4_ALWAYS_INLINE C4_PURE C * data () noexcept
inline C4_ALWAYS_INLINE C4_PURE C const  * data () const noexcept
inline C4_ALWAYS_INLINE C4_PURE C & operator[] (size_t i) noexcept
inline C4_ALWAYS_INLINE C4_PURE C const  & operator[] (size_t i) const noexcept
inline C4_ALWAYS_INLINE C4_PURE C & front () noexcept
inline C4_ALWAYS_INLINE C4_PURE C const  & front () const noexcept
inline C4_ALWAYS_INLINE C4_PURE C & back () noexcept
inline C4_ALWAYS_INLINE C4_PURE C const  & back () const noexcept

Comparison methods

inline C4_PURE int compare (C const c) const noexcept
inline C4_PURE int compare (const char *C4_RESTRICT that, size_t sz) const noexcept
inline C4_ALWAYS_INLINE C4_PURE int compare (ro_substr const that) const noexcept
inline C4_ALWAYS_INLINE C4_PURE bool operator== (std::nullptr_t) const noexcept
inline C4_ALWAYS_INLINE C4_PURE bool operator!= (std::nullptr_t) const noexcept
inline C4_ALWAYS_INLINE C4_PURE bool operator== (C const c) const noexcept
inline C4_ALWAYS_INLINE C4_PURE bool operator!= (C const c) const noexcept
inline C4_ALWAYS_INLINE C4_PURE bool operator< (C const c) const noexcept
inline C4_ALWAYS_INLINE C4_PURE bool operator> (C const c) const noexcept
inline C4_ALWAYS_INLINE C4_PURE bool operator<= (C const c) const noexcept
inline C4_ALWAYS_INLINE C4_PURE bool operator>= (C const c) const noexcept
template<class U> inline C4_ALWAYS_INLINE C4_PURE bool operator== (basic_substring< U > const that) const noexcept
template<class U> inline C4_ALWAYS_INLINE C4_PURE bool operator!= (basic_substring< U > const that) const noexcept
template<class U> inline C4_ALWAYS_INLINE C4_PURE bool operator< (basic_substring< U > const that) const noexcept
template<class U> inline C4_ALWAYS_INLINE C4_PURE bool operator> (basic_substring< U > const that) const noexcept
template<class U> inline C4_ALWAYS_INLINE C4_PURE bool operator<= (basic_substring< U > const that) const noexcept
template<class U> inline C4_ALWAYS_INLINE C4_PURE bool operator>= (basic_substring< U > const that) const noexcept
template<size_t N> inline C4_ALWAYS_INLINE C4_PURE bool operator== (const char(&that)[N]) const noexcept
template<size_t N> inline C4_ALWAYS_INLINE C4_PURE bool operator!= (const char(&that)[N]) const noexcept
template<size_t N> inline C4_ALWAYS_INLINE C4_PURE bool operator< (const char(&that)[N]) const noexcept
template<size_t N> inline C4_ALWAYS_INLINE C4_PURE bool operator> (const char(&that)[N]) const noexcept
template<size_t N> inline C4_ALWAYS_INLINE C4_PURE bool operator<= (const char(&that)[N]) const noexcept
template<size_t N> inline C4_ALWAYS_INLINE C4_PURE bool operator>= (const char(&that)[N]) const noexcept

Sub-selection methods

inline C4_ALWAYS_INLINE C4_PURE bool is_sub (ro_substr const that) const noexcept

true if *this is a substring of that (ie, from the same buffer)

inline C4_ALWAYS_INLINE C4_PURE bool is_super (ro_substr const that) const noexcept

true if that is a substring of *this (ie, from the same buffer)

inline C4_ALWAYS_INLINE C4_PURE bool overlaps (ro_substr const that) const noexcept

true if there is overlap of at least one element between that and *this

inline C4_ALWAYS_INLINE C4_PURE basic_substring sub (size_t first) const noexcept

return [first,len[

inline C4_ALWAYS_INLINE C4_PURE basic_substring sub (size_t first, size_t num) const noexcept

return [first,first+num[. If num==npos, return [first,len[

inline C4_ALWAYS_INLINE C4_PURE basic_substring range (size_t first, size_t last=npos) const noexcept

return [first,last[. If last==npos, return [first,len[

inline C4_ALWAYS_INLINE C4_PURE basic_substring first (size_t num) const noexcept

return the first num elements: [0,num[

inline C4_ALWAYS_INLINE C4_PURE basic_substring last (size_t num) const noexcept

return the last @num elements: [len-num,len[

inline C4_ALWAYS_INLINE C4_PURE basic_substring offs (size_t left, size_t right) const noexcept

offset from the ends: return [left,len-right[ ; ie, trim a number of characters from the left and right. This is equivalent to python’s negative list indices.

inline C4_ALWAYS_INLINE C4_PURE basic_substring left_of (size_t pos) const noexcept

return [0, pos[ . Same as .first(pos), but provided for compatibility with .right_of()

inline C4_ALWAYS_INLINE C4_PURE basic_substring left_of (size_t pos, bool include_pos) const noexcept

return [0, pos+include_pos[ . Same as .first(pos+1), but provided for compatibility with .right_of()

inline C4_ALWAYS_INLINE C4_PURE basic_substring right_of (size_t pos) const noexcept

return [pos+1, len[

inline C4_ALWAYS_INLINE C4_PURE basic_substring right_of (size_t pos, bool include_pos) const noexcept

return [pos+!include_pos, len[

inline C4_ALWAYS_INLINE C4_PURE basic_substring left_of (ro_substr const subs) const noexcept

given subs a substring of the current string, get the portion of the current string to the left of it

inline C4_ALWAYS_INLINE C4_PURE basic_substring right_of (ro_substr const subs) const noexcept

given subs a substring of the current string, get the portion of the current string to the right of it

Removing characters (trim()) / patterns (strip()) from the tips of the string

inline basic_substring triml(const C c) const

trim left

inline basic_substring triml(ro_substr chars) const

trim left ANY of the characters.

See also

stripl() to remove a pattern from the left

inline basic_substring trimr(const C c) const

trim the character c from the right

inline basic_substring trimr(ro_substr chars) const

trim right ANY of the characters

See also

stripr() to remove a pattern from the right

inline basic_substring trim(const C c) const

trim the character c left and right

inline basic_substring trim(ro_substr const chars) const

trim left and right ANY of the characters

See also

strip() to remove a pattern from the left and right

inline basic_substring stripl(ro_substr pattern) const

remove a pattern from the left

See also

triml() to remove characters

inline basic_substring stripr(ro_substr pattern) const

remove a pattern from the right

See also

trimr() to remove characters

Lookup methods

inline size_t find(const C c, size_t start_pos = 0) const
inline size_t find(ro_substr pattern, size_t start_pos = 0) const
inline size_t count(const C c, size_t pos = 0) const

count the number of occurrences of c

inline size_t count(ro_substr c, size_t pos = 0) const

count the number of occurrences of s

inline basic_substring select(const C c, size_t pos = 0) const

get the substr consisting of the first occurrence of c after pos, or an empty substr if none occurs

inline basic_substring select(ro_substr pattern, size_t pos = 0) const

get the substr consisting of the first occurrence of pattern after pos, or an empty substr if none occurs

inline first_of_any_result first_of_any(ro_substr s0, ro_substr s1) const
inline first_of_any_result first_of_any(ro_substr s0, ro_substr s1, ro_substr s2) const
inline first_of_any_result first_of_any(ro_substr s0, ro_substr s1, ro_substr s2, ro_substr s3) const
inline first_of_any_result first_of_any(ro_substr s0, ro_substr s1, ro_substr s2, ro_substr s3, ro_substr s4) const
template<class It>
inline first_of_any_result first_of_any_iter(It first_span, It last_span) const
inline bool begins_with(const C c) const

true if the first character of the string is c

inline bool begins_with(const C c, size_t num) const

true if the first num characters of the string are c

inline bool begins_with(ro_substr pattern) const

true if the string begins with the given pattern

inline bool begins_with_any(ro_substr chars) const

true if the first character of the string is any of the given chars

inline bool ends_with(const C c) const

true if the last character of the string is c

inline bool ends_with(const C c, size_t num) const

true if the last num characters of the string are c

inline bool ends_with(ro_substr pattern) const

true if the string ends with the given pattern

inline bool ends_with_any(ro_substr chars) const

true if the last character of the string is any of the given chars

inline size_t first_of(const C c, size_t start = 0) const
Returns:

the first position where c is found in the string, or npos if none is found

inline size_t last_of(const C c, size_t start = npos) const
Returns:

the last position where c is found in the string, or npos if none is found

inline size_t first_of(ro_substr chars, size_t start = 0) const
Returns:

the first position where ANY of the chars is found in the string, or npos if none is found

inline size_t last_of(ro_substr chars, size_t start = npos) const
Returns:

the last position where ANY of the chars is found in the string, or npos if none is found

inline size_t first_not_of(const C c, size_t start = 0) const
inline size_t last_not_of(const C c, size_t start = npos) const
inline size_t first_not_of(ro_substr chars, size_t start = 0) const
inline size_t last_not_of(ro_substr chars, size_t start = npos) const

Range lookup methods

inline basic_substring pair_range(CC open, CC close) const

get the range delimited by an open-close pair of characters.

Note

There must be no nested pairs.

Note

No checks for escapes are performed.

inline basic_substring pair_range_esc(CC open_close, CC escape = CC('\\'))

get the range delimited by a single open-close character (eg, quotes).

Note

The open-close character can be escaped.

inline basic_substring pair_range_nested(CC open, CC close) const

get the range delimited by an open-close pair of characters, with possibly nested occurrences. No checks for escapes are performed.

inline basic_substring unquoted() const

Number-matching query methods

inline bool is_number() const

Note

any leading or trailing whitespace will return false.

Returns:

true if the substring contents are a floating-point or integer number.

inline bool is_real() const

Note

any leading or trailing whitespace will return false.

Returns:

true if the substring contents are a real number.

inline bool is_integer() const

Note

any leading or trailing whitespace will return false.

Returns:

true if the substring contents are an integer number.

inline bool is_unsigned_integer() const

Note

any leading or trailing whitespace will return false.

Returns:

true if the substring contents are an unsigned integer number.

inline basic_substring first_non_empty_span() const

get the first span consisting exclusively of non-empty characters

inline basic_substring first_uint_span() const

get the first span which can be interpreted as an unsigned integer

inline basic_substring first_int_span() const

get the first span which can be interpreted as a signed integer

inline basic_substring _first_integral_span(size_t skip_start) const
inline basic_substring first_real_span() const

get the first span which can be interpreted as a real (floating-point) number

inline C4_NO_INLINE C4_PURE basic_substring _word_follows (size_t pos, csubstr word) const noexcept
inline C4_NO_INLINE C4_PURE basic_substring _first_real_span_dec (size_t pos) const noexcept
inline C4_NO_INLINE C4_PURE basic_substring _first_real_span_hex (size_t pos) const noexcept
inline C4_NO_INLINE C4_PURE basic_substring _first_real_span_bin (size_t pos) const noexcept
inline C4_NO_INLINE C4_PURE basic_substring _first_real_span_oct (size_t pos) const noexcept
static inline constexpr C4_ALWAYS_INLINE C4_CONST bool _is_delim_char (char c) noexcept

true if the character is a delimiter character at the end

static inline constexpr C4_ALWAYS_INLINE C4_CONST bool _is_hex_char (char c) noexcept

true if the character is in [0-9a-fA-F]

Path-like manipulation methods

inline basic_substring basename(C sep = C('/')) const
inline basic_substring dirname(C sep = C('/')) const
inline C4_ALWAYS_INLINE basic_substring name_wo_extshort () const
inline C4_ALWAYS_INLINE basic_substring name_wo_extlong () const
inline C4_ALWAYS_INLINE basic_substring extshort () const
inline C4_ALWAYS_INLINE basic_substring extlong () const

Public Members

C *C4_RESTRICT str

a restricted pointer to the first character of the substring

size_t len

the length of the substring

struct first_of_any_result

Public Functions

inline operator bool() const

Public Members

size_t which
size_t pos