I need a method that helps me to find a string inside another substring, or in other words, find a string inside a subrange of other string. Besides, I need to find it in reverse order because I know that the string I'm looking for is closed to the end of the substring used as "haystack".
Let's suppose the following piece of code, where rfind_in_substr
is the method I'm asking for:
std::string sample("An example with the example word example trice");
// substring "ample with the example wo"
std::size_t substr_beg = 5;
std::size_t substr_size = 24;
// (1)
std::size_t pos = rfind_in_substr(sample, substr_beg,
substr_size, "example");
// pos == 20, because its the index of the start of the second
// "example" word inside the main string.
Of course, the line (1) could be replaced by:
std::size_t pos = substr_beg + sample.substr
(substr_beg, substr_size).rfind("example");
But that implies an unnecesary copy of the substring. Is there any method or C++/boost method that could help me doing that?
I was looking at boost::algorithm::string
library but I've found nothing (that I had understood). I know that C++17 has the std::string_view
class, that would be perfect, but I'm using C++14.
From Boost.StringAlgo:
Now, if you need to use this with other member functions from
std::string
, you need to do extra steps in converting a resulting range into an index like this answer does, but if you aren't, then simply use the range interface and avoid thestd::string
's "helpful" methods.Another option is to use
boost::string_ref
which is whatstd::string_view
is basically based on: