@@ -522,51 +522,56 @@ func PrintEnv(w io.Writer, env []cfg.EnvVar, onlyChanged bool) {
522
522
}
523
523
}
524
524
525
+ // isWindowsUnquotableRune reports whether r can't be quoted in a
526
+ // Windows "set" command.
527
+ // These runes will be replaced by the Unicode replacement character.
528
+ func isWindowsUnquotableRune (r rune ) bool {
529
+ if r == '\r' || r == '\n' {
530
+ return true
531
+ }
532
+ return ! unicode .IsGraphic (r ) && ! unicode .IsSpace (r )
533
+ }
534
+
525
535
func hasNonGraphic (s string ) bool {
526
- for _ , c := range []byte (s ) {
527
- if c == '\r' || c == '\n' || (! unicode .IsGraphic (rune (c )) && ! unicode .IsSpace (rune (c ))) {
528
- return true
529
- }
530
- }
531
- return false
536
+ return strings .ContainsFunc (s , isWindowsUnquotableRune )
532
537
}
533
538
534
539
func shellQuote (s string ) string {
535
- var b bytes. Buffer
536
- b .WriteByte ('\'' )
537
- for _ , x := range [] byte ( s ) {
538
- if x == '\'' {
540
+ var sb strings. Builder
541
+ sb .WriteByte ('\'' )
542
+ for _ , r := range s {
543
+ if r == '\'' {
539
544
// Close the single quoted string, add an escaped single quote,
540
545
// and start another single quoted string.
541
- b .WriteString (`'\''` )
546
+ sb .WriteString (`'\''` )
542
547
} else {
543
- b . WriteByte ( x )
548
+ sb . WriteRune ( r )
544
549
}
545
550
}
546
- b .WriteByte ('\'' )
547
- return b .String ()
551
+ sb .WriteByte ('\'' )
552
+ return sb .String ()
548
553
}
549
554
550
555
func batchEscape (s string ) string {
551
- var b bytes. Buffer
552
- for _ , x := range [] byte ( s ) {
553
- if x == '\r' || x == '\n' || ( ! unicode . IsGraphic ( rune ( x )) && ! unicode . IsSpace ( rune ( x )) ) {
554
- b .WriteRune (unicode .ReplacementChar )
556
+ var sb strings. Builder
557
+ for _ , r := range s {
558
+ if isWindowsUnquotableRune ( r ) {
559
+ sb .WriteRune (unicode .ReplacementChar )
555
560
continue
556
561
}
557
- switch x {
562
+ switch r {
558
563
case '%' :
559
- b .WriteString ("%%" )
564
+ sb .WriteString ("%%" )
560
565
case '<' , '>' , '|' , '&' , '^' :
561
566
// These are special characters that need to be escaped with ^. See
562
567
// https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/set_1.
563
- b .WriteByte ('^' )
564
- b . WriteByte ( x )
568
+ sb .WriteByte ('^' )
569
+ sb . WriteRune ( r )
565
570
default :
566
- b . WriteByte ( x )
571
+ sb . WriteRune ( r )
567
572
}
568
573
}
569
- return b .String ()
574
+ return sb .String ()
570
575
}
571
576
572
577
func printEnvAsJSON (env []cfg.EnvVar , onlyChanged bool ) {
0 commit comments