Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature idea: equalv #314

Open
reynir opened this issue Jun 19, 2023 · 1 comment
Open

Feature idea: equalv #314

reynir opened this issue Jun 19, 2023 · 1 comment

Comments

@reynir
Copy link
Member

reynir commented Jun 19, 2023

I found that I wanted to compare two lists of cstructs if they each combined contained the same bytes. It can easily but inefficiently be implemented using Cstruct.equal (Cstruct.concat xs) (Cstruct.concat ys). Implementing it more efficiently is less straight forward.

  let cstruct_equalv a b =
    let rec loop a b =
      match a, b with
      | [], [] -> true
      | [], _ | _, [] -> false
      | a :: a', b :: b' ->
        let l = min (Cstruct.length a) (Cstruct.length b) in
        let remainder x xs =
          if Cstruct.length x > l then
            Cstruct.sub x l (Cstruct.length x - l) :: xs
          else
            xs
        in
        Cstruct.equal (Cstruct.sub a 0 l) (Cstruct.sub b 0 l) &&
        loop (remainder a a') (remainder b b')
    in
    Cstruct.lenv a = Cstruct.lenv b &&
    loop a b

It may be a slippery slope to add more and more *v functions.

@avsm
Copy link
Member

avsm commented Jun 19, 2023

We could define a Cstruct.List module and put the various (len, equal, compare) functions in there for efficient operations over lists of Cstructs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants