jobjo / popper

Property-based testing at ease
ISC License
43 stars 1 forks source link

Better way of rendered length of unicode string #1

Open jobjo opened 3 years ago

jobjo commented 3 years ago

Needed in order to determine the rendered length of a string (that is the string without markup sections for color/style). Current version:

let style_regexp = Str.regexp "\\[[0-9]+m"

let rendered_length : type a. (Format.formatter -> a -> unit) -> a -> int =
 fun f a ->
    Format.kasprintf Fun.id "%a" f a
    |> Str.global_replace style_regexp ""
    |> String.to_seq
    |> Seq.filter (fun c -> Char.code c <> 27)
    |> Seq.fold_left (fun n _ -> n + 1) 0