summaryrefslogtreecommitdiff
path: root/caesar.go
diff options
context:
space:
mode:
authorBen Burwell <ben@benburwell.com>2019-09-17 23:08:46 -0400
committerBen Burwell <ben@benburwell.com>2019-09-17 23:08:46 -0400
commita3a1e9decb6a5e36b76c9c7e1a149db81c60bd6e (patch)
treeca2236e5e66183e415d93b41ee9c9b019c6a362f /caesar.go
parent679a62a9407c09be0cfb4e22455dca5ae694ce01 (diff)
add examples, fix bug
Diffstat (limited to 'caesar.go')
-rw-r--r--caesar.go9
1 files changed, 8 insertions, 1 deletions
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