Created
July 9, 2014 16:09
-
-
Save bodokaiser/6ccd65cf5a5daabbb889 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
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