@@ -113,37 +113,17 @@ let translate_path_for_sh =
113
113
let quote_for_sh fn =
114
114
(* we lose some portability as [$'] isn't posix. This is why we prefer single
115
115
quotes when possible *)
116
- let has_single_quote = ref false in
117
- (* minimum chars for [$'']*)
118
- let posix_quote_len = ref 3 in
119
- String. iter fn ~f: (function
120
- | '\' ' ->
121
- posix_quote_len := ! posix_quote_len + 2 ;
122
- has_single_quote := true
123
- | '\\' -> posix_quote_len := ! posix_quote_len + 2
124
- | _ -> incr posix_quote_len);
125
- match ! has_single_quote with
116
+ match String. exists fn ~f: (fun c -> c = '\' ') with
126
117
| false -> " '" ^ fn ^ " '"
127
118
| true ->
128
- let len = ! posix_quote_len in
129
- let res = Bytes. create len in
130
- Bytes. set res 0 '$' ;
131
- Bytes. set res 1 '\' ';
132
- Bytes. set res (len - 1 ) '\' ';
133
- let i = ref 2 in
119
+ let buf = Buffer. create (String. length fn + 4 ) in
120
+ Buffer. add_string buf " $'" ;
134
121
String. iter fn ~f: (function
135
- | '\' ' ->
136
- Bytes. set res ! i '\\' ;
137
- Bytes. set res (! i + 1 ) '\' ';
138
- i := ! i + 2
139
- | '\\' ->
140
- Bytes. set res ! i '\\' ;
141
- Bytes. set res (! i + 1 ) '\\' ;
142
- i := ! i + 2
143
- | c ->
144
- Bytes. set res ! i c;
145
- incr i);
146
- Bytes. to_string res
122
+ | '\' ' -> Buffer. add_string buf " \\ '"
123
+ | '\\' -> Buffer. add_string buf " \\\\ "
124
+ | c -> Buffer. add_char buf c);
125
+ Buffer. add_char buf '\' ';
126
+ Buffer. contents buf
147
127
148
128
let cram_stanzas lexbuf =
149
129
let rec loop acc =
0 commit comments