Ch9.13.1: String View Operations Reference

Overview

This appendix documents all string-view-specific operations. Most of these are also available on string (see Appendix §9.12.1). Return here when you need a particular feature.

1. npos

::fast_io::containers::npos (= SIZE_MAX) is returned by all find* operations when the search fails, and serves as the default count parameter meaning “to the end”.


#include <fast_io_dsal/string_view.h>
#include <fast_io.h>
using ::fast_io::containers::npos;

int main() {
    ::fast_io::string_view sv{"Hello, World!"};

    // npos as "not found"
    auto pos = sv.find_character('z');
    if (pos == npos) {
        ::fast_io::io::println("'z' not found");
    }

    // npos as "to end"
    auto tail = sv.subview(7zu); // default count = npos → "World!"
}

2. Subview Operations

Operation Description Example
subview(pos, count=npos) View of count characters starting at pos. sv.subview(7zu) → from index 7 to end
subview_front(count) View of the first count characters. sv.subview_front(5zu) → "Hello"
subview_back(count) View of the last count characters. sv.subview_back(6zu) → "World!"
copy(dest, count, pos=0) Copy count characters to raw buffer dest. sv.copy(buf, 5zu)

All subview* methods have _unchecked variants that skip bounds checking.

3. Search / Find Operations

Same API as string (see §9.12.1). All return npos when the target is not found.

Operation Description Example
find_character(ch, pos=0) Find first occurrence of character. sv.find_character('W') → 7
find_not_character(ch, pos=0) Find first character that is not ch. sv.find_not_character('H') → 1
rfind_character(ch, pos=npos) Find last occurrence of character. sv.rfind_character('l') → 10
rfind_not_character(ch, pos=npos) Find last character that is not ch. sv.rfind_not_character('!') → 11
find(view, pos=0) Find first occurrence of substring. sv.find("World") → 7
rfind(view, pos=0) Find last occurrence of substring. sv.rfind("l") → 10
find_first_of(view, pos) Find first character matching any in view. sv.find_first_of("aeiou") → 1 ('e')
find_first_not_of(view, pos) Find first character not matching any in view. sv.find_first_not_of("Helo") → 5 (',')
find_last_of(view, pos) Find last character matching any in view. sv.find_last_of("aeiou") → 8 ('o')
find_last_not_of(view, pos) Find last character not matching any in view. sv.find_last_not_of(" !") → 11 ('d')
contains_character(ch) Returns true if character exists. sv.contains_character('W') → true
contains(view) Returns true if substring exists. sv.contains("World") → true

4. starts_with / ends_with

Operation Description Example
starts_with(view) Returns true if view begins with view. sv.starts_with("Hello") → true
starts_with_character(ch) Returns true if first character is ch. sv.starts_with_character('H') → true
ends_with(view) Returns true if view ends with view. sv.ends_with("!") → true
ends_with_character(ch) Returns true if last character is ch. sv.ends_with_character('!') → true

5. Trim Operations (view-only)

These return a new view with characters removed — they do not modify the underlying data.

Operation Description
trim_subview_c_space() Return view with leading and trailing whitespace removed.
trim_prefix_subview_c_space() Return view with leading whitespace removed.
trim_suffix_subview_c_space() Return view with trailing whitespace removed.

Generic trim_subview(), trim_prefix_subview(), trim_suffix_subview() overloads accept a character category trait.

6. remove_prefix / remove_suffix

These shrink the view by advancing the start or reducing the end. They modify the view itself (not the underlying data).

Operation Description Example
remove_prefix(n) Advance the start by n characters. sv.remove_prefix(6zu);
remove_suffix(n) Shrink the end by n characters. sv.remove_suffix(1zu);
clear() Reset view to null pointer and size 0. sv.clear();

Both have _unchecked variants that skip bounds checking.

7. Comparison

String views support == and <=> (C++20 three-way comparison). Comparison is lexicographic.


::fast_io::string_view a{"abc"};
::fast_io::string_view b{"abd"};

bool eq = (a == b);          // false
auto cmp = (a <=> b);       // std::strong_ordering::less