Skip to content

Commit

Permalink
expand: fix SplitBraces v3 TODO
Browse files Browse the repository at this point in the history
Leave an internal TODO for skipping the allocations if there are no
brace expansions. The important bit is to simplify the API, as we can't
do that once v3.0.0 is out.
  • Loading branch information
mvdan committed Jan 20, 2019
1 parent 4c45382 commit bf05740
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
2 changes: 1 addition & 1 deletion expand/braces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func TestBraces(t *testing.T) {
wantStr := printWords(tc.want...)
wantBraceExpParts(t, tc.in, false)

inBraces, _ := syntax.SplitBraces(tc.in)
inBraces := syntax.SplitBraces(tc.in)
wantBraceExpParts(t, inBraces, inStr != wantStr)

got := Braces(inBraces)
Expand Down
8 changes: 4 additions & 4 deletions expand/expand.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,11 +365,11 @@ func Fields(cfg *Config, words ...*syntax.Word) ([]string, error) {
fields := make([]string, 0, len(words))
dir := cfg.envGet("PWD")
for _, word := range words {
expBraces := []*syntax.Word{word}
if expWord, any := syntax.SplitBraces(word); any {
expBraces = Braces(expWord)
afterBraces := []*syntax.Word{word}
if w2 := syntax.SplitBraces(word); w2 != word {
afterBraces = Braces(w2)
}
for _, word2 := range expBraces {
for _, word2 := range afterBraces {
wfields, err := cfg.wordFields(word2.Parts)
if err != nil {
return nil, err
Expand Down
20 changes: 12 additions & 8 deletions syntax/braces.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ var (
litRightBrace = &Lit{Value: "}"}
)

// TODO: remove bool return parameter, make it equivalent to input != output.

// SplitBraces may introduce a number of BraceExp parts to a word, which can be
// used to perform brace expansion with expand.Braces. For example, passing it a
// literal word "foo{bar,baz}" will result in a word containing the literal
// "foo", and a brace expansion with the elements "bar" and "baz".
// SplitBraces will return a new Word with a number of BraceExp parts, if the
// input word contained brace expansions within its literal parts. Otherwise,
// the input word is returned intact.
//
// For example, a literal word "foo{bar,baz}" will result in a word containing
// the literal "foo", and a brace expansion with the elements "bar" and "baz".
//
// It does not return an error; malformed brace expansions are simply skipped.
// For example, the literal word "a{b" is returned unchanged.
func SplitBraces(word *Word) (*Word, bool) {
func SplitBraces(word *Word) *Word {
any := false
top := &Word{}
acc := top
Expand Down Expand Up @@ -169,5 +169,9 @@ func SplitBraces(word *Word) (*Word, bool) {
acc.Parts = append(acc.Parts, elem.Parts...)
}
}
return top, any
if !any {
// TODO: avoid allocations in this case
return word
}
return top
}

0 comments on commit bf05740

Please sign in to comment.