Skip to content

Feature idea: equalv #314

@reynir

Description

@reynir

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions