Use maps and slices packages

Use slices.Clone, slices.Contains, maps.Clone and maps.Equal.

Test: go test ./...
Change-Id: I96596f157dec2558007da4917c998a64d0cc4d79
This commit is contained in:
Colin Cross 2024-03-28 12:37:22 -07:00
parent 894b318528
commit 4cc5290aac
8 changed files with 29 additions and 37 deletions

View file

@ -20,6 +20,7 @@ import (
"go/doc" "go/doc"
"html/template" "html/template"
"reflect" "reflect"
"slices"
"strconv" "strconv"
"strings" "strings"
"unicode" "unicode"
@ -34,7 +35,7 @@ import (
func (ps *PropertyStruct) Clone() *PropertyStruct { func (ps *PropertyStruct) Clone() *PropertyStruct {
ret := *ps ret := *ps
ret.Properties = append([]Property(nil), ret.Properties...) ret.Properties = slices.Clone(ret.Properties)
for i, prop := range ret.Properties { for i, prop := range ret.Properties {
ret.Properties[i] = prop.Clone() ret.Properties[i] = prop.Clone()
} }
@ -44,7 +45,7 @@ func (ps *PropertyStruct) Clone() *PropertyStruct {
func (p *Property) Clone() Property { func (p *Property) Clone() Property {
ret := *p ret := *p
ret.Properties = append([]Property(nil), ret.Properties...) ret.Properties = slices.Clone(ret.Properties)
for i, prop := range ret.Properties { for i, prop := range ret.Properties {
ret.Properties[i] = prop.Clone() ret.Properties[i] = prop.Clone()
} }

View file

@ -24,6 +24,7 @@ import (
"hash/fnv" "hash/fnv"
"io" "io"
"io/ioutil" "io/ioutil"
"maps"
"os" "os"
"path/filepath" "path/filepath"
"reflect" "reflect"
@ -418,15 +419,7 @@ type Variation struct {
type variationMap map[string]string type variationMap map[string]string
func (vm variationMap) clone() variationMap { func (vm variationMap) clone() variationMap {
if vm == nil { return maps.Clone(vm)
return nil
}
newVm := make(variationMap)
for k, v := range vm {
newVm[k] = v
}
return newVm
} }
// Compare this variationMap to another one. Returns true if the every entry in this map // Compare this variationMap to another one. Returns true if the every entry in this map
@ -441,10 +434,7 @@ func (vm variationMap) subsetOf(other variationMap) bool {
} }
func (vm variationMap) equal(other variationMap) bool { func (vm variationMap) equal(other variationMap) bool {
if len(vm) != len(other) { return maps.Equal(vm, other)
return false
}
return vm.subsetOf(other)
} }
type singletonInfo struct { type singletonInfo struct {
@ -800,16 +790,11 @@ type transitionMutatorImpl struct {
// Adds each argument in items to l if it's not already there. // Adds each argument in items to l if it's not already there.
func addToStringListIfNotPresent(l []string, items ...string) []string { func addToStringListIfNotPresent(l []string, items ...string) []string {
OUTER:
for _, i := range items { for _, i := range items {
for _, existing := range l { if !slices.Contains(l, i) {
if existing == i {
continue OUTER
}
}
l = append(l, i) l = append(l, i)
} }
}
return l return l
} }
@ -1746,14 +1731,14 @@ func (c *Context) createVariations(origModule *moduleInfo, mutatorName string,
m := *origModule m := *origModule
newModule := &m newModule := &m
newModule.directDeps = append([]depInfo(nil), origModule.directDeps...) newModule.directDeps = slices.Clone(origModule.directDeps)
newModule.reverseDeps = nil newModule.reverseDeps = nil
newModule.forwardDeps = nil newModule.forwardDeps = nil
newModule.logicModule = newLogicModule newModule.logicModule = newLogicModule
newModule.variant = newVariant(origModule, mutatorName, variationName, local) newModule.variant = newVariant(origModule, mutatorName, variationName, local)
newModule.properties = newProperties newModule.properties = newProperties
newModule.providers = append([]interface{}(nil), origModule.providers...) newModule.providers = slices.Clone(origModule.providers)
newModule.providerInitialValueHashes = append([]uint64(nil), origModule.providerInitialValueHashes...) newModule.providerInitialValueHashes = slices.Clone(origModule.providerInitialValueHashes)
newModules = append(newModules, newModule) newModules = append(newModules, newModule)

View file

@ -16,6 +16,7 @@ package blueprint
import ( import (
"fmt" "fmt"
"slices"
"sort" "sort"
"strings" "strings"
@ -40,7 +41,7 @@ func verifyGlob(key globKey, pattern string, excludes []string, g pathtools.Glob
func (c *Context) glob(pattern string, excludes []string) ([]string, error) { func (c *Context) glob(pattern string, excludes []string) ([]string, error) {
// Sort excludes so that two globs with the same excludes in a different order reuse the same // Sort excludes so that two globs with the same excludes in a different order reuse the same
// key. Make a copy first to avoid modifying the caller's version. // key. Make a copy first to avoid modifying the caller's version.
excludes = append([]string(nil), excludes...) excludes = slices.Clone(excludes)
sort.Strings(excludes) sort.Strings(excludes)
key := globToKey(pattern, excludes) key := globToKey(pattern, excludes)
@ -54,7 +55,7 @@ func (c *Context) glob(pattern string, excludes []string) ([]string, error) {
// Glob has already been done, double check it is identical // Glob has already been done, double check it is identical
verifyGlob(key, pattern, excludes, g) verifyGlob(key, pattern, excludes, g)
// Return a copy so that modifications don't affect the cached value. // Return a copy so that modifications don't affect the cached value.
return append([]string(nil), g.Matches...), nil return slices.Clone(g.Matches), nil
} }
// Get a globbed file list // Get a globbed file list
@ -74,11 +75,11 @@ func (c *Context) glob(pattern string, excludes []string) ([]string, error) {
// Getting the list raced with another goroutine, throw away the results and use theirs // Getting the list raced with another goroutine, throw away the results and use theirs
verifyGlob(key, pattern, excludes, g) verifyGlob(key, pattern, excludes, g)
// Return a copy so that modifications don't affect the cached value. // Return a copy so that modifications don't affect the cached value.
return append([]string(nil), g.Matches...), nil return slices.Clone(g.Matches), nil
} }
// Return a copy so that modifications don't affect the cached value. // Return a copy so that modifications don't affect the cached value.
return append([]string(nil), result.Matches...), nil return slices.Clone(result.Matches), nil
} }
func (c *Context) Globs() pathtools.MultipleGlobResults { func (c *Context) Globs() pathtools.MultipleGlobResults {

View file

@ -16,6 +16,7 @@ package blueprint
import ( import (
"reflect" "reflect"
"slices"
"strconv" "strconv"
"strings" "strings"
"testing" "testing"
@ -271,7 +272,7 @@ func Test_parseNinjaOrSimpleStrings(t *testing.T) {
for _, tt := range testCases { for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
inCopy := append([]string(nil), tt.in...) inCopy := slices.Clone(tt.in)
scope := newLocalScope(nil, "") scope := newLocalScope(nil, "")
scope.AddLocalVariable("abc", "abc") scope.AddLocalVariable("abc", "abc")

View file

@ -18,6 +18,7 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"reflect" "reflect"
"slices"
"syscall" "syscall"
"testing" "testing"
) )
@ -233,7 +234,7 @@ func TestFs_ListDirsRecursiveFollowSymlinks(t *testing.T) {
t.Run(test.name, func(t *testing.T) { t.Run(test.name, func(t *testing.T) {
got, err := fs.ListDirsRecursive(filepath.Join(dir, test.name), FollowSymlinks) got, err := fs.ListDirsRecursive(filepath.Join(dir, test.name), FollowSymlinks)
checkErr(t, test.err, err) checkErr(t, test.err, err)
want := append([]string(nil), test.dirs...) want := slices.Clone(test.dirs)
for i := range want { for i := range want {
want[i] = filepath.Join(dir, want[i]) want[i] = filepath.Join(dir, want[i])
} }
@ -279,7 +280,7 @@ func TestFs_ListDirsRecursiveDontFollowSymlinks(t *testing.T) {
t.Run(test.name, func(t *testing.T) { t.Run(test.name, func(t *testing.T) {
got, err := fs.ListDirsRecursive(filepath.Join(dir, test.name), DontFollowSymlinks) got, err := fs.ListDirsRecursive(filepath.Join(dir, test.name), DontFollowSymlinks)
checkErr(t, test.err, err) checkErr(t, test.err, err)
want := append([]string(nil), test.dirs...) want := slices.Clone(test.dirs)
for i := range want { for i := range want {
want[i] = filepath.Join(dir, want[i]) want[i] = filepath.Join(dir, want[i])
} }

View file

@ -15,6 +15,7 @@
package proptools package proptools
import ( import (
"slices"
"strings" "strings"
"unsafe" "unsafe"
) )
@ -33,7 +34,7 @@ func NinjaEscapeList(slice []string) []string {
if !sliceCopied { if !sliceCopied {
// If this was the first string that was modified by escaping then make a copy of the // If this was the first string that was modified by escaping then make a copy of the
// input slice to use as the output slice. // input slice to use as the output slice.
slice = append([]string(nil), slice...) slice = slices.Clone(slice)
sliceCopied = true sliceCopied = true
} }
slice[i] = escaped slice[i] = escaped
@ -66,7 +67,7 @@ func ShellEscapeList(slice []string) []string {
if !sliceCopied { if !sliceCopied {
// If this was the first string that was modified by escaping then make a copy of the // If this was the first string that was modified by escaping then make a copy of the
// input slice to use as the output slice. // input slice to use as the output slice.
slice = append([]string(nil), slice...) slice = slices.Clone(slice)
sliceCopied = true sliceCopied = true
} }
slice[i] = escaped slice[i] = escaped
@ -83,7 +84,7 @@ func ShellEscapeListIncludingSpaces(slice []string) []string {
if !sliceCopied { if !sliceCopied {
// If this was the first string that was modified by escaping then make a copy of the // If this was the first string that was modified by escaping then make a copy of the
// input slice to use as the output slice. // input slice to use as the output slice.
slice = append([]string(nil), slice...) slice = slices.Clone(slice)
sliceCopied = true sliceCopied = true
} }
slice[i] = escaped slice[i] = escaped

View file

@ -18,6 +18,7 @@ import (
"bytes" "bytes"
"os/exec" "os/exec"
"reflect" "reflect"
"slices"
"testing" "testing"
"unsafe" "unsafe"
) )
@ -235,7 +236,7 @@ func TestNinjaEscapeList(t *testing.T) {
t.Run(tf.name, func(t *testing.T) { t.Run(tf.name, func(t *testing.T) {
for _, tt := range testCases { for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
inCopy := append([]string(nil), tt.in...) inCopy := slices.Clone(tt.in)
got := tf.f(tt.in) got := tf.f(tt.in)

View file

@ -17,6 +17,7 @@ package proptools
import ( import (
"fmt" "fmt"
"reflect" "reflect"
"slices"
"strings" "strings"
) )
@ -317,7 +318,7 @@ func extendPropertiesRecursive(dstValues []reflect.Value, srcValue reflect.Value
// of destinations to consider. Make a copy of dstValues if necessary // of destinations to consider. Make a copy of dstValues if necessary
// to avoid modifying the backing array of an input parameter. // to avoid modifying the backing array of an input parameter.
if !dstValuesCopied { if !dstValuesCopied {
dstValues = append([]reflect.Value(nil), dstValues...) dstValues = slices.Clone(dstValues)
dstValuesCopied = true dstValuesCopied = true
} }
dstValues = append(dstValues, embeddedDstValue) dstValues = append(dstValues, embeddedDstValue)