generic indexOf() for arrays ?
Jonathan M Davis
jmdavisProg at gmx.com
Fri Apr 27 21:38:51 PDT 2012
On Saturday, April 28, 2012 12:18:57 Ary Manzana wrote:
> But isn't it the same funcionality? Why use a different name for the
> same funcionality?
It's _not_ the same functionality, even if it's similar.
std.string.indexOf varies drastically from std.algorithm.countUntil and in a
way that easily leads to confusion if they're both named indexOf.
std.array.indexOf returns the index of the string that you passed in. That
number doesn't say _anything_ about how many characters are before that sub-
string. If str.indexOf("hello") returns 6, that's index 6, but it could be the
third character rather than the sixth, because strings are UTF-8 and therefore
variably-lengthed encoded.
std.algorithm.countUntil returns the number of elements in the range up to the
range passed in and does not necessarily have any relation with any index
(particularly if the range isn't random access). In the case of
str.countUntil("hello"), it will return one minus the number of the character
that "hello" starts at. So, if it's the third character, then the result will
be 2, whereas the result of std.string.indexOf could have been 6.
Also, the result of indexOf isn't necessarily an index (particularly in the
case of strings - but also in the case where a range is not random access), so
calling countUntil indexOf is arguably a poor choice.
- Jonathan M Davis
More information about the Digitalmars-d
mailing list