Last active
December 24, 2015 00:49
-
-
Save matthewmcneely/6719274 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 parse | |
import ( | |
"fmt" | |
"strings" | |
"strconv" | |
) | |
// A ParseError indicates an error in converting a word into an integer. | |
type ParseError struct { | |
Index int // The index into the space-separated list of words. | |
Word string // The word that generated the parse error. | |
Error error // The raw error that precipitated this error, if any. | |
} | |
// String returns a human-readable error message. | |
func (e *ParseError) String() string { | |
return fmt.Sprintf("pkg parse: error parsing %q as int", e.Word) | |
} | |
// Parse parses the space-separated words in in put as integers. | |
func Parse(input string) (numbers []int, err error) { | |
defer func() { | |
if r := recover(); r != nil { | |
var ok bool | |
err, ok = r.(error) | |
if !ok { | |
err = fmt.Errorf("%v", r) | |
} | |
} | |
}() | |
fields := strings.Fields(input) | |
numbers = fields2numbers(fields) // here panic can occur | |
return | |
} | |
func fields2numbers(fields []string) (numbers []int) { | |
if len(fields) == 0 { | |
panic("no words to parse") | |
} | |
for idx, field := range fields { | |
num, err := strconv.Atoi(field) | |
if err != nil { | |
panic(&ParseError{idx, field, err}) | |
} | |
numbers = append(numbers, num) | |
} | |
return | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment