Analyzed according to its documentation, TextHelper::excerpt excerpts 1 character too many after the matching phrase in the text. A side effect of this is for example:
>> excerpt 'ab', 'a', 0
=> "ab..."
Documentation provides examples consistent with this behavior, contradicting the description of the function itself. Tests are consistent with this behavior too, but contradict the description in the same way.
Provided is a patch that
- fixes the documentation,
- fixes the function itself
- fixes the existing tests
- adds another test to test behavior in borderline cases (such as the example above).
Additionally, the text "The resulting string will be stripped in any case." is added to the documentation to document this peculiar behavior, for example:
>> excerpt(" zabcd ", "b", 3)
=> "zabcd"
Whether this behavior is meaningful or desired can be discussed, but I thought it should at least be documented. (In my opinion the string should only be lstripped or rstripped if the real excerpt_string is being prepended or appended, respectively)
I ran the tests with Ruby 1.8.6, if someone with 1.9 could test this, I'd be thankful (I modified the 1.9 version of excerpt and tests blindly).