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 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/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/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..0b75935 100644 --- a/src/endianBigstring.cppo.ml +++ b/src/endianBigstring.cppo.ml @@ -18,68 +18,10 @@ open Bigarray type bigstring = (char, int8_unsigned_elt, c_layout) Array1.t +type t = bigstring 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.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 24bba95..7f76d0b 100644 --- a/src/endianBigstring.cppo.mli +++ b/src/endianBigstring.cppo.mli @@ -19,66 +19,7 @@ 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.FULL with type t = bigstring end module BigEndian : sig diff --git a/src/endianBytes.cppo.ml b/src/endianBytes.cppo.ml index 7c636b6..a134e27 100644 --- a/src/endianBytes.cppo.ml +++ b/src/endianBytes.cppo.ml @@ -15,67 +15,10 @@ (* *) (************************************************************************) -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)] *) +type t = Bytes.t +module type EndianBytesSig = sig + 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 f5facfe..94e665f 100644 --- a/src/endianBytes.cppo.mli +++ b/src/endianBytes.cppo.mli @@ -16,66 +16,7 @@ (************************************************************************) 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.FULL 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..1e6a6d2 --- /dev/null +++ b/src/endianSig.ml @@ -0,0 +1,96 @@ +(************************************************************************) +(* 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 + +module type FULL = sig + type 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 47e5617..9aa7dda 100644 --- a/src/endianString.cppo.ml +++ b/src/endianString.cppo.ml @@ -15,63 +15,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}. *) +type t = string +module type EndianStringSig = sig + 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 67f6818..65fb6c2 100644 --- a/src/endianString.cppo.mli +++ b/src/endianString.cppo.mli @@ -16,62 +16,7 @@ (************************************************************************) 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 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 ${<})))) 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..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) @@ -22,14 +31,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);