Skip to content

Commit

Permalink
fix(stringx) rpartition returned bad order args if not found
Browse files Browse the repository at this point in the history
fixes #299
  • Loading branch information
Tieske committed May 29, 2019
1 parent 772c7ec commit f7209be
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
- `dir.copyfile`, `dir.movefile`, and `dir.makepath` create the new file/path with
the requested casing, and no longer force lowercase (only impacted Windows)
- added a missing assertion on `path.getmtime` [#291](https://github.com/stevedonovan/Penlight/pull/291)
- `stringx.rpartition` returned bad resuklts on a not-found [#299](https://github.com/stevedonovan/Penlight/pull/299)

## 1.6.0 (2018-11-23)

Expand Down
6 changes: 5 additions & 1 deletion lua/pl/stringx.lua
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,11 @@ end
function stringx.rpartition(s,ch)
assert_string(1,s)
assert_nonempty_string(2,ch)
return _partition(s,ch,stringx.rfind)
local a,b,c = _partition(s,ch,stringx.rfind)
if a == s then -- no match found
return c,b,a
end
return a,b,c
end

--- return the 'character' at the index.
Expand Down
3 changes: 3 additions & 0 deletions tests/test-stringx.lua
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,12 @@ asserteq(T(stringx.partition('a', 'a')), T('', 'a', ''))
asserteq(T(stringx.partition('abc', 'b')), T('a', 'b', 'c'))
asserteq(T(stringx.partition('abc', '.+')), T('abc','',''))
asserteq(T(stringx.partition('a,b,c', ',')), T('a',',','b,c'))
asserteq(T(stringx.partition('abc', '/')), T('abc', '', ''))
-- rpartition
asserteq(T(stringx.rpartition('a/b/c', '/')), T('a/b', '/', 'c'))
asserteq(T(stringx.rpartition('abc', 'b')), T('a', 'b', 'c'))
asserteq(T(stringx.rpartition('a', 'a')), T('', 'a', ''))
asserteq(T(stringx.rpartition('abc', '/')), T('', '', 'abc'))


-- at (works like s:sub(idx,idx), so negative indices allowed
Expand Down

0 comments on commit f7209be

Please sign in to comment.