Skip to content

String View

DryPerspective edited this page Sep 20, 2023 · 3 revisions

A std::string_view implementation, following the same pattern as the standard with a dp::basic_string_view template and typedefs for the C++98 char types char and wchar_t. An object which contains a non-owning view of a string or contiguous character sequence. As with std::string_view, it is the programmer's responsibility to ensure that their dp::string_view does not outlive the data it is viewing.

Some liberties had to be taken on interface to preserve functionality - the standard typically processes and concatenates strings and views by converting std::string types to std::string_view types; and doing so as part of the interface for std::string. I, of course, modify the std::string types, so have to implement the elsewhere. This was achieved by adding a constructor and conversion operator for std::string types to dp::string_view, and defining the concatenation addition operators here.

Functions

begin
cbegin
Returns an iterator to the beginning
end
cend
Returns an iterator to the end
rbegin
crbegin
Returns a reverse iterator to the beginning
rend
crend
Returns a reverse iterator to the end
operator[] Access a specified element, without bounds checking
at Access a specified element, with bounds checking
front Access the first character.
back Access the last character
data Access the viewed char array. Not guaranteed to be null-terminated
size
length
Returns the number of characters in the view
max_size Returns the maximum possible number of characters a view can track
empty Check if the view is empty
remove_prefix Shrinks the view by moving the front forwards
remove suffix Shrinks the view by moving the end backwards
swap Swaps the contents of two views
copy Copies characters
substr Returns a substring
compare Compares two views
starts_with Checks if the string_view starts with the given prefix
ends_with Checks if the string_view ends with the given suffix
contains Checks if the string_view contains the given substring or character
find Find characters in a view
rfind Find the last occurrence of a substring
find_first_of Find the first occurrence of characters
find_last_of Find the last occurrence of characters
find_first_not_of Find the first absence of characters
find_last_not_of Find the last absence of characters
operator basic_string<CharT, Traits> Converts a string view into its equivalent std::string type
operator==
operator!=
operator<
operator<=
operator>
operator>=
Compares contents of two views according to Traits::compare()
operator<< Performs stream output on a view
operator+
operator+=
Concatenates string_views with each other and with std::string types

Sample Code

#include <string>
#include "cpp98/string_view"

//All operations in this function have no effect on the underlying string
void ExamineString(dp::string_view str){
    if(str.size() > 10) str.remove_suffix(str.size() - 10); //Shrink view to ten characters
    
    if(str.contains("foo")) Print("This string has foo in it");
    
    for(std::size_t i = 0; i < str.size(); ++i){
        Print(i + ' '); //Print the view with a space between each character
    }
}

int main(){
    const char* literal = "Hello world";
    std::string string = "Hello world";
    char C-array[] = "Hello world";

    //No conversions or allocations take place with this call.
    ExamineString(literal);
    ExamineString(string);
    ExamineString(C-array);
}

Clone this wiki locally