Skip to content

Commit

Permalink
Use Re
Browse files Browse the repository at this point in the history
  • Loading branch information
emillon committed Jul 13, 2021
1 parent c5e9a64 commit 9abf3f7
Showing 1 changed file with 11 additions and 16 deletions.
27 changes: 11 additions & 16 deletions lib/switch_name.ml
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,17 @@ let to_string s = s

let invalid_chars = [ '/'; ':'; '#' ]

let count_leading_dashes s =
let exception Done in
let r = ref 0 in
(try String.iter (function '-' -> incr r | _ -> raise Done) s
with Done -> ());
!r

let strip_leading_dashes s =
let n = count_leading_dashes s in
let s_len = String.length s in
String.sub s n (s_len - n)

let escape_string s =
s
|> String.map (fun c -> if List.mem c invalid_chars then '-' else c)
|> strip_leading_dashes
let strip_leading_dashes =
let open Re in
let re = compile (seq [ bos; rep1 (char '-') ]) in
replace_string re ~by:""

let remove_invalid_chars =
let open Re in
let re = compile (alt (List.map char invalid_chars)) in
replace_string re ~by:"-"

let escape_string s = s |> remove_invalid_chars |> strip_leading_dashes

let parse s =
if List.exists (fun c -> String.contains s c) invalid_chars then
Expand Down

0 comments on commit 9abf3f7

Please sign in to comment.