We have several medium to large software projects targeting different platforms (arm32/64 and x86/amd64 on Linux and Windows) with a lot of duplicated code in them, since every project has its own split(string, delimiter)
, find_files(regex/pattern)
, and stuff like that.
Because of that we're fixing identical bugs over and over again in different projects and waste time inventing just another variant in the next project. So I'm working on small, platform "independent" libraries just for utility functions like these (string stuff, sockets, filesystem, time, ...)
Now to the actual problem.
The string library currently provides lots of functions taking std::string
as parameters and returning std::string
(or containers containing them).
I would like to replace them with std::string_view where appropriate. But I'm not sure where that would be appropriate and how.
Functions like
std::vector<std::string> split(const std::string& str, char delimiter);
could be
std::vector<std::string_view> split(std::string_view str, char delimiter);
The std::string_view
parameter makes sense here, but the return type would force callers to handle it differently, making it more complicated when the caller wants to store the results (i.e. needs std::string).
Does it make sense to provide multiple variants like
std::vector<std::string> split(std::string_view str, char delimiter);
std::vector<std::string_view> split_v(std::string_view str, char delimiter);
?
What should be the default? (i.e. would it make more sense to have split
return string_view
and provide a split_s
return string
?). Or is there a completely different way? What would be a good approach in general, regarding both performance and ease of use (and thus acceptance),and making it hard to misuse?
template <class T = std::string_view> std::vector<T> split(std::string_view s, char delim)
vector<string>
they can do that directly by assignment to the appropriate type.