Last active
September 14, 2017 14:54
-
-
Save vladimirvivien/4a3c1f43f70f51071638dcd365922c88 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type alphaReader struct { | |
reader io.Reader | |
} | |
func newAlphaReader(reader io.Reader) *alphaReader { | |
return &alphaReader{reader: reader} | |
} | |
func alpha(r byte) byte { | |
if (r >= 'A' && r <= 'Z') || (r >= 'a' && r <= 'z') { | |
return r | |
} | |
return 0 | |
} | |
func (a *alphaReader) Read(p []byte) (int, error) { | |
n, err := a.reader.Read(p) | |
if err != nil { | |
return n, err | |
} | |
buf := make([]byte, n) | |
for i := 0; i < n; i++ { | |
if char := alpha(p[i]); char != 0 { | |
buf[i] = char | |
} | |
} | |
copy(p, buf) | |
return n, nil | |
} | |
func main() { | |
// use an io.Reader as source for alphaReader | |
reader := newAlphaReader(strings.NewReader("Hello! It's 9am, where is the sun?")) | |
p := make([]byte, 4) | |
for { | |
n, err := reader.Read(p) | |
if err == io.EOF { | |
break | |
} | |
fmt.Print(string(p[:n])) | |
} | |
fmt.Println() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment