While prototyping and playing around in C++, trying some concepts for making a utf8-aware immutable string, I stood with the following dilemma:
Is there any way to return a immutable view of a string. Like, instead of returning a substring, I want to be able to return a substring that references a part of the original string.
// Just some quick prototyping of ideas.
// Heavier than just a normal string.
// Construction would be heavier too because of the indices vector.
// Size would end up being O1 though.
// Indexing would also be faster.
struct ustring {
std::string data;
std::vector<size_t> indices;
// How do I return a view to a string?
std::string operator [](size_t const i) const {
return data.substr(indices[i], indices[i + 1] - indices[i]);
}
};
string_viewclass available to you from c++17?<experimental/string_view>in c++14, and before that, boost had a string_view library. You could also use the GSL.