From 8e111e9e5c657509fecf54672504b335f2785600 Mon Sep 17 00:00:00 2001 From: MrBlackk Date: Tue, 15 Sep 2020 14:21:27 +0300 Subject: [PATCH] fix to work just with latin letters, and skip whitespaces commas etc. --- .../07_tcp-apps/01_rot13/main.go | 28 +++++++++++-------- 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/015_understanding-TCP-servers/07_tcp-apps/01_rot13/main.go b/015_understanding-TCP-servers/07_tcp-apps/01_rot13/main.go index 70e4ef72..deed4fce 100644 --- a/015_understanding-TCP-servers/07_tcp-apps/01_rot13/main.go +++ b/015_understanding-TCP-servers/07_tcp-apps/01_rot13/main.go @@ -5,7 +5,7 @@ import ( "fmt" "log" "net" - "strings" + "unicode" ) func main() { @@ -28,23 +28,27 @@ func main() { func handle(conn net.Conn) { scanner := bufio.NewScanner(conn) for scanner.Scan() { - ln := strings.ToLower(scanner.Text()) - bs := []byte(ln) - r := rot13(bs) + ln := scanner.Text() + r := rot13(ln) fmt.Fprintf(conn, "%s - %s\n\n", ln, r) } } -func rot13(bs []byte) []byte { - var r13 = make([]byte, len(bs)) - for i, v := range bs { - // ascii 97 - 122 - if v <= 109 { - r13[i] = v + 13 +func rot13(ln string) string { + var r13 = make([]rune, len(ln)) + for i, v := range ln { + if unicode.IsLower(v) && v < unicode.MaxASCII { + r13[i] = rotateCharacter(v, 'a') + } else if unicode.IsUpper(v) && v < unicode.MaxASCII { + r13[i] = rotateCharacter(v, 'A') } else { - r13[i] = v - 13 + r13[i] = v } } - return r13 + return string(r13) +} + +func rotateCharacter(v rune, base rune) rune { + return (v - base + 13) % 26 + base }