Last active
September 14, 2017 14:49
-
-
Save vladimirvivien/76a6ddee2fd3029db9dc402cf26e12c8 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 { | |
src string | |
cur int | |
} | |
func newAlphaReader(src string) *alphaReader { | |
return &alphaReader{src: src} | |
} | |
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) { | |
if a.cur >= len(a.src) { | |
return 0, io.EOF | |
} | |
x := len(a.src) - a.cur | |
n, bound := 0, 0 | |
if x >= len(p) { | |
bound = len(p) | |
} else if x <= len(p) { | |
bound = x | |
} | |
buf := make([]byte, bound) | |
for n < bound { | |
if char := alpha(a.src[a.cur]); char != 0 { | |
buf[n] = char | |
} | |
n++ | |
a.cur++ | |
} | |
copy(p, buf) | |
return n, nil | |
} | |
func main() { | |
reader := newAlphaReader("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