let unescape s ~error =
let buf = Buffer.create (String.length s) in
let rec loop pos =
match String.lfindi s ~pos ~f:(fun _ c -> (=) '%' c) with
| None ->
Buffer.add_substring buf s pos String.(length s - pos)
| Some idx ->
if String.length s >= idx + 2 then (
let char = Scanf.sscanf (String.sub s (idx + 1) 2) "%x" ident in
Buffer.add_substring buf s pos String.(idx - pos);
Buffer.add_char buf (Char.of_int_exn char);
loop (idx + 3)
) else (
failwith "A"
)
in
try loop 0; Ok (Buffer.contents buf) with
| e -> Error (error s)