diff options
Diffstat (limited to 'caesar.go')
-rw-r--r-- | caesar.go | 9 |
1 files changed, 8 insertions, 1 deletions
@@ -26,9 +26,16 @@ func (rr RuneRange) contains(r rune) bool { return r >= rr.Start && r <= rr.End } +// size calculates the size of the range. +func (rr RuneRange) size() int { + return int(rr.End-rr.Start) + 1 +} + // shift shifts r by d within the range, modulo the size of the range. func (rr RuneRange) shift(r rune, d int) rune { - return rr.Start + (r - rr.Start + rune(d)%(rr.End-rr.Start)) + pos := int(r - rr.Start) + newPos := (pos + d) % rr.size() + return rr.Start + rune(newPos) } // A Coder encodes and decodes Caesar cipher messages according to its key and |