diff options
author | Luke Shumaker <lukeshu@lukeshu.com> | 2017-11-22 14:47:56 -0500 |
---|---|---|
committer | Luke Shumaker <lukeshu@lukeshu.com> | 2017-11-22 14:47:56 -0500 |
commit | f13250e6a926640c4d0ee858f84fcf8036d612aa (patch) | |
tree | d50dceaca4c048f7efde241d1af85afccfe406b5 /util.go | |
parent | db24f3cbd10603f852032a95b5983335b6b5aff2 (diff) |
ahhh
Diffstat (limited to 'util.go')
-rw-r--r-- | util.go | 49 |
1 files changed, 49 insertions, 0 deletions
@@ -0,0 +1,49 @@ +package libfastimport + +import ( + "fmt" + "strconv" + "strings" +) + +func trimLinePrefix(line string, prefix string) string { + if !strings.HasPrefix(line, prefix) { + panic("line didn't have prefix") + } + if !strings.HasSuffix(line, "\n") { + panic("line didn't have prefix") + } + return strings.TrimSuffix(strings.TrimPrefix(line, prefix), "\n") +} + +func parse_data(line string) (data string, err error) { + nl := strings.IndexByte(line, '\n') + if nl < 0 { + return "", fmt.Errorf("data: expected newline: %v", data) + } + head := line[:nl+1] + rest := line[nl+1:] + if !strings.HasPrefix(head, "data ") { + return "", fmt.Errorf("data: could not parse: %v", data) + } + if strings.HasPrefix(head, "data <<") { + // Delimited format + delim := trimLinePrefix(head, "data <<") + suffix := "\n" + delim + "\n" + if !strings.HasSuffix(rest, suffix) { + return "", fmt.Errorf("data: did not find suffix: %v", suffix) + } + data = strings.TrimSuffix(rest, suffix) + } else { + // Exact byte count format + size, err := strconv.Atoi(trimLinePrefix(head, "data ")) + if err != nil { + return "", err + } + if size != len(rest) { + panic("FIReader should not have let this happen") + } + data = rest + } + return +} |