I’m making an indexOfReverse utility function in my C++ program, and I have caught a bit of a snag. Implementing the offset and maxOffset in my indexOf was very intuitive to me.. These start from the beginning of the string (+offset), and go forth to the end of the string (or maxOffset if given).
While implementing offsets for the indexOfReverse, though, I was wondering what the most logical way to do this is? Should the offset be from the beginning of the string, or end of the string? Is there an established norm for this I am missing somewhere? I tried explaining it to my duck, but he just looked at me funny. I’m leaning towards thinking of it from the beginning, because of its ease-of-use being more valuable to me than its counter-intuitiveness.. considering the offset from the end just makes things harder to think about in my head.
I have looked at how it’s done in a string library for C++, PHP, and Python, and what seems common in the description of each is finding the last of occurrence of a string where the offset is considered from the beginning, rather than doing true reverse-string searching… But I desire true reverse-string searching rather than last occurrence ofs. Is there a most logical way to consider this, though, so I can move on?
0
Take a look at how it’s done in the C++ standard library: string::rfind
. Better yet, just use that function instead of writing your own.
They take offsets from the beginning of the string. Doing so makes it easier to use the result in, say, substr
:
string str = "I haven't done C++ in a while.";
size_t last_in = str.rfind("in");
string last_phrase = str.substr(last_in); // "in a while."
There’s actually two decisions to make for an rfind
function:
-
Should the result index be based on the beginning of the haystack?
I haven't done C++ in a while. 0123456789
or the end of it?
I haven't done C++ in a while. 9876543210
-
Should the result index be the beginning of the found result?
I haven't done C++ in a while. ^
or the end of it?
I haven't done C++ in a while. ^
If we answer “end” to either of these questions, we have to answer one more question: does “end” mean the last character?
in
^
or the character after it?
in
^
The latter is more consistent with the regime of beginning-based indexes.
1
For most string users, measuring the offset from the beginning of the string regardless of the method called would make the most sense.
Recognize that when a user wants to get the last occurrence of something, it does not imply that the user have a logical use of the “reversed string”. If it were so, the user could have simply done a reverse
first, and proceed with whatever operation desired. Rather, the user is more likely interested in splitting the string into three or more parts:
Mr. Thomas A. Anderson
<<<|------|>>>>>>>>>>>
Namely, everything before, the matched substring, and everything after.
Because of this, there is a need to pass the string offsets obtained from one method into another method, between find
and rfind
. Having two definitions just complicates the matter.
A more nuanced approach would be to define an enumerator
of matches, a.k.a. Regex
. This enumerator allows navigating any number of matches. For each occurrence, the positions of the first character and last character can be queried.
2