Skip to content

Commit bac2ec5

Browse files
committed
refactor(cram): remove micro optimizations
Signed-off-by: Rudi Grinberg <me@rgrinberg.com>
1 parent 8578a41 commit bac2ec5

File tree

1 file changed

+8
-28
lines changed

1 file changed

+8
-28
lines changed

src/dune_rules/cram_exec.ml

+8-28
Original file line numberDiff line numberDiff line change
@@ -113,37 +113,17 @@ let translate_path_for_sh =
113113
let quote_for_sh fn =
114114
(* we lose some portability as [$'] isn't posix. This is why we prefer single
115115
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
126117
| false -> "'" ^ fn ^ "'"
127118
| 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 "$'";
134121
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
147127

148128
let cram_stanzas lexbuf =
149129
let rec loop acc =

0 commit comments

Comments
 (0)