algorithm: handle empty inputs in rotateLeft (#26191)

`rotateLeft` and `rotatedLeft` raised `DivByZeroDefect` for empty
containers because their whole-container overloads computed `dist mod
arg.len` before checking for an empty input.

Handle empty inputs explicitly:

- `rotateLeft` returns `0` and leaves the container unchanged.
- `rotatedLeft` returns an empty sequence.

Add a regression test covering both overloads.

The slice overloads are intentionally left unchanged because zero-length
slice semantics need separate consideration.

Signed-off-by: cuishuang <imcusg@gmail.com>
This commit is contained in:
cui fliter
2026-09-09 16:57:29 +08:00
committed by GitHub
parent 715173f4be
commit 0ed883ef11
2 changed files with 10 additions and 0 deletions

View File

@@ -74,6 +74,12 @@ block:
doAssert s5.rotateLeft(3 ..< 10, 11) == 6
doAssert s5 == "xxxefgabcdxxx"
block:
var x: seq[int] = @[]
doAssert x.rotateLeft(1) == 0
doAssert x == @[]
doAssert x.rotatedLeft(1) == @[]
block product:
doAssert product(newSeq[seq[int]]()) == newSeq[seq[int]](), "empty input"
doAssert product(@[newSeq[int](), @[], @[]]) == newSeq[seq[int]](), "bit more empty input"