Last active
December 30, 2015 11:31
-
-
Save Komosa/8a896504dff5163e88d6 to your computer and use it in GitHub Desktop.
parse lines as dkvp
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 main | |
import ( | |
"fmt" | |
"regexp" | |
"strconv" | |
"strings" | |
) | |
type DKVP map[string]string | |
func From(line string) DKVP { | |
res := DKVP{} | |
for _, pair := range strings.Split(line, ",") { | |
kv := strings.SplitN(pair, "=", 2) | |
if _, ok := res[kv[0]]; !ok { | |
if len(kv) == 1 { | |
res[kv[0]] = "" | |
} else { | |
res[kv[0]] = kv[1] | |
} | |
} else if len(kv) == 2 { | |
f0, err0 := strconv.ParseFloat(kv[1], 64) | |
f1, err1 := strconv.ParseFloat(res[kv[0]], 64) | |
if err0 != nil || err1 != nil { | |
continue | |
} | |
res[kv[0]] = strconv.FormatFloat(f0+f1, 'g', -1, 64) | |
} | |
} | |
return res | |
} | |
var merge_io_re *regexp.Regexp = regexp.MustCompile("_i=|_o=") | |
func merge_io(line string) string { | |
return merge_io_re.ReplaceAllLiteralString(line, "=") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment