wgcfg: use string cut instead of string split

Signed-off-by: julianknodt <julianknodt@gmail.com>
josh/deflake-pipe-again
julianknodt 3 years ago committed by Julian Knodt
parent 664edbe566
commit d349a3231e

@ -69,6 +69,14 @@ func parseKeyHex(s string) (*wgkey.Key, error) {
return &key, nil return &key, nil
} }
// stringsCut is strings.Cut from proposed https://github.com/golang/go/issues/46336.
func stringsCut(s, sep string) (before, after string, found bool) {
if i := strings.Index(s, sep); i >= 0 {
return s[:i], s[i+len(sep):], true
}
return s, "", false
}
// FromUAPI generates a Config from r. // FromUAPI generates a Config from r.
// r should be generated by calling device.IpcGetOperation; // r should be generated by calling device.IpcGetOperation;
// it is not compatible with other uapi streams. // it is not compatible with other uapi streams.
@ -83,12 +91,10 @@ func FromUAPI(r io.Reader) (*Config, error) {
if line == "" { if line == "" {
continue continue
} }
parts := strings.Split(line, "=") key, value, ok := stringsCut(line, "=")
if len(parts) != 2 { if !ok {
return nil, fmt.Errorf("failed to parse line %q, found %d =-separated parts, want 2", line, len(parts)) return nil, fmt.Errorf("failed to cut line %q on =", line)
} }
key := parts[0]
value := parts[1]
valueBytes := scanner.Bytes()[len(key)+1:] valueBytes := scanner.Bytes()[len(key)+1:]
if key == "public_key" { if key == "public_key" {

@ -7,6 +7,7 @@ package wgcfg
import ( import (
"bufio" "bufio"
"bytes" "bytes"
"io"
"reflect" "reflect"
"runtime" "runtime"
"testing" "testing"
@ -90,10 +91,10 @@ func BenchmarkFromUAPI(b *testing.B) {
r := bytes.NewReader(buf.Bytes()) r := bytes.NewReader(buf.Bytes())
b.ReportAllocs() b.ReportAllocs()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
r.Seek(0, io.SeekStart)
_, err := FromUAPI(r) _, err := FromUAPI(r)
if err != nil { if err != nil {
b.Errorf("failed from UAPI: %v", err) b.Errorf("failed from UAPI: %v", err)
} }
r.Seek(0, 0)
} }
} }

Loading…
Cancel
Save