Description
Hi, I have some thoughts on the edge case return values for the current search
API.
Currently, search
describes a failure to match as follows:
search(string, "substring") = start:end such that string[start:end] == "substring", or 0:-1 if unmatched.
search(string, 'c') = index such that string[index] == 'c', or 0 if unmatched.
This means that users of this API must handle the case of unmatching with some non-intuitive check. For example:
str = "haystack"
result = search(str, "needle")
if result == 0:-1 # or result[1] == 0 or first(result) == 0
# handle case where no result was found
else
# handle case where result was found
end
It turns out that first(result) == 0
is the condition that handles the three cases for when the object to search for (the needle) is either a string, a character, or a vector of characters since the API has two different return types depending on the the needle type.
The proposal here is to instead throw some type of exception e.g. UnmatchedException
.