@@ -193,7 +193,9 @@ func (p *Pattern) match(path string) (bool, error) {
193193}
194194
195195func (p * Pattern ) compile () error {
196- regStr := "^"
196+ var regStrBuilder strings.Builder
197+ regStrBuilder .WriteString ("^" )
198+
197199 pattern := p .cleanedPattern
198200 // Go through the pattern and convert it to a regexp.
199201 // We use a scanner so we can support utf-8 chars.
@@ -222,46 +224,56 @@ func (p *Pattern) compile() error {
222224
223225 if scan .Peek () == scanner .EOF {
224226 // is "**EOF" - to align with .gitignore just accept all
225- regStr += ".*"
227+ regStrBuilder . WriteString ( ".*" )
226228 } else {
227229 // is "**"
228230 // Note that this allows for any # of /'s (even 0) because
229231 // the .* will eat everything, even /'s
230- regStr += "(.*" + escSL + ")?"
232+ regStrBuilder .WriteString ("(.*" )
233+ regStrBuilder .WriteString (escSL )
234+ regStrBuilder .WriteString (")?" )
231235 }
232236 } else {
233237 // is "*" so map it to anything but "/"
234- regStr += "[^" + escSL + "]*"
238+ regStrBuilder .WriteString ("[^" )
239+ regStrBuilder .WriteString (escSL )
240+ regStrBuilder .WriteString ("]*" )
235241 }
236242 } else if ch == '?' {
237243 // "?" is any char except "/"
238- regStr += "[^" + escSL + "]"
244+ regStrBuilder .WriteString ("[^" )
245+ regStrBuilder .WriteString (escSL )
246+ regStrBuilder .WriteString ("]" )
239247 } else if ch == '.' || ch == '$' {
240248 // Escape some regexp special chars that have no meaning
241249 // in golang's filepath.Match
242- regStr += bs + string (ch )
250+ regStrBuilder .WriteString (bs )
251+ regStrBuilder .WriteRune (ch )
243252 } else if ch == '\\' {
244253 // escape next char.
245254 if sl == bs {
246255 // On windows map "\" to "\\", meaning an escaped backslash,
247256 // and then just continue because filepath.Match on
248257 // Windows doesn't allow escaping at all
249- regStr += escSL
258+ regStrBuilder . WriteString ( escSL )
250259 continue
251260 }
252261 if scan .Peek () != scanner .EOF {
253- regStr += bs + string (scan .Next ())
262+ regStrBuilder .WriteString (bs )
263+ regStrBuilder .WriteRune (scan .Next ())
254264 } else {
255265 return filepath .ErrBadPattern
256266 }
257267 } else {
258- regStr += string (ch )
268+ regStrBuilder . WriteRune (ch )
259269 }
260270 }
261271
262- regStr += "(" + escSL + ".*)?$"
272+ regStrBuilder .WriteString ("(" )
273+ regStrBuilder .WriteString (escSL )
274+ regStrBuilder .WriteString (".*)?$" )
263275
264- re , err := regexp .Compile (regStr )
276+ re , err := regexp .Compile (regStrBuilder . String () )
265277 if err != nil {
266278 return err
267279 }
0 commit comments