summaryrefslogtreecommitdiff
path: root/caesar.go
diff options
context:
space:
mode:
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