Skip to content

Commit

Permalink
make use of maps.Keys and maps.Values from Go 1.23
Browse files Browse the repository at this point in the history
  • Loading branch information
mvdan committed Feb 11, 2025
1 parent 9df5974 commit c9f18cd
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 24 deletions.
21 changes: 7 additions & 14 deletions expand/expand.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ import (
"fmt"
"io"
"io/fs"
"maps"
"os"
"os/user"
"path/filepath"
"regexp"
"runtime"
"slices"
"strconv"
"strings"

Expand Down Expand Up @@ -725,19 +727,15 @@ func (cfg *Config) quotedElemFields(pe *syntax.ParamExp) []string {
case "@": // "${!name[@]}"
switch vr := cfg.Env.Get(name); vr.Kind {
case Indexed:
keys := make([]string, 0, len(vr.Map))
// TODO: maps.Keys if it makes it into Go 1.23
// TODO: if an indexed array only has elements 0 and 10,
// we should not return all indices in between those.
keys := make([]string, 0, len(vr.List))
for key := range vr.List {
keys = append(keys, strconv.Itoa(key))
}
return keys
case Associative:
keys := make([]string, 0, len(vr.Map))
// TODO: maps.Keys if it makes it into Go 1.23
for key := range vr.Map {
keys = append(keys, key)
}
return keys
return slices.Collect(maps.Keys(vr.Map))
}
}
return nil
Expand All @@ -754,12 +752,7 @@ func (cfg *Config) quotedElemFields(pe *syntax.ParamExp) []string {
case Indexed:
return vr.List
case Associative:
// TODO: maps.Values if it makes it into Go 1.23
elems := make([]string, 0, len(vr.Map))
for _, elem := range vr.Map {
elems = append(elems, elem)
}
return elems
return slices.Collect(maps.Values(vr.Map))
}
case "*": // "${name[*]}"
if vr := cfg.Env.Get(name); vr.Kind == Indexed {
Expand Down
13 changes: 3 additions & 10 deletions expand/param.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package expand

import (
"fmt"
"maps"
"regexp"
"slices"
"strconv"
Expand Down Expand Up @@ -167,10 +168,7 @@ func (cfg *Config) paramExp(pe *syntax.ParamExp) (string, error) {
}
}
case pe.Index != nil && vr.Kind == Associative:
// TODO: use maps.Keys
for k := range vr.Map {
strs = append(strs, k)
}
strs = slices.AppendSeq(strs, maps.Keys(vr.Map))
case !vr.IsSet():
return "", fmt.Errorf("invalid indirect expansion")
case str == "":
Expand Down Expand Up @@ -400,12 +398,7 @@ func (cfg *Config) varInd(vr Variable, idx syntax.ArithmExpr) (string, error) {
case Associative:
switch lit := nodeLit(idx); lit {
case "@", "*":
strs := make([]string, 0, len(vr.Map))
// TODO: use maps.Values
for _, val := range vr.Map {
strs = append(strs, val)
}
slices.Sort(strs)
strs := slices.Sorted(maps.Values(vr.Map))
if lit == "*" {
return cfg.ifsJoin(strs), nil
}
Expand Down

0 comments on commit c9f18cd

Please sign in to comment.