diff options
author | Ben Burwell <ben@benburwell.com> | 2019-09-17 23:08:46 -0400 |
---|---|---|
committer | Ben Burwell <ben@benburwell.com> | 2019-09-17 23:08:46 -0400 |
commit | a3a1e9decb6a5e36b76c9c7e1a149db81c60bd6e (patch) | |
tree | ca2236e5e66183e415d93b41ee9c9b019c6a362f /caesar.go | |
parent | 679a62a9407c09be0cfb4e22455dca5ae694ce01 (diff) |
add examples, fix bug
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 |