From a3a1e9decb6a5e36b76c9c7e1a149db81c60bd6e Mon Sep 17 00:00:00 2001 From: Ben Burwell Date: Tue, 17 Sep 2019 23:08:46 -0400 Subject: add examples, fix bug --- caesar.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'caesar.go') diff --git a/caesar.go b/caesar.go index 191c5b8..4829e6a 100644 --- a/caesar.go +++ b/caesar.go @@ -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 -- cgit v1.2.3