Skip to content

Commit c9f18cd

Browse files
committed
make use of maps.Keys and maps.Values from Go 1.23
1 parent 9df5974 commit c9f18cd

File tree

2 files changed

+10
-24
lines changed

2 files changed

+10
-24
lines changed

expand/expand.go

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@ import (
99
"fmt"
1010
"io"
1111
"io/fs"
12+
"maps"
1213
"os"
1314
"os/user"
1415
"path/filepath"
1516
"regexp"
1617
"runtime"
18+
"slices"
1719
"strconv"
1820
"strings"
1921

@@ -725,19 +727,15 @@ func (cfg *Config) quotedElemFields(pe *syntax.ParamExp) []string {
725727
case "@": // "${!name[@]}"
726728
switch vr := cfg.Env.Get(name); vr.Kind {
727729
case Indexed:
728-
keys := make([]string, 0, len(vr.Map))
729-
// TODO: maps.Keys if it makes it into Go 1.23
730+
// TODO: if an indexed array only has elements 0 and 10,
731+
// we should not return all indices in between those.
732+
keys := make([]string, 0, len(vr.List))
730733
for key := range vr.List {
731734
keys = append(keys, strconv.Itoa(key))
732735
}
733736
return keys
734737
case Associative:
735-
keys := make([]string, 0, len(vr.Map))
736-
// TODO: maps.Keys if it makes it into Go 1.23
737-
for key := range vr.Map {
738-
keys = append(keys, key)
739-
}
740-
return keys
738+
return slices.Collect(maps.Keys(vr.Map))
741739
}
742740
}
743741
return nil
@@ -754,12 +752,7 @@ func (cfg *Config) quotedElemFields(pe *syntax.ParamExp) []string {
754752
case Indexed:
755753
return vr.List
756754
case Associative:
757-
// TODO: maps.Values if it makes it into Go 1.23
758-
elems := make([]string, 0, len(vr.Map))
759-
for _, elem := range vr.Map {
760-
elems = append(elems, elem)
761-
}
762-
return elems
755+
return slices.Collect(maps.Values(vr.Map))
763756
}
764757
case "*": // "${name[*]}"
765758
if vr := cfg.Env.Get(name); vr.Kind == Indexed {

expand/param.go

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package expand
55

66
import (
77
"fmt"
8+
"maps"
89
"regexp"
910
"slices"
1011
"strconv"
@@ -167,10 +168,7 @@ func (cfg *Config) paramExp(pe *syntax.ParamExp) (string, error) {
167168
}
168169
}
169170
case pe.Index != nil && vr.Kind == Associative:
170-
// TODO: use maps.Keys
171-
for k := range vr.Map {
172-
strs = append(strs, k)
173-
}
171+
strs = slices.AppendSeq(strs, maps.Keys(vr.Map))
174172
case !vr.IsSet():
175173
return "", fmt.Errorf("invalid indirect expansion")
176174
case str == "":
@@ -400,12 +398,7 @@ func (cfg *Config) varInd(vr Variable, idx syntax.ArithmExpr) (string, error) {
400398
case Associative:
401399
switch lit := nodeLit(idx); lit {
402400
case "@", "*":
403-
strs := make([]string, 0, len(vr.Map))
404-
// TODO: use maps.Values
405-
for _, val := range vr.Map {
406-
strs = append(strs, val)
407-
}
408-
slices.Sort(strs)
401+
strs := slices.Sorted(maps.Values(vr.Map))
409402
if lit == "*" {
410403
return cfg.ifsJoin(strs), nil
411404
}

0 commit comments

Comments
 (0)