Skip to content

Instantly share code, notes, and snippets.

@flc
Created September 4, 2013 15:59
Show Gist options
  • Save flc/6439105 to your computer and use it in GitHub Desktop.
Save flc/6439105 to your computer and use it in GitHub Desktop.
A Tour of Go - Exercise: Rot13 Reader http://tour.golang.org/#61
package main
import (
"io"
"os"
"strings"
//"fmt"
"bytes"
)
var ascii_uppercase = []byte("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
var ascii_lowercase = []byte("abcdefghijklmnopqrstuvwxyz")
var ascii_uppercase_len = len(ascii_uppercase)
var ascii_lowercase_len = len(ascii_lowercase)
type rot13Reader struct {
r io.Reader
}
func rot13(b byte) byte {
pos := bytes.IndexByte(ascii_uppercase, b)
if pos != -1 {
return ascii_uppercase[(pos+13) % ascii_uppercase_len]
}
pos = bytes.IndexByte(ascii_lowercase, b)
if pos != -1 {
return ascii_lowercase[(pos+13) % ascii_lowercase_len]
}
return b
}
func (r rot13Reader) Read(p []byte) (n int, err error) {
n, err = r.r.Read(p)
for i := 0; i < n; i++ {
p[i] = rot13(p[i])
}
return n, err
}
func main() {
s := strings.NewReader(
"Lbh penpxrq gur pbqr!")
r := rot13Reader{s}
io.Copy(os.Stdout, &r)
}
@hashlash
Copy link

https://gist.github.com/hashlash/63edd5ab69cafe6ef762023e1cacc7ef

package main

import (
    "io"
    "os"
    "strings"
)

type rot13Reader struct {
    r io.Reader
}

func (r13 rot13Reader) Read(b []byte) (int, error) {
    l, err := r13.r.Read(b)
    if err == nil {
        for i := range b {
            switch {
                case 'A' <= b[i] && b[i] <= 'Z':
                    b[i] = (b[i] - 'A' + 13) % 26 + 'A'
                case 'a' <= b[i] && b[i] <= 'z':
                    b[i] = (b[i] - 'a' + 13) % 26 + 'a'
            }
        }
    }
    return l, err
}

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

@KunalKumarSwift
Copy link

KunalKumarSwift commented Sep 7, 2021

func (r rot13Reader) Read(b []byte) (n int, err error) {
	// read incoming stream and get length and error
	n, err = r.r.Read(b)
	// iterate over that range and read individual byte element
	for i := 0; i < n; i++ {
		v := b[i]
		// check ascii codes and keep replacing in the original stream b
		if v >= 65 && v <= 77 {
			b[i] = v + 13
		}

		if v >= 78 && v <= 90 {
			b[i] = v - 13
		}

		if v >= 97 && v <= 109 {
			b[i] = v + 13
		}

		if v >= 110 && v <= 122 {
			b[i] = v - 13
		}
	}
        if err == io.EOF {
		return 0, err
	}
	return n, nil
}

@C-Canchola
Copy link

C-Canchola commented Oct 3, 2021

Saw this on wikipedia and wanted to see if I could implement the Read method using it:
$ # Map upper case A-Z to N-ZA-M and lower case a-z to n-za-m
$ tr 'A-Za-z' 'N-ZA-Mn-za-m' <<< "The Quick Brown Fox Jumps Over The Lazy Dog"
Gur Dhvpx Oebja Sbk Whzcf Bire Gur Ynml Qbt

My attempt:

package main

import (
	"bytes"
	"io"
	"os"
	"os/exec"
	"strings"
)

type rot13Reader struct {
	r io.Reader
}

// trRot returns a new byte slice by using the tr command on the originally provided byte slice.
func trRot(b []byte) ([]byte, error) {

	r13Buf := &bytes.Buffer{}

	cmd := exec.Command("tr", "A-Za-z", "N-ZA-Mn-za-m")
	cmd.Stdin, cmd.Stdout, cmd.Stderr = bytes.NewBuffer(b), r13Buf, os.Stderr

	if err := cmd.Run(); err != nil {
		return nil, err
	}

	return r13Buf.Bytes(), nil
}

func (rr rot13Reader) Read(b []byte) (int, error) {

	readBytes := make([]byte, len(b))

	n, err := rr.r.Read(readBytes)
	if err != nil {
		return n, err
	}

	r13Bytes, err := trRot(readBytes)
	if err != nil {
		return 0, err
	}

	return copy(b, r13Bytes), err
}

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

@Spaider
Copy link

Spaider commented Jun 7, 2022

Hey, looks like almost nobody noticed that there's an exclamation mark at the end of the string :)

package main

import (
	"io"
	"os"
	"strings"
)

type rot13Reader struct {
	r io.Reader
}

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

	for i := 0; i < n; i++ {
		var inp = p[i]
		if (inp >= 65 && inp <= 77) ||
			(inp >= 97 && inp <= 109) {
			p[i] = inp + 13
		} else if (inp >= 78 && inp <= 90) ||
			(inp >= 110 && inp <= 122) {
			p[i] = inp - 13
		}
	}

	return n, err
}

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment