@@ -105,11 +105,12 @@ func (s *Lex) process(word string, env map[string]string) (ProcessWordResult, er
105
105
106
106
type shellWord struct {
107
107
* Lex
108
- scanner scanner.Scanner
109
- envs map [string ]string
110
- rawEscapes bool
111
- matches map [string ]struct {}
112
- nonmatches map [string ]struct {}
108
+ wordsBuffer strings.Builder
109
+ scanner scanner.Scanner
110
+ envs map [string ]string
111
+ rawEscapes bool
112
+ matches map [string ]struct {}
113
+ nonmatches map [string ]struct {}
113
114
}
114
115
115
116
func (sw * shellWord ) process (source string ) (string , []string , error ) {
@@ -121,16 +122,16 @@ func (sw *shellWord) process(source string) (string, []string, error) {
121
122
}
122
123
123
124
type wordsStruct struct {
124
- word string
125
+ buf * strings. Builder
125
126
words []string
126
127
inWord bool
127
128
}
128
129
129
130
func (w * wordsStruct ) addChar (ch rune ) {
130
131
if unicode .IsSpace (ch ) && w .inWord {
131
- if len ( w . word ) != 0 {
132
- w .words = append (w .words , w .word )
133
- w .word = ""
132
+ if w . buf . Len ( ) != 0 {
133
+ w .words = append (w .words , w .buf . String () )
134
+ w .buf . Reset ()
134
135
w .inWord = false
135
136
}
136
137
} else if ! unicode .IsSpace (ch ) {
@@ -139,7 +140,7 @@ func (w *wordsStruct) addChar(ch rune) {
139
140
}
140
141
141
142
func (w * wordsStruct ) addRawChar (ch rune ) {
142
- w .word += string (ch )
143
+ w .buf . WriteRune (ch )
143
144
w .inWord = true
144
145
}
145
146
@@ -150,16 +151,16 @@ func (w *wordsStruct) addString(str string) {
150
151
}
151
152
152
153
func (w * wordsStruct ) addRawString (str string ) {
153
- w .word += str
154
+ w .buf . WriteString ( str )
154
155
w .inWord = true
155
156
}
156
157
157
158
func (w * wordsStruct ) getWords () []string {
158
- if len ( w . word ) > 0 {
159
- w .words = append (w .words , w .word )
159
+ if w . buf . Len ( ) > 0 {
160
+ w .words = append (w .words , w .buf . String () )
160
161
161
162
// Just in case we're called again by mistake
162
- w .word = ""
163
+ w .buf . Reset ()
163
164
w .inWord = false
164
165
}
165
166
return w .words
@@ -168,9 +169,14 @@ func (w *wordsStruct) getWords() []string {
168
169
// Process the word, starting at 'pos', and stop when we get to the
169
170
// end of the word or the 'stopChar' character
170
171
func (sw * shellWord ) processStopOn (stopChar rune , rawEscapes bool ) (string , []string , error ) {
171
- var result bytes.Buffer
172
+ // result buffer can't be currently shared for shellWord as it is called internally
173
+ // by processDollar
174
+ var result strings.Builder
175
+ sw .wordsBuffer .Reset ()
172
176
var words wordsStruct
177
+ words .buf = & sw .wordsBuffer
173
178
179
+ // no need to initialize all the time
174
180
var charFuncMapping = map [rune ]func () (string , error ){
175
181
'$' : sw .processDollar ,
176
182
}
0 commit comments