From f13250e6a926640c4d0ee858f84fcf8036d612aa Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Wed, 22 Nov 2017 14:47:56 -0500 Subject: ahhh --- util.go | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 util.go (limited to 'util.go') diff --git a/util.go b/util.go new file mode 100644 index 0000000..dbb7d70 --- /dev/null +++ b/util.go @@ -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 +} -- cgit v1.2.3