From 5e07bfe8211ae379845aaffe52acd800242feb8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Proust?= Date: Wed, 4 Sep 2019 14:11:47 +1000 Subject: [PATCH 1/5] Update dune tooling and expose abstract interface --- dune-project | 2 + ocplib-endian.opam | 6 +-- src/dune | 100 +++++++++++++++++++++++++++++++++++ src/endianBigstring.cppo.ml | 62 +--------------------- src/endianBigstring.cppo.mli | 62 +--------------------- src/endianBytes.cppo.ml | 62 +--------------------- src/endianBytes.cppo.mli | 62 +--------------------- src/endianSig.ml | 90 +++++++++++++++++++++++++++++++ src/endianString.cppo.ml | 58 +------------------- src/endianString.cppo.mli | 58 ++------------------ src/jbuild | 68 ------------------------ tests/dune | 50 ++++++++++++++++++ tests/jbuild | 42 --------------- 13 files changed, 258 insertions(+), 464 deletions(-) create mode 100644 dune-project create mode 100644 src/dune create mode 100644 src/endianSig.ml delete mode 100644 src/jbuild create mode 100644 tests/dune delete mode 100644 tests/jbuild diff --git a/dune-project b/dune-project new file mode 100644 index 0000000..ae33d72 --- /dev/null +++ b/dune-project @@ -0,0 +1,2 @@ +(lang dune 1.0) +(name ocplib-endian) diff --git a/ocplib-endian.opam b/ocplib-endian.opam index 13717e0..a1f1384 100644 --- a/ocplib-endian.opam +++ b/ocplib-endian.opam @@ -16,13 +16,13 @@ bug-reports: "https://github.com/OCamlPro/ocplib-endian/issues" depends: [ "base-bytes" "cppo" {>= "1.1.0"} - "jbuilder" {build} + "dune" {build & >= "1.0"} ] build: [ - ["jbuilder" "build" "--only-packages" "ocplib-endian" "--root" "." "-j" jobs "@install"] + ["dune" "build" "-p" name "-j" jobs] ] build-test: [ - ["jbuilder" "runtest" "--only-packages" "ocplib-endian" "--root" "." "-j" jobs] + ["dune" "runtest" "-p" name "-j" jobs] ] dev-repo: "git+https://github.com/OCamlPro/ocplib-endian.git" url { diff --git a/src/dune b/src/dune new file mode 100644 index 0000000..6cacf5c --- /dev/null +++ b/src/dune @@ -0,0 +1,100 @@ +(rule + (targets endianString.mli) + (deps + (:< endianString.cppo.mli)) + (action + (run %{bin:cppo} -V OCAML:%{ocaml_version} %{<} -o %{targets}))) + +(rule + (targets endianString.ml) + (deps + (:< endianString.cppo.ml) + common.ml + common_401.ml + common_400.ml) + (action + (run %{bin:cppo} -V OCAML:%{ocaml_version} %{<} -o %{targets}))) + +(rule + (targets endianBytes.mli) + (deps + (:< endianBytes.cppo.mli)) + (action + (run %{bin:cppo} -V OCAML:%{ocaml_version} %{<} -o %{targets}))) + +(rule + (targets endianBytes.ml) + (deps + (:< endianBytes.cppo.ml) + common.ml + common_401.ml + common_400.ml) + (action + (run %{bin:cppo} -V OCAML:%{ocaml_version} %{<} -o %{targets}))) + +(rule + (targets endianBigstring.mli) + (deps + (:< endianBigstring.cppo.mli)) + (action + (run %{bin:cppo} -V OCAML:%{ocaml_version} %{<} -o %{targets}))) + +(rule + (targets endianBigstring.ml) + (deps + (:< endianBigstring.cppo.ml) + common.ml + common_401.ml + common_400.ml) + (action + (run %{bin:cppo} -V OCAML:%{ocaml_version} %{<} -o %{targets}))) + +(rule + (targets common_400.ml) + (deps + (:< common_400.cppo.ml) + be_ocaml_400.ml + le_ocaml_400.ml + ne_ocaml_400.ml + common_float.ml) + (action + (run %{bin:cppo} -V OCAML:%{ocaml_version} %{<} -o %{targets}))) + +(rule + (targets common_401.ml) + (deps + (:< common_401.cppo.ml) + be_ocaml_401.ml + le_ocaml_401.ml + ne_ocaml_401.ml + common_float.ml) + (action + (run %{bin:cppo} -V OCAML:%{ocaml_version} %{<} -o %{targets}))) + +(library + (name ocplib_endian_sig) + (public_name ocplib-endian.sig) + (synopsis + "Optimised functions to read and write int16/32/64 from strings and bytes") + (wrapped false) + (flags -w -52) + (modules endianSig) + (libraries bytes)) + +(library + (name ocplib_endian) + (public_name ocplib-endian) + (synopsis + "Optimised functions to read and write int16/32/64 from strings and bytes") + (wrapped false) + (flags -w -52) + (modules endianString endianBytes) + (libraries bytes ocplib-endian.sig)) + +(library + (name ocplib_endian_bigstring) + (public_name ocplib-endian.bigstring) + (synopsis "Optimised functions to read and write int16/32/64 from bigarrays") + (wrapped false) + (modules endianBigstring) + (libraries ocplib_endian bigarray bytes ocplib-endian.sig)) diff --git a/src/endianBigstring.cppo.ml b/src/endianBigstring.cppo.ml index 21c3956..6a2b253 100644 --- a/src/endianBigstring.cppo.ml +++ b/src/endianBigstring.cppo.ml @@ -20,66 +20,8 @@ open Bigarray type bigstring = (char, int8_unsigned_elt, c_layout) Array1.t module type EndianBigstringSig = sig - (** Functions reading according to Big Endian byte order *) - - val get_char : bigstring -> int -> char - (** [get_char buff i] reads 1 byte at offset i as a char *) - - val get_uint8 : bigstring -> int -> int - (** [get_uint8 buff i] reads 1 byte at offset i as an unsigned int of 8 - bits. i.e. It returns a value between 0 and 2^8-1 *) - - val get_int8 : bigstring -> int -> int - (** [get_int8 buff i] reads 1 byte at offset i as a signed int of 8 - bits. i.e. It returns a value between -2^7 and 2^7-1 *) - - val get_uint16 : bigstring -> int -> int - (** [get_uint16 buff i] reads 2 bytes at offset i as an unsigned int - of 16 bits. i.e. It returns a value between 0 and 2^16-1 *) - - val get_int16 : bigstring -> int -> int - (** [get_int16 buff i] reads 2 byte at offset i as a signed int of - 16 bits. i.e. It returns a value between -2^15 and 2^15-1 *) - - val get_int32 : bigstring -> int -> int32 - (** [get_int32 buff i] reads 4 bytes at offset i as an int32. *) - - val get_int64 : bigstring -> int -> int64 - (** [get_int64 buff i] reads 8 bytes at offset i as an int64. *) - - val get_float : bigstring -> int -> float - (** [get_float buff i] is equivalent to - [Int32.float_of_bits (get_int32 buff i)] *) - - val get_double : bigstring -> int -> float - (** [get_double buff i] is equivalent to - [Int64.float_of_bits (get_int64 buff i)] *) - - val set_char : bigstring -> int -> char -> unit - (** [set_char buff i v] writes [v] to [buff] at offset [i] *) - - val set_int8 : bigstring -> int -> int -> unit - (** [set_int8 buff i v] writes the least significant 8 bits of [v] - to [buff] at offset [i] *) - - val set_int16 : bigstring -> int -> int -> unit - (** [set_int16 buff i v] writes the least significant 16 bits of [v] - to [buff] at offset [i] *) - - val set_int32 : bigstring -> int -> int32 -> unit - (** [set_int32 buff i v] writes [v] to [buff] at offset [i] *) - - val set_int64 : bigstring -> int -> int64 -> unit - (** [set_int64 buff i v] writes [v] to [buff] at offset [i] *) - - val set_float : bigstring -> int -> float -> unit - (** [set_float buff i v] is equivalent to - [set_int32 buff i (Int32.bits_of_float v)] *) - - val set_double : bigstring -> int -> float -> unit - (** [set_double buff i v] is equivalent to - [set_int64 buff i (Int64.bits_of_float v)] *) - + include EndianSig.GET with type t := bigstring + include EndianSig.SET with type t := bigstring end let get_char (s:bigstring) off = diff --git a/src/endianBigstring.cppo.mli b/src/endianBigstring.cppo.mli index 24bba95..49627e6 100644 --- a/src/endianBigstring.cppo.mli +++ b/src/endianBigstring.cppo.mli @@ -19,66 +19,8 @@ open Bigarray type bigstring = (char, int8_unsigned_elt, c_layout) Array1.t module type EndianBigstringSig = sig - (** Functions reading according to Big Endian byte order *) - - val get_char : bigstring -> int -> char - (** [get_char buff i] reads 1 byte at offset i as a char *) - - val get_uint8 : bigstring -> int -> int - (** [get_uint8 buff i] reads 1 byte at offset i as an unsigned int of 8 - bits. i.e. It returns a value between 0 and 2^8-1 *) - - val get_int8 : bigstring -> int -> int - (** [get_int8 buff i] reads 1 byte at offset i as a signed int of 8 - bits. i.e. It returns a value between -2^7 and 2^7-1 *) - - val get_uint16 : bigstring -> int -> int - (** [get_uint16 buff i] reads 2 bytes at offset i as an unsigned int - of 16 bits. i.e. It returns a value between 0 and 2^16-1 *) - - val get_int16 : bigstring -> int -> int - (** [get_int16 buff i] reads 2 byte at offset i as a signed int of - 16 bits. i.e. It returns a value between -2^15 and 2^15-1 *) - - val get_int32 : bigstring -> int -> int32 - (** [get_int32 buff i] reads 4 bytes at offset i as an int32. *) - - val get_int64 : bigstring -> int -> int64 - (** [get_int64 buff i] reads 8 bytes at offset i as an int64. *) - - val get_float : bigstring -> int -> float - (** [get_float buff i] is equivalent to - [Int32.float_of_bits (get_int32 buff i)] *) - - val get_double : bigstring -> int -> float - (** [get_double buff i] is equivalent to - [Int64.float_of_bits (get_int64 buff i)] *) - - val set_char : bigstring -> int -> char -> unit - (** [set_char buff i v] writes [v] to [buff] at offset [i] *) - - val set_int8 : bigstring -> int -> int -> unit - (** [set_int8 buff i v] writes the least significant 8 bits of [v] - to [buff] at offset [i] *) - - val set_int16 : bigstring -> int -> int -> unit - (** [set_int16 buff i v] writes the least significant 16 bits of [v] - to [buff] at offset [i] *) - - val set_int32 : bigstring -> int -> int32 -> unit - (** [set_int32 buff i v] writes [v] to [buff] at offset [i] *) - - val set_int64 : bigstring -> int -> int64 -> unit - (** [set_int64 buff i v] writes [v] to [buff] at offset [i] *) - - val set_float : bigstring -> int -> float -> unit - (** [set_float buff i v] is equivalent to - [set_int32 buff i (Int32.bits_of_float v)] *) - - val set_double : bigstring -> int -> float -> unit - (** [set_double buff i v] is equivalent to - [set_int64 buff i (Int64.bits_of_float v)] *) - + include EndianSig.GET with type t := bigstring + include EndianSig.SET with type t := bigstring end module BigEndian : sig diff --git a/src/endianBytes.cppo.ml b/src/endianBytes.cppo.ml index 7c636b6..9945a6e 100644 --- a/src/endianBytes.cppo.ml +++ b/src/endianBytes.cppo.ml @@ -16,66 +16,8 @@ (************************************************************************) module type EndianBytesSig = sig - (** Functions reading according to Big Endian byte order *) - - val get_char : Bytes.t -> int -> char - (** [get_char buff i] reads 1 byte at offset i as a char *) - - val get_uint8 : Bytes.t -> int -> int - (** [get_uint8 buff i] reads 1 byte at offset i as an unsigned int of 8 - bits. i.e. It returns a value between 0 and 2^8-1 *) - - val get_int8 : Bytes.t -> int -> int - (** [get_int8 buff i] reads 1 byte at offset i as a signed int of 8 - bits. i.e. It returns a value between -2^7 and 2^7-1 *) - - val get_uint16 : Bytes.t -> int -> int - (** [get_uint16 buff i] reads 2 bytes at offset i as an unsigned int - of 16 bits. i.e. It returns a value between 0 and 2^16-1 *) - - val get_int16 : Bytes.t -> int -> int - (** [get_int16 buff i] reads 2 byte at offset i as a signed int of - 16 bits. i.e. It returns a value between -2^15 and 2^15-1 *) - - val get_int32 : Bytes.t -> int -> int32 - (** [get_int32 buff i] reads 4 bytes at offset i as an int32. *) - - val get_int64 : Bytes.t -> int -> int64 - (** [get_int64 buff i] reads 8 bytes at offset i as an int64. *) - - val get_float : Bytes.t -> int -> float - (** [get_float buff i] is equivalent to - [Int32.float_of_bits (get_int32 buff i)] *) - - val get_double : Bytes.t -> int -> float - (** [get_double buff i] is equivalent to - [Int64.float_of_bits (get_int64 buff i)] *) - - val set_char : Bytes.t -> int -> char -> unit - (** [set_char buff i v] writes [v] to [buff] at offset [i] *) - - val set_int8 : Bytes.t -> int -> int -> unit - (** [set_int8 buff i v] writes the least significant 8 bits of [v] - to [buff] at offset [i] *) - - val set_int16 : Bytes.t -> int -> int -> unit - (** [set_int16 buff i v] writes the least significant 16 bits of [v] - to [buff] at offset [i] *) - - val set_int32 : Bytes.t -> int -> int32 -> unit - (** [set_int32 buff i v] writes [v] to [buff] at offset [i] *) - - val set_int64 : Bytes.t -> int -> int64 -> unit - (** [set_int64 buff i v] writes [v] to [buff] at offset [i] *) - - val set_float : Bytes.t -> int -> float -> unit - (** [set_float buff i v] is equivalent to - [set_int32 buff i (Int32.bits_of_float v)] *) - - val set_double : Bytes.t -> int -> float -> unit - (** [set_double buff i v] is equivalent to - [set_int64 buff i (Int64.bits_of_float v)] *) - + include EndianSig.GET with type t := Bytes.t + include EndianSig.SET with type t := Bytes.t end let get_char (s:Bytes.t) off = diff --git a/src/endianBytes.cppo.mli b/src/endianBytes.cppo.mli index f5facfe..e988f76 100644 --- a/src/endianBytes.cppo.mli +++ b/src/endianBytes.cppo.mli @@ -16,66 +16,8 @@ (************************************************************************) module type EndianBytesSig = sig - (** Functions reading according to Big Endian byte order *) - - val get_char : Bytes.t -> int -> char - (** [get_char buff i] reads 1 byte at offset i as a char *) - - val get_uint8 : Bytes.t -> int -> int - (** [get_uint8 buff i] reads 1 byte at offset i as an unsigned int of 8 - bits. i.e. It returns a value between 0 and 2^8-1 *) - - val get_int8 : Bytes.t -> int -> int - (** [get_int8 buff i] reads 1 byte at offset i as a signed int of 8 - bits. i.e. It returns a value between -2^7 and 2^7-1 *) - - val get_uint16 : Bytes.t -> int -> int - (** [get_uint16 buff i] reads 2 bytes at offset i as an unsigned int - of 16 bits. i.e. It returns a value between 0 and 2^16-1 *) - - val get_int16 : Bytes.t -> int -> int - (** [get_int16 buff i] reads 2 byte at offset i as a signed int of - 16 bits. i.e. It returns a value between -2^15 and 2^15-1 *) - - val get_int32 : Bytes.t -> int -> int32 - (** [get_int32 buff i] reads 4 bytes at offset i as an int32. *) - - val get_int64 : Bytes.t -> int -> int64 - (** [get_int64 buff i] reads 8 bytes at offset i as an int64. *) - - val get_float : Bytes.t -> int -> float - (** [get_float buff i] is equivalent to - [Int32.float_of_bits (get_int32 buff i)] *) - - val get_double : Bytes.t -> int -> float - (** [get_double buff i] is equivalent to - [Int64.float_of_bits (get_int64 buff i)] *) - - val set_char : Bytes.t -> int -> char -> unit - (** [set_char buff i v] writes [v] to [buff] at offset [i] *) - - val set_int8 : Bytes.t -> int -> int -> unit - (** [set_int8 buff i v] writes the least significant 8 bits of [v] - to [buff] at offset [i] *) - - val set_int16 : Bytes.t -> int -> int -> unit - (** [set_int16 buff i v] writes the least significant 16 bits of [v] - to [buff] at offset [i] *) - - val set_int32 : Bytes.t -> int -> int32 -> unit - (** [set_int32 buff i v] writes [v] to [buff] at offset [i] *) - - val set_int64 : Bytes.t -> int -> int64 -> unit - (** [set_int64 buff i v] writes [v] to [buff] at offset [i] *) - - val set_float : Bytes.t -> int -> float -> unit - (** [set_float buff i v] is equivalent to - [set_int32 buff i (Int32.bits_of_float v)] *) - - val set_double : Bytes.t -> int -> float -> unit - (** [set_double buff i v] is equivalent to - [set_int64 buff i (Int64.bits_of_float v)] *) - + include EndianSig.GET with type t := Bytes.t + include EndianSig.SET with type t := Bytes.t end module BigEndian : sig diff --git a/src/endianSig.ml b/src/endianSig.ml new file mode 100644 index 0000000..3125bda --- /dev/null +++ b/src/endianSig.ml @@ -0,0 +1,90 @@ +(************************************************************************) +(* ocplib-endian *) +(* *) +(* Copyright 2014 OCamlPro *) +(* *) +(* This file is distributed under the terms of the GNU Lesser General *) +(* Public License as published by the Free Software Foundation; either *) +(* version 2.1 of the License, or (at your option) any later version, *) +(* with the OCaml static compilation exception. *) +(* *) +(* ocplib-endian is distributed in the hope that it will be useful, *) +(* but WITHOUT ANY WARRANTY; without even the implied warranty of *) +(* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *) +(* GNU General Public License for more details. *) +(* *) +(************************************************************************) + +module type GET = sig + (** Byte-order-aware functions for reading *) + + type t + (** The type of buffers/containers/etc. that are read from. *) + + val get_char : t -> int -> char + (** [get_char buff i] reads 1 byte at offset i as a char *) + + val get_uint8 : t -> int -> int + (** [get_uint8 buff i] reads 1 byte at offset i as an unsigned int of 8 + bits. i.e. It returns a value between 0 and 2^8-1 *) + + val get_int8 : t -> int -> int + (** [get_int8 buff i] reads 1 byte at offset i as a signed int of 8 + bits. i.e. It returns a value between -2^7 and 2^7-1 *) + + val get_uint16 : t -> int -> int + (** [get_uint16 buff i] reads 2 bytes at offset i as an unsigned int + of 16 bits. i.e. It returns a value between 0 and 2^16-1 *) + + val get_int16 : t -> int -> int + (** [get_int16 buff i] reads 2 byte at offset i as a signed int of + 16 bits. i.e. It returns a value between -2^15 and 2^15-1 *) + + val get_int32 : t -> int -> int32 + (** [get_int32 buff i] reads 4 bytes at offset i as an int32. *) + + val get_int64 : t -> int -> int64 + (** [get_int64 buff i] reads 8 bytes at offset i as an int64. *) + + val get_float : t -> int -> float + (** [get_float buff i] is equivalent to + [Int32.float_of_bits (get_int32 buff i)] *) + + val get_double : t -> int -> float + (** [get_double buff i] is equivalent to + [Int64.float_of_bits (get_int64 buff i)] *) + +end + +module type SET = sig + (** Byte-order-aware functions for writing *) + + type t + (** The type of buffers/containers/etc. that are written to. *) + + val set_char : t -> int -> char -> unit + (** [set_char buff i v] writes [v] to [buff] at offset [i] *) + + val set_int8 : t -> int -> int -> unit + (** [set_int8 buff i v] writes the least significant 8 bits of [v] + to [buff] at offset [i] *) + + val set_int16 : t -> int -> int -> unit + (** [set_int16 buff i v] writes the least significant 16 bits of [v] + to [buff] at offset [i] *) + + val set_int32 : t -> int -> int32 -> unit + (** [set_int32 buff i v] writes [v] to [buff] at offset [i] *) + + val set_int64 : t -> int -> int64 -> unit + (** [set_int64 buff i v] writes [v] to [buff] at offset [i] *) + + val set_float : t -> int -> float -> unit + (** [set_float buff i v] is equivalent to + [set_int32 buff i (Int32.bits_of_float v)] *) + + val set_double : t -> int -> float -> unit + (** [set_double buff i v] is equivalent to + [set_int64 buff i (Int64.bits_of_float v)] *) + +end diff --git a/src/endianString.cppo.ml b/src/endianString.cppo.ml index 47e5617..c702bd6 100644 --- a/src/endianString.cppo.ml +++ b/src/endianString.cppo.ml @@ -16,62 +16,8 @@ (************************************************************************) module type EndianStringSig = sig - (** Functions reading according to Big Endian byte order *) - - val get_char : string -> int -> char - (** [get_char buff i] reads 1 byte at offset i as a char *) - - val get_uint8 : string -> int -> int - (** [get_uint8 buff i] reads 1 byte at offset i as an unsigned int of 8 - bits. i.e. It returns a value between 0 and 2^8-1 *) - - val get_int8 : string -> int -> int - (** [get_int8 buff i] reads 1 byte at offset i as a signed int of 8 - bits. i.e. It returns a value between -2^7 and 2^7-1 *) - - val get_uint16 : string -> int -> int - (** [get_uint16 buff i] reads 2 bytes at offset i as an unsigned int - of 16 bits. i.e. It returns a value between 0 and 2^16-1 *) - - val get_int16 : string -> int -> int - (** [get_int16 buff i] reads 2 byte at offset i as a signed int of - 16 bits. i.e. It returns a value between -2^15 and 2^15-1 *) - - val get_int32 : string -> int -> int32 - (** [get_int32 buff i] reads 4 bytes at offset i as an int32. *) - - val get_int64 : string -> int -> int64 - (** [get_int64 buff i] reads 8 bytes at offset i as an int64. *) - - val get_float : string -> int -> float - (** [get_float buff i] is equivalent to - [Int32.float_of_bits (get_int32 buff i)] *) - - val get_double : string -> int -> float - (** [get_double buff i] is equivalent to - [Int64.float_of_bits (get_int64 buff i)] *) - - val set_char : Bytes.t -> int -> char -> unit - (** @deprecated This is a deprecated alias of {!endianBytes.set_char}. *) - - val set_int8 : Bytes.t -> int -> int -> unit - (** @deprecated This is a deprecated alias of {!endianBytes.set_int8}. *) - - val set_int16 : Bytes.t -> int -> int -> unit - (** @deprecated This is a deprecated alias of {!endianBytes.set_int16}. *) - - val set_int32 : Bytes.t -> int -> int32 -> unit - (** @deprecated This is a deprecated alias of {!endianBytes.set_int32}. *) - - val set_int64 : Bytes.t -> int -> int64 -> unit - (** @deprecated This is a deprecated alias of {!endianBytes.set_int64}. *) - - val set_float : Bytes.t -> int -> float -> unit - (** @deprecated This is a deprecated alias of {!endianBytes.set_float}. *) - - val set_double : Bytes.t -> int -> float -> unit - (** @deprecated This is a deprecated alias of {!endianBytes.set_double}. *) - + include EndianSig.GET with type t := string + include EndianSig.SET with type t := Bytes.t end let get_char (s:string) off = diff --git a/src/endianString.cppo.mli b/src/endianString.cppo.mli index 67f6818..2bfdb55 100644 --- a/src/endianString.cppo.mli +++ b/src/endianString.cppo.mli @@ -16,62 +16,10 @@ (************************************************************************) module type EndianStringSig = sig - (** Functions reading according to Big Endian byte order *) - - val get_char : string -> int -> char - (** [get_char buff i] reads 1 byte at offset i as a char *) - - val get_uint8 : string -> int -> int - (** [get_uint8 buff i] reads 1 byte at offset i as an unsigned int of 8 - bits. i.e. It returns a value between 0 and 2^8-1 *) - - val get_int8 : string -> int -> int - (** [get_int8 buff i] reads 1 byte at offset i as a signed int of 8 - bits. i.e. It returns a value between -2^7 and 2^7-1 *) - - val get_uint16 : string -> int -> int - (** [get_uint16 buff i] reads 2 bytes at offset i as an unsigned int - of 16 bits. i.e. It returns a value between 0 and 2^16-1 *) - - val get_int16 : string -> int -> int - (** [get_int16 buff i] reads 2 byte at offset i as a signed int of - 16 bits. i.e. It returns a value between -2^15 and 2^15-1 *) - - val get_int32 : string -> int -> int32 - (** [get_int32 buff i] reads 4 bytes at offset i as an int32. *) - - val get_int64 : string -> int -> int64 - (** [get_int64 buff i] reads 8 bytes at offset i as an int64. *) - - val get_float : string -> int -> float - (** [get_float buff i] is equivalent to - [Int32.float_of_bits (get_int32 buff i)] *) - - val get_double : string -> int -> float - (** [get_double buff i] is equivalent to - [Int64.float_of_bits (get_int64 buff i)] *) - - val set_char : Bytes.t -> int -> char -> unit - (** @deprecated This is a deprecated alias of {!endianBytes.set_char}. *) - - val set_int8 : Bytes.t -> int -> int -> unit - (** @deprecated This is a deprecated alias of {!endianBytes.set_int8}. *) - - val set_int16 : Bytes.t -> int -> int -> unit - (** @deprecated This is a deprecated alias of {!endianBytes.set_int16}. *) - - val set_int32 : Bytes.t -> int -> int32 -> unit - (** @deprecated This is a deprecated alias of {!endianBytes.set_int32}. *) - - val set_int64 : Bytes.t -> int -> int64 -> unit - (** @deprecated This is a deprecated alias of {!endianBytes.set_int64}. *) - - val set_float : Bytes.t -> int -> float -> unit - (** @deprecated This is a deprecated alias of {!endianBytes.set_float}. *) - - val set_double : Bytes.t -> int -> float -> unit - (** @deprecated This is a deprecated alias of {!endianBytes.set_double}. *) + include EndianSig.GET with type t := string + include EndianSig.SET with type t := Bytes.t + (** @deprecated Use [EndianBytes.set_*] directly. *) end module BigEndian : sig diff --git a/src/jbuild b/src/jbuild deleted file mode 100644 index 43434d3..0000000 --- a/src/jbuild +++ /dev/null @@ -1,68 +0,0 @@ -(jbuild_version 1) - -(rule - ((targets (endianString.mli)) - (deps (endianString.cppo.mli)) - (action (run ${bin:cppo} -V OCAML:${ocaml_version} ${<} -o ${@})))) - -(rule - ((targets (endianString.ml)) - (deps (endianString.cppo.ml common.ml common_401.ml common_400.ml)) - (action (run ${bin:cppo} -V OCAML:${ocaml_version} ${<} -o ${@})))) - -(rule - ((targets (endianBytes.mli)) - (deps (endianBytes.cppo.mli)) - (action (run ${bin:cppo} -V OCAML:${ocaml_version} ${<} -o ${@})))) - -(rule - ((targets (endianBytes.ml)) - (deps (endianBytes.cppo.ml common.ml common_401.ml common_400.ml)) - (action (run ${bin:cppo} -V OCAML:${ocaml_version} ${<} -o ${@})))) - -(rule - ((targets (endianBigstring.mli)) - (deps (endianBigstring.cppo.mli)) - (action (run ${bin:cppo} -V OCAML:${ocaml_version} ${<} -o ${@})))) - -(rule - ((targets (endianBigstring.ml)) - (deps (endianBigstring.cppo.ml common.ml common_401.ml common_400.ml)) - (action (run ${bin:cppo} -V OCAML:${ocaml_version} ${<} -o ${@})))) - -(rule - ((targets (common_400.ml)) - (deps (common_400.cppo.ml - be_ocaml_400.ml le_ocaml_400.ml ne_ocaml_400.ml common_float.ml)) - (action (run ${bin:cppo} -V OCAML:${ocaml_version} ${<} -o ${@})))) - -(rule - ((targets (common_401.ml)) - (deps (common_401.cppo.ml - be_ocaml_401.ml le_ocaml_401.ml ne_ocaml_401.ml common_float.ml)) - (action (run ${bin:cppo} -V OCAML:${ocaml_version} ${<} -o ${@})))) - -(library - ((name ocplib_endian) - (public_name ocplib-endian) - (synopsis - "Optimised functions to read and write int16/32/64 from strings and bytes") - (wrapped false) - (flags (-w -52)) - (modules (endianString endianBytes)) - (libraries (bytes)))) - -(library - ((name ocplib_endian_bigstring) - (public_name ocplib-endian.bigstring) - (synopsis - "Optimised functions to read and write int16/32/64 from bigarrays") - (wrapped false) - (modules (endianBigstring)) - (libraries (ocplib_endian bigarray bytes)))) - -(install - ((section lib) - (files (endianString.ml endianBytes.ml - (endianBigstring.ml as bigstring/endianBigstring.ml))) - (package ocplib-endian))) \ No newline at end of file diff --git a/tests/dune b/tests/dune new file mode 100644 index 0000000..ed8347d --- /dev/null +++ b/tests/dune @@ -0,0 +1,50 @@ +(rule + (targets test_string.ml) + (deps + (:< test_string.cppo.ml)) + (action + (run %{bin:cppo} -V OCAML:%{ocaml_version} %{<} -o %{targets}))) + +(rule + (targets test_bytes.ml) + (deps + (:< test_bytes.cppo.ml)) + (action + (run %{bin:cppo} -V OCAML:%{ocaml_version} %{<} -o %{targets}))) + +(rule + (targets test_bigstring.ml) + (deps + (:< test_bigstring.cppo.ml)) + (action + (run %{bin:cppo} -V OCAML:%{ocaml_version} %{<} -o %{targets}))) + +(library + (name tests) + (wrapped false) + (modules test_string test_bytes test_bigstring) + (libraries ocplib-endian ocplib-endian.bigstring bigarray bytes)) + +(executables + (names test) + (modules test) + (libraries ocplib-endian tests)) + +(executables + (names bench) + (modules bench) + (libraries ocplib-endian ocplib-endian.bigstring)) + +(alias + (name runtest) + (deps + (:< test.exe)) + (action + (run %{<}))) + +(alias + (name runtest) + (deps + (:< test.bc)) + (action + (run %{<}))) diff --git a/tests/jbuild b/tests/jbuild deleted file mode 100644 index c416997..0000000 --- a/tests/jbuild +++ /dev/null @@ -1,42 +0,0 @@ -(jbuild_version 1) - -(rule - ((targets (test_string.ml)) - (deps (test_string.cppo.ml)) - (action (run ${bin:cppo} -V OCAML:${ocaml_version} ${<} -o ${@})))) - -(rule - ((targets (test_bytes.ml)) - (deps (test_bytes.cppo.ml)) - (action (run ${bin:cppo} -V OCAML:${ocaml_version} ${<} -o ${@})))) - -(rule - ((targets (test_bigstring.ml)) - (deps (test_bigstring.cppo.ml)) - (action (run ${bin:cppo} -V OCAML:${ocaml_version} ${<} -o ${@})))) - -(library - ((name tests) - (wrapped false) - (modules (test_string test_bytes test_bigstring)) - (libraries (ocplib-endian ocplib-endian.bigstring bigarray bytes)))) - -(executables - ((names (test)) - (modules (test)) - (libraries (ocplib-endian tests)))) - -(executables - ((names (bench)) - (modules (bench)) - (libraries (ocplib-endian ocplib-endian.bigstring)))) - -(alias - ((name runtest) - (deps (test.exe)) - (action (run ${<})))) - -(alias - ((name runtest) - (deps (test.bc)) - (action (run ${<})))) From a82cab28278622f91dba5da1b40590cedd53d054 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Proust?= Date: Wed, 4 Sep 2019 14:12:18 +1000 Subject: [PATCH 2/5] Amend tests to remove warnings --- tests/test_bigstring.cppo.ml | 4 ++-- tests/test_bytes.cppo.ml | 4 ++-- tests/test_string.cppo.ml | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/test_bigstring.cppo.ml b/tests/test_bigstring.cppo.ml index ff572e8..af91d51 100644 --- a/tests/test_bigstring.cppo.ml +++ b/tests/test_bigstring.cppo.ml @@ -27,14 +27,14 @@ let assert_bound_check2 f v1 v2 = ignore(f v1 v2); assert false with - | Invalid_argument("index out of bounds") -> () + | Invalid_argument _ -> () let assert_bound_check3 f v1 v2 v3 = try ignore(f v1 v2 v3); assert false with - | Invalid_argument("index out of bounds") -> () + | Invalid_argument _ -> () let test1 () = assert_bound_check2 BE.get_int8 s (-1); diff --git a/tests/test_bytes.cppo.ml b/tests/test_bytes.cppo.ml index 24a31bd..a8f07a4 100644 --- a/tests/test_bytes.cppo.ml +++ b/tests/test_bytes.cppo.ml @@ -22,14 +22,14 @@ let assert_bound_check2 f v1 v2 = ignore(f v1 v2); assert false with - | Invalid_argument("index out of bounds") -> () + | Invalid_argument _ -> () let assert_bound_check3 f v1 v2 v3 = try ignore(f v1 v2 v3); assert false with - | Invalid_argument("index out of bounds") -> () + | Invalid_argument _ -> () let test1 () = assert_bound_check2 BE.get_int8 (to_t s) (-1); diff --git a/tests/test_string.cppo.ml b/tests/test_string.cppo.ml index 6f5f9a9..2eca7bf 100644 --- a/tests/test_string.cppo.ml +++ b/tests/test_string.cppo.ml @@ -22,14 +22,14 @@ let assert_bound_check2 f v1 v2 = ignore(f v1 v2); assert false with - | Invalid_argument("index out of bounds") -> () + | Invalid_argument _ -> () let assert_bound_check3 f v1 v2 v3 = try ignore(f v1 v2 v3); assert false with - | Invalid_argument("index out of bounds") -> () + | Invalid_argument _ -> () let test1 () = assert_bound_check2 BE.get_int8 (to_t s) (-1); From a7ae7296c5bcef9d4ccec7bbba2844816ea314ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Proust?= Date: Wed, 4 Sep 2019 14:41:15 +1000 Subject: [PATCH 3/5] Remove deprecated functions and simplify functorisation --- src/common_400.cppo.ml | 12 ++++++++++++ src/common_401.cppo.ml | 12 ++++++++++++ src/endianBigstring.cppo.ml | 4 ++-- src/endianBigstring.cppo.mli | 3 +-- src/endianBytes.cppo.ml | 5 +++-- src/endianBytes.cppo.mli | 3 +-- src/endianSig.ml | 10 ++++++++-- src/endianString.cppo.ml | 5 +++-- src/endianString.cppo.mli | 5 +---- tests/test_string.cppo.ml | 15 ++++++++++++--- 10 files changed, 55 insertions(+), 19 deletions(-) diff --git a/src/common_400.cppo.ml b/src/common_400.cppo.ml index e5d4c40..811edaa 100644 --- a/src/common_400.cppo.ml +++ b/src/common_400.cppo.ml @@ -1,5 +1,7 @@ module BigEndian = struct + type nonrec t = t + let get_char = get_char let get_uint8 = get_uint8 let get_int8 = get_int8 @@ -13,6 +15,8 @@ end module BigEndian_unsafe = struct + type nonrec t = t + let get_char = unsafe_get_char let get_uint8 = unsafe_get_uint8 let get_int8 = unsafe_get_int8 @@ -26,6 +30,8 @@ end module LittleEndian = struct + type nonrec t = t + let get_char = get_char let get_uint8 = get_uint8 let get_int8 = get_int8 @@ -39,6 +45,8 @@ end module LittleEndian_unsafe = struct + type nonrec t = t + let get_char = unsafe_get_char let get_uint8 = unsafe_get_uint8 let get_int8 = unsafe_get_int8 @@ -53,6 +61,8 @@ end #if OCAML_VERSION >= (4, 00, 0) module NativeEndian = struct + type nonrec t = t + let get_char = get_char let get_uint8 = get_uint8 let get_int8 = get_int8 @@ -66,6 +76,8 @@ end module NativeEndian_unsafe = struct + type nonrec t = t + let get_char = unsafe_get_char let get_uint8 = unsafe_get_uint8 let get_int8 = unsafe_get_int8 diff --git a/src/common_401.cppo.ml b/src/common_401.cppo.ml index eba9509..87dd023 100644 --- a/src/common_401.cppo.ml +++ b/src/common_401.cppo.ml @@ -5,6 +5,8 @@ external swapnative : nativeint -> nativeint = "%bswap_native" module BigEndian = struct + type nonrec t = t + let get_char = get_char let get_uint8 = get_uint8 let get_int8 = get_int8 @@ -18,6 +20,8 @@ end module BigEndian_unsafe = struct + type nonrec t = t + let get_char = unsafe_get_char let get_uint8 = unsafe_get_uint8 let get_int8 = unsafe_get_int8 @@ -37,6 +41,8 @@ end module LittleEndian = struct + type nonrec t = t + let get_char = get_char let get_uint8 = get_uint8 let get_int8 = get_int8 @@ -50,6 +56,8 @@ end module LittleEndian_unsafe = struct + type nonrec t = t + let get_char = unsafe_get_char let get_uint8 = unsafe_get_uint8 let get_int8 = unsafe_get_int8 @@ -69,6 +77,8 @@ end module NativeEndian = struct + type nonrec t = t + let get_char = get_char let get_uint8 = get_uint8 let get_int8 = get_int8 @@ -82,6 +92,8 @@ end module NativeEndian_unsafe = struct + type nonrec t = t + let get_char = unsafe_get_char let get_uint8 = unsafe_get_uint8 let get_int8 = unsafe_get_int8 diff --git a/src/endianBigstring.cppo.ml b/src/endianBigstring.cppo.ml index 6a2b253..f79e175 100644 --- a/src/endianBigstring.cppo.ml +++ b/src/endianBigstring.cppo.ml @@ -18,10 +18,10 @@ open Bigarray type bigstring = (char, int8_unsigned_elt, c_layout) Array1.t +type t = bigstring module type EndianBigstringSig = sig - include EndianSig.GET with type t := bigstring - include EndianSig.SET with type t := bigstring + include EndianSig.RW with type t = bigstring end let get_char (s:bigstring) off = diff --git a/src/endianBigstring.cppo.mli b/src/endianBigstring.cppo.mli index 49627e6..923b3a5 100644 --- a/src/endianBigstring.cppo.mli +++ b/src/endianBigstring.cppo.mli @@ -19,8 +19,7 @@ open Bigarray type bigstring = (char, int8_unsigned_elt, c_layout) Array1.t module type EndianBigstringSig = sig - include EndianSig.GET with type t := bigstring - include EndianSig.SET with type t := bigstring + include EndianSig.RW with type t = bigstring end module BigEndian : sig diff --git a/src/endianBytes.cppo.ml b/src/endianBytes.cppo.ml index 9945a6e..88f3c92 100644 --- a/src/endianBytes.cppo.ml +++ b/src/endianBytes.cppo.ml @@ -15,9 +15,10 @@ (* *) (************************************************************************) +type t = Bytes.t + module type EndianBytesSig = sig - include EndianSig.GET with type t := Bytes.t - include EndianSig.SET with type t := Bytes.t + include EndianSig.RW with type t = Bytes.t end let get_char (s:Bytes.t) off = diff --git a/src/endianBytes.cppo.mli b/src/endianBytes.cppo.mli index e988f76..885827d 100644 --- a/src/endianBytes.cppo.mli +++ b/src/endianBytes.cppo.mli @@ -16,8 +16,7 @@ (************************************************************************) module type EndianBytesSig = sig - include EndianSig.GET with type t := Bytes.t - include EndianSig.SET with type t := Bytes.t + include EndianSig.RW with type t = Bytes.t end module BigEndian : sig diff --git a/src/endianSig.ml b/src/endianSig.ml index 3125bda..31233b5 100644 --- a/src/endianSig.ml +++ b/src/endianSig.ml @@ -15,7 +15,7 @@ (* *) (************************************************************************) -module type GET = sig +module type R = sig (** Byte-order-aware functions for reading *) type t @@ -56,7 +56,7 @@ module type GET = sig end -module type SET = sig +module type W = sig (** Byte-order-aware functions for writing *) type t @@ -88,3 +88,9 @@ module type SET = sig [set_int64 buff i (Int64.bits_of_float v)] *) end + +module type RW = sig + type t + include R with type t := t + include W with type t := t +end diff --git a/src/endianString.cppo.ml b/src/endianString.cppo.ml index c702bd6..39ea784 100644 --- a/src/endianString.cppo.ml +++ b/src/endianString.cppo.ml @@ -15,9 +15,10 @@ (* *) (************************************************************************) +type t = string + module type EndianStringSig = sig - include EndianSig.GET with type t := string - include EndianSig.SET with type t := Bytes.t + include EndianSig.R with type t = string end let get_char (s:string) off = diff --git a/src/endianString.cppo.mli b/src/endianString.cppo.mli index 2bfdb55..0acfc0e 100644 --- a/src/endianString.cppo.mli +++ b/src/endianString.cppo.mli @@ -16,10 +16,7 @@ (************************************************************************) module type EndianStringSig = sig - include EndianSig.GET with type t := string - - include EndianSig.SET with type t := Bytes.t - (** @deprecated Use [EndianBytes.set_*] directly. *) + include EndianSig.R with type t = string end module BigEndian : sig diff --git a/tests/test_string.cppo.ml b/tests/test_string.cppo.ml index 2eca7bf..1e6c421 100644 --- a/tests/test_string.cppo.ml +++ b/tests/test_string.cppo.ml @@ -3,10 +3,19 @@ open EndianString let to_t = Bytes.unsafe_to_string (* do not allocate to avoid breaking tests *) -module BE = BigEndian -module LE = LittleEndian +module BE = struct + include (BigEndian : EndianSig.R with type t = string) + include (EndianBytes.BigEndian : EndianSig.W with type t := Bytes.t) +end +module LE = struct + include (LittleEndian : EndianSig.R with type t = string) + include (EndianBytes.LittleEndian : EndianSig.W with type t := Bytes.t) +end #if OCAML_VERSION >= (4, 00, 0) -module NE = NativeEndian +module NE = struct + include (NativeEndian : EndianSig.R with type t = string) + include (EndianBytes.NativeEndian : EndianSig.W with type t := Bytes.t) +end #endif #if OCAML_VERSION >= (4, 00, 0) From c48dd4f852858172d46880bd63f6f9c6a1142947 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Proust?= Date: Wed, 4 Sep 2019 14:59:37 +1000 Subject: [PATCH 4/5] Travis: explicitely list versions of ocaml --- .travis.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index ddef687..021febe 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,7 +3,9 @@ sudo: required install: wget https://raw.githubusercontent.com/ocaml/ocaml-ci-scripts/master/.travis-opam.sh script: bash -ex .travis-opam.sh env: - - OCAML_VERSION=latest TESTS=true PACKAGE=ocplib-endian + - OCAML_VERSION=4.07 TESTS=true PACKAGE=ocplib-endian + - OCAML_VERSION=4.06 TESTS=true PACKAGE=ocplib-endian + - OCAML_VERSION=4.05 TESTS=true PACKAGE=ocplib-endian - OCAML_VERSION=4.04 TESTS=true PACKAGE=ocplib-endian - OCAML_VERSION=4.03 TESTS=true PACKAGE=ocplib-endian - OCAML_VERSION=4.02 TESTS=true PACKAGE=ocplib-endian From 75b39969bdb95aaab988ad9ac605977497259c7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Proust?= Date: Thu, 14 Nov 2019 13:44:15 -1000 Subject: [PATCH 5/5] Rename R/W into GET/SET --- src/endianBigstring.cppo.ml | 2 +- src/endianBigstring.cppo.mli | 2 +- src/endianBytes.cppo.ml | 2 +- src/endianBytes.cppo.mli | 2 +- src/endianSig.ml | 10 +++++----- src/endianString.cppo.ml | 2 +- src/endianString.cppo.mli | 2 +- 7 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/endianBigstring.cppo.ml b/src/endianBigstring.cppo.ml index f79e175..0b75935 100644 --- a/src/endianBigstring.cppo.ml +++ b/src/endianBigstring.cppo.ml @@ -21,7 +21,7 @@ type bigstring = (char, int8_unsigned_elt, c_layout) Array1.t type t = bigstring module type EndianBigstringSig = sig - include EndianSig.RW with type t = bigstring + include EndianSig.FULL with type t = bigstring end let get_char (s:bigstring) off = diff --git a/src/endianBigstring.cppo.mli b/src/endianBigstring.cppo.mli index 923b3a5..7f76d0b 100644 --- a/src/endianBigstring.cppo.mli +++ b/src/endianBigstring.cppo.mli @@ -19,7 +19,7 @@ open Bigarray type bigstring = (char, int8_unsigned_elt, c_layout) Array1.t module type EndianBigstringSig = sig - include EndianSig.RW with type t = bigstring + include EndianSig.FULL with type t = bigstring end module BigEndian : sig diff --git a/src/endianBytes.cppo.ml b/src/endianBytes.cppo.ml index 88f3c92..a134e27 100644 --- a/src/endianBytes.cppo.ml +++ b/src/endianBytes.cppo.ml @@ -18,7 +18,7 @@ type t = Bytes.t module type EndianBytesSig = sig - include EndianSig.RW with type t = Bytes.t + include EndianSig.FULL with type t = Bytes.t end let get_char (s:Bytes.t) off = diff --git a/src/endianBytes.cppo.mli b/src/endianBytes.cppo.mli index 885827d..94e665f 100644 --- a/src/endianBytes.cppo.mli +++ b/src/endianBytes.cppo.mli @@ -16,7 +16,7 @@ (************************************************************************) module type EndianBytesSig = sig - include EndianSig.RW with type t = Bytes.t + include EndianSig.FULL with type t = Bytes.t end module BigEndian : sig diff --git a/src/endianSig.ml b/src/endianSig.ml index 31233b5..1e6a6d2 100644 --- a/src/endianSig.ml +++ b/src/endianSig.ml @@ -15,7 +15,7 @@ (* *) (************************************************************************) -module type R = sig +module type GET = sig (** Byte-order-aware functions for reading *) type t @@ -56,7 +56,7 @@ module type R = sig end -module type W = sig +module type SET = sig (** Byte-order-aware functions for writing *) type t @@ -89,8 +89,8 @@ module type W = sig end -module type RW = sig +module type FULL = sig type t - include R with type t := t - include W with type t := t + include GET with type t := t + include SET with type t := t end diff --git a/src/endianString.cppo.ml b/src/endianString.cppo.ml index 39ea784..9aa7dda 100644 --- a/src/endianString.cppo.ml +++ b/src/endianString.cppo.ml @@ -18,7 +18,7 @@ type t = string module type EndianStringSig = sig - include EndianSig.R with type t = string + include EndianSig.GET with type t = string end let get_char (s:string) off = diff --git a/src/endianString.cppo.mli b/src/endianString.cppo.mli index 0acfc0e..65fb6c2 100644 --- a/src/endianString.cppo.mli +++ b/src/endianString.cppo.mli @@ -16,7 +16,7 @@ (************************************************************************) module type EndianStringSig = sig - include EndianSig.R with type t = string + include EndianSig.GET with type t = string end module BigEndian : sig