Skip to content

Instantly share code, notes, and snippets.

@bodokaiser
Created July 9, 2014 16:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bodokaiser/6ccd65cf5a5daabbb889 to your computer and use it in GitHub Desktop.
Save bodokaiser/6ccd65cf5a5daabbb889 to your computer and use it in GitHub Desktop.
package parser
type Parser interface {
io.Reader
io.Writer
}
type EmailParser struct {
results [][]byte
}
func (p *EmailParser) Read(b []byte) (int, error) {
// write last byte array of p.results to "b"
// remove last byte array from p.results
return len(b), nil
}
func (p *EmailParser) Write(b []byte) (int, error) {
size := len(b)
// traverse through byte range
for position, char := range b {
// until we find an @ char
if char == '@' {
var offset, length int
// go back through the chars until we have something illegal
for offset = position - 1; offset > 0; offset-- {
char := b[offset]
if !alpha(char) {
break
}
}
// go forward through the chars until we have something illegal
for length = position + 1; length < size; length++ {
char := b[length]
if char != '.' && !alpha(char) {
break
}
}
// send data through channel
p.results <- b[offset+1 : length]
}
}
return size, nil
}
func alpha(c byte) bool {
return 'a' <= c && 'z' >= c
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment