Skip to content

Commit

Permalink
Create exercise-rot13Reader.go
Browse files Browse the repository at this point in the history
  • Loading branch information
mitDarkMomo authored May 29, 2018
1 parent f05f9a2 commit 25d84c3
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions exercise-rot13Reader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import (
"io"
"os"
"strings"
)

type rot13Reader struct {
r io.Reader
}

// rot13 转换
func rot13Tr(b byte) byte {
switch {
case ('A' <= b && b <= 'M') || ('a' <= b && b <= 'm'):
b += 13
case ('M' < b && b <= 'Z') || ('m' < b && b <= 'z'):
b -= 13
}
return b
}

func (r13 rot13Reader) Read(bytes []byte) (int, error) {
n, err := r13.r.Read(bytes)

for i := range bytes {
bytes[i] = rot13Tr(bytes[i])
}

return n, err
}

func main() {
s := strings.NewReader("Lbh penpxrq gur pbqr!")
r := rot13Reader{s}
io.Copy(os.Stdout, &r)
}

0 comments on commit 25d84c3

Please sign in to comment.