purescript/purescript-strings

lastIndexOf' does something odd with indices

paf31 opened this issue · 4 comments

Apparently that is the intended behavior. From MDN:

fromIndex (Optional)
The index at which to start searching backwards in the string. It can be any integer. The default value is str.length - 1, so the whole array is searched. If fromIndex >= str.length, the whole string is searched. If fromIndex < 0, the behavior will be the same as if it would be 0.

So we should probably just document it.

Or are you referring to the fact that it does not yield Nothing if the index is equal to the string length (i.e. out of bounds):

exports["_lastIndexOf'"] = function (just) {
  return function (nothing) {
    return function (x) {
      return function (startAt) {
        return function (s) {
          if (startAt < 0 || startAt > s.length) return nothing;
          var i = s.lastIndexOf(x, startAt);
          return i === -1 ? nothing : just(i);
        };
      };
    };
  };
};

I think this should probably be changed to startAt > s.length - 1.

I have looked at this again, and it actually seems fine to me like it is. The index thing I mentioned in the last post still seems weird to me, but it makes sense when looking at the test cases (i.e. if you search for an empty string).

In my opinion, this issue can be closed.

I have to say, though, that I would be completely fine with removing this function alltogether (same holds for indexOf').

The indexOf' varieties are much faster than dropping from a string and then searching again, but maybe you're right... I've never used them myself, although I think I did add them originally.