Skip to content

Instantly share code, notes, and snippets.

@a-h
Last active March 27, 2016 23:24
Show Gist options
  • Select an option

  • Save a-h/09bb3e60946f48291597 to your computer and use it in GitHub Desktop.

Select an option

Save a-h/09bb3e60946f48291597 to your computer and use it in GitHub Desktop.
ROT13 Tour
package main
import (
"golang.org/x/tour/pic"
"image"
"image/color"
)
type Image struct{}
func (Image) ColorModel() color.Model {
return color.RGBAModel
}
func (Image) Bounds() image.Rectangle {
return image.Rect(0, 0, 100, 100)
}
func (Image) At(x, y int) color.Color {
return color.RGBA{uint8(x), uint8(y), uint8(x), 255}
}
func main() {
m := Image{}
pic.ShowImage(m)
}
package main
import (
"io"
"os"
"strings"
)
type rot13Reader struct {
r io.Reader
}
func position(alphabet string, b byte, offset int) (changed bool, result byte) {
index := strings.IndexByte(alphabet, b)
if index >= 0 {
pos := index + offset
if pos >= len(alphabet) {
pos -= len(alphabet)
}
return true, alphabet[pos]
} else {
return false, b
}
}
func (re rot13Reader) Read(b []byte) (read int, e error) {
read_from_underlying, underlying_error := re.r.Read(b);
if underlying_error == nil {
for i := 0; i < read_from_underlying; i ++ {
changed, n := position("ABCDEFGHIJKLMNOPQRSTUVWXYZ", b[i], 13)
if !changed {
_, n = position("abcdefghijklmnopqrstuvwxyz", b[i], 13)
}
b[i] = n
}
}
return read_from_underlying, underlying_error
}
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