From 3906f7c0d084c178ee8155abd9b8e570d5d43f25 Mon Sep 17 00:00:00 2001 From: Manni Wood Date: Thu, 17 Nov 2016 21:45:46 -0500 Subject: [PATCH] Casts aclitem earl to avoid O(2n) --- aclitem_parse_test.go | 24 ++++++++++++------------ values.go | 16 +++++----------- 2 files changed, 17 insertions(+), 23 deletions(-) diff --git a/aclitem_parse_test.go b/aclitem_parse_test.go index a0e5e858..5c7c748f 100644 --- a/aclitem_parse_test.go +++ b/aclitem_parse_test.go @@ -36,62 +36,62 @@ func TestEscapeAclItem(t *testing.T) { func TestParseAclItemArray(t *testing.T) { tests := []struct { input string - expected []string + expected []AclItem errMsg string }{ { "", - []string{}, + []AclItem{}, "", }, { "one", - []string{"one"}, + []AclItem{"one"}, "", }, { `"one"`, - []string{"one"}, + []AclItem{"one"}, "", }, { "one,two,three", - []string{"one", "two", "three"}, + []AclItem{"one", "two", "three"}, "", }, { `"one","two","three"`, - []string{"one", "two", "three"}, + []AclItem{"one", "two", "three"}, "", }, { `"one",two,"three"`, - []string{"one", "two", "three"}, + []AclItem{"one", "two", "three"}, "", }, { `one,two,"three"`, - []string{"one", "two", "three"}, + []AclItem{"one", "two", "three"}, "", }, { `"one","two",three`, - []string{"one", "two", "three"}, + []AclItem{"one", "two", "three"}, "", }, { `"one","t w o",three`, - []string{"one", "t w o", "three"}, + []AclItem{"one", "t w o", "three"}, "", }, { `"one","t, w o\"\}\\",three`, - []string{"one", `t, w o"}\`, "three"}, + []AclItem{"one", `t, w o"}\`, "three"}, "", }, { `"one","two",three"`, - []string{"one", "two", `three"`}, + []AclItem{"one", "two", `three"`}, "", }, { diff --git a/values.go b/values.go index 4312a61f..490d2f38 100644 --- a/values.go +++ b/values.go @@ -3057,11 +3057,11 @@ func encodeAclItemSlice(w *WriteBuf, oid Oid, aclitems []AclItem) error { // parseAclItemArray parses the textual representation // of the aclitem[] type. -func parseAclItemArray(arr string) ([]string, error) { +func parseAclItemArray(arr string) ([]AclItem, error) { r := strings.NewReader(arr) // Difficult to guess a performant initial capacity for a slice of // aclitems, but let's go with 5. - vals := make([]string, 0, 5) + vals := make([]AclItem, 0, 5) // A single value vlu := "" for { @@ -3094,13 +3094,13 @@ func parseAclItemArray(arr string) ([]string, error) { if err != nil { if err == io.EOF { // This error was expected and is OK. - vals = append(vals, vlu) + vals = append(vals, AclItem(vlu)) return vals, nil } // This error was not expected. return nil, err } - vals = append(vals, vlu) + vals = append(vals, AclItem(vlu)) } } @@ -3192,13 +3192,7 @@ func decodeAclItemArray(vr *ValueReader) []AclItem { vr.Fatal(ProtocolError(err.Error())) return nil } - - // cast strings into AclItems before returning - aclitems := make([]AclItem, len(strs)) - for i := range aclitems { - aclitems[i] = AclItem(strs[i]) - } - return aclitems + return strs } func encodeStringSlice(w *WriteBuf, oid Oid, slice []string) error {