diff options
author | Stephane Glondu <steph@glondu.net> | 2020-01-24 15:00:47 +0100 |
---|---|---|
committer | Stéphane Glondu <steph@glondu.net> | 2020-01-24 15:00:47 +0100 |
commit | 6918f6f32b91bee183435402d82ad4af50046bf7 (patch) | |
tree | e8cad07c9d6d43f2e49f213d4942cd7cd54d3942 | |
parent | b9b15cb0caa8075c66bed4f177b797abdb5ee1bb (diff) |
New upstream version 3.1.0
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | README.md | 6 | ||||
-rw-r--r-- | bitstring.opam | 12 | ||||
-rw-r--r-- | examples/.merlin | 6 | ||||
-rw-r--r-- | ppx/.merlin | 9 | ||||
-rw-r--r-- | ppx/ppx_bitstring.ml | 14 | ||||
-rw-r--r-- | src/.merlin | 3 | ||||
-rw-r--r-- | src/bitstring.ml | 24 | ||||
-rw-r--r-- | src/bitstring.mli | 12 | ||||
-rw-r--r-- | src/bitstring_c.c | 123 | ||||
-rw-r--r-- | src/bitstring_config.ml | 9 | ||||
-rw-r--r-- | src/bitstring_fastpath.c | 195 | ||||
-rw-r--r-- | src/bitstring_types.ml | 8 | ||||
-rw-r--r-- | src/byteswap.h | 93 | ||||
-rw-r--r-- | src/jbuild | 4 | ||||
-rw-r--r-- | tests/.merlin | 9 | ||||
-rw-r--r-- | tests/BitstringConstructorTest.ml | 13 | ||||
-rw-r--r-- | tests/BitstringLegacyTest.ml | 55 | ||||
-rw-r--r-- | tests/bitstring_tests.ml | 1 |
19 files changed, 379 insertions, 218 deletions
@@ -9,6 +9,7 @@ *.so *.opt *.actual +*.merlin /_build /autom4te.cache @@ -11,7 +11,7 @@ The original `README` content can be found in the `README.orig` file. ## Documentation -The documentation is located [here](http://xguer.in/bitstring). +The documentation is located [here](https://bitstring.software). ## How to install @@ -61,6 +61,10 @@ $ jbuilder build $ jbuilder runtest ``` +## Appreciation + +Tokens of appreciation are gladly accepted in the form of [virtual coffee](https://buymeacoff.ee/xguerin). + ## License The library is licensed under the LGPL v2 or later, with the OCaml linking diff --git a/bitstring.opam b/bitstring.opam index a82b176..1cac698 100644 --- a/bitstring.opam +++ b/bitstring.opam @@ -1,12 +1,12 @@ authors : [ "Richard W.M. Jones" "Xavier R. Guérin" ] -bug-reports : "https://github.com/xguerin/bitstring/issues" -dev-repo : "https://github.com/xguerin/bitstring.git" -doc : "https://xguer.in/bitstring" -homepage : "https://github.com/xguerin/bitstring" +bug-reports : "https://bitbucket.org/thanatonauts/bitstring/issues" +dev-repo : "https://bitbucket.org/thanatonauts/bitstring.git" +doc : "https://bitstring.software" +homepage : "https://bitstring.software" license : "LGPLv2+ with exceptions and GPLv2+" maintainer : "Xavier R. Guérin <ghub@applepine.org>" opam-version : "1.2" -version : "3.0.0" +version : "3.1.0" build: [ ["jbuilder" "build" "-p" name "-j" jobs] @@ -20,7 +20,7 @@ depends: [ "jbuilder" { build } "ppx_tools_versioned" { build } "ocaml-migrate-parsetree" { build & >= "1.0.5" } - "ounit" { build } + "ounit" { test } ] conflicts: [ diff --git a/examples/.merlin b/examples/.merlin deleted file mode 100644 index b8e003b..0000000 --- a/examples/.merlin +++ /dev/null @@ -1,6 +0,0 @@ -B ../_build/default/examples -B ../_build/default/src -FLG -ppx '/Users/xguerin/Workspace/bitstring/_build/default/.ppx/bitstring.ppx/ppx.exe --as-ppx' -FLG -w -40 -w -40 -w -40 -w -40 -w -40 -w -40 -w -40 -PKG unix -S ../src diff --git a/ppx/.merlin b/ppx/.merlin deleted file mode 100644 index 901f85c..0000000 --- a/ppx/.merlin +++ /dev/null @@ -1,9 +0,0 @@ -B ../_build/default/ppx -FLG -ppx '/Users/xguerin/Workspace/bitstring/_build/default/.ppx/ppx_tools_versioned.metaquot_405/ppx.exe --as-ppx --cookie '\''library-name="ppx_bitstring"'\''' -FLG -w -40 -PKG compiler-libs -PKG compiler-libs.common -PKG ocaml-migrate-parsetree -PKG ppx_tools_versioned -PKG result -PKG str diff --git a/ppx/ppx_bitstring.ml b/ppx/ppx_bitstring.ml index 53cc6d7..a17157b 100644 --- a/ppx/ppx_bitstring.ml +++ b/ppx/ppx_bitstring.ml @@ -516,9 +516,23 @@ let parse_match_fields str = location_exn ~loc:str.loc "Invalid number of fields in statement" ;; +(* + * Some operators like the subtype cast operator (:>) can throw off the parser. + * The function below resolve these ambiguities on a case-by-case basis. + *) +let stitch_ambiguous_operators lst = + let fn e = function + | [] -> [ e ] + | hd :: tl when hd = "" || e == "" -> e :: hd :: tl + | hd :: tl when Str.first_chars hd 1 = ">" -> (e ^ ":" ^ hd) :: tl + | l -> e :: l + in + List.fold_right fn lst [] + let parse_const_fields str = let open Qualifiers in split_string ~on:":" str.txt + |> stitch_ambiguous_operators |> split_loc ~loc:str.loc |> function | [ vl; len ] -> diff --git a/src/.merlin b/src/.merlin deleted file mode 100644 index 0ca502f..0000000 --- a/src/.merlin +++ /dev/null @@ -1,3 +0,0 @@ -B ../_build/default/src -FLG -open Bitstring__ -w -40 -PKG unix diff --git a/src/bitstring.ml b/src/bitstring.ml index ac5313a..1d0b60b 100644 --- a/src/bitstring.ml +++ b/src/bitstring.ml @@ -1,5 +1,8 @@ -(* Bitstring library. - * Copyright (C) 2008 Red Hat Inc., Richard W.M. Jones +(* + * Bitstring library. + * + * Copyright (C) 2008-2016 Red Hat Inc., Richard W.M. Jones + * Copyright (C) 2016 Red Hat Inc, Richard W.M. Jones, Xavier R. Guerin. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -15,8 +18,6 @@ * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - * - * $Id$ *) open Printf @@ -1206,6 +1207,21 @@ let is_ones_bitstring ((data, off, len) as bits) = 0 = compare bits ones ) +external is_prefix_fastpath: bytes -> int -> bytes -> int -> int -> bool + = "ocaml_bitstring_is_prefix_fastpath" + +let is_prefix ((b1, o1, l1) as bs1) ((b2, o2, l2) as bs2) = + (* Fail if either bitstring is invalid *) + if l2 > l1 || l1 = 0 || l2 = 0 then + false + (* Use the fast path if the bitstrings are aligned *) + else if o1 land 7 = o2 land 7 then + is_prefix_fastpath b1 o1 b2 o2 l2 + (* Bitstrings are unaligned *) + else + let re = Str.regexp_string (string_of_bitstring bs2) in + Str.string_partial_match re (string_of_bitstring bs1) 0 + (*----------------------------------------------------------------------*) (* Bit get/set functions. *) diff --git a/src/bitstring.mli b/src/bitstring.mli index 1aaec16..4f06887 100644 --- a/src/bitstring.mli +++ b/src/bitstring.mli @@ -1,5 +1,8 @@ -(** Bitstring library. *) -(* Copyright (C) 2008 Red Hat Inc., Richard W.M. Jones +(* + * Bitstring library. + * + * Copyright (C) 2008-2016 Red Hat Inc., Richard W.M. Jones + * Copyright (C) 2016 Red Hat Inc, Richard W.M. Jones, Xavier R. Guerin. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -15,8 +18,6 @@ * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - * - * $Id$ *) (** @@ -713,6 +714,9 @@ val is_zeroes_bitstring : bitstring -> bool val is_ones_bitstring : bitstring -> bool (** Tests if the bitstring is all one bits (cf. {!ones_bitstring}). *) +val is_prefix: bitstring -> bitstring -> bool +(** [is_prefix bs1 bs2] returns true if bs2 is a prefix of bs1 *) + (** {3 Bitstring manipulation} *) val bitstring_length : bitstring -> int diff --git a/src/bitstring_c.c b/src/bitstring_c.c deleted file mode 100644 index 6c2f3ab..0000000 --- a/src/bitstring_c.c +++ /dev/null @@ -1,123 +0,0 @@ -/* Bitstring library. - * Copyright (C) 2008 Red Hat Inc., Richard W.M. Jones - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version, - * with the OCaml linking exception described in COPYING.LIB. - * - * This library 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 - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - * - * $Id: bitstring.ml 146 2008-08-20 16:58:33Z richard.wm.jones $ - */ - -/* This file contains hand-coded, optimized C implementations of - * certain very frequently used functions. - */ - -#if defined(__APPLE__) -#include <machine/endian.h> -#else -#include <endian.h> -#endif - -#include <stdio.h> -#include <stdlib.h> -#include <stdint.h> -#include <byteswap.h> -#include <string.h> - -#include <caml/mlvalues.h> -#include <caml/fail.h> -#include <caml/memory.h> -#include <caml/alloc.h> - -/* Fastpath functions. These are used in the common case for reading - * ints where the following conditions are known to be true: - * (a) the int size is a whole number of bytes (eg. 16, 24, 32, etc bits) - * (b) the access in the match is byte-aligned - * (c) the access in the underlying bitstring is byte-aligned - * - * These functions used to all be "noalloc" meaning they must not - * perform any OCaml allocations. However starting with OCaml 4.02, a - * compiler optimization means that unforunately we now have to use - * ordinary alloc functions in some cases. - * - * The final offset in the string is calculated by the OCaml (caller) - * code. All we need to do is to read the string+offset and byteswap, - * sign-extend as necessary. - * - * There is one function for every combination of: - * (i) int size: 16, 32, 64 bits - * (ii) endian: bigendian, littleendian, nativeendian - * (iii) signed and unsigned - * - * XXX Future work: Expand this to 24, 40, 48, 56 bits. This - * requires some extra work because sign-extension won't "just happen". - */ - -#if BYTE_ORDER == BIG_ENDIAN -#define swap_be(size,v) -#define swap_le(size,v) v = bswap_##size (v) -#define swap_ne(size,v) -#else -#define swap_be(size,v) v = bswap_##size (v) -#define swap_le(size,v) -#define swap_ne(size,v) -#endif - -#define fastpath1(size,endian,signed,type) \ - CAMLprim value \ - ocaml_bitstring_extract_fastpath_int##size##_##endian##_##signed \ - (value strv, value offv) \ - { \ - type *ptr = (type *) ((char *) String_val (strv) + Int_val (offv)); \ - type r; \ - memcpy(&r, ptr, sizeof(r)); \ - swap_##endian(size,r); \ - return Val_int (r); \ - } - -fastpath1(16,be,unsigned,uint16_t) -fastpath1(16,le,unsigned,uint16_t) -fastpath1(16,ne,unsigned,uint16_t) -fastpath1(16,be,signed,int16_t) -fastpath1(16,le,signed,int16_t) -fastpath1(16,ne,signed,int16_t) - -#define fastpath2(size,endian,signed,type,copy) \ - CAMLprim value \ - ocaml_bitstring_extract_fastpath_int##size##_##endian##_##signed \ - (value strv, value offv) \ - { \ - CAMLparam2 (strv, offv); \ - CAMLlocal1 (rv); \ - type *ptr = (type *) ((char *) String_val (strv) + Int_val (offv)); \ - type r; \ - memcpy(&r, ptr, sizeof(r)); \ - swap_##endian(size,r); \ - rv = copy (r); \ - CAMLreturn (rv); \ - } - -fastpath2(32,be,unsigned,uint32_t,caml_copy_int32) -fastpath2(32,le,unsigned,uint32_t,caml_copy_int32) -fastpath2(32,ne,unsigned,uint32_t,caml_copy_int32) -fastpath2(32,be,signed,int32_t,caml_copy_int32) -fastpath2(32,le,signed,int32_t,caml_copy_int32) -fastpath2(32,ne,signed,int32_t,caml_copy_int32) - -fastpath2(64,be,unsigned,uint64_t,caml_copy_int64) -fastpath2(64,le,unsigned,uint64_t,caml_copy_int64) -fastpath2(64,ne,unsigned,uint64_t,caml_copy_int64) -fastpath2(64,be,signed,int64_t,caml_copy_int64) -fastpath2(64,le,signed,int64_t,caml_copy_int64) -fastpath2(64,ne,signed,int64_t,caml_copy_int64) diff --git a/src/bitstring_config.ml b/src/bitstring_config.ml index bc8433b..a83fc8b 100644 --- a/src/bitstring_config.ml +++ b/src/bitstring_config.ml @@ -1,7 +1,8 @@ -(* Bitstring library. - * Copyright (C) 2008 Red Hat Inc., Richard W.M. Jones +(* + * Bitstring library. * - * @configure_input@ + * Copyright (C) 2008-2016 Red Hat Inc., Richard W.M. Jones + * Copyright (C) 2016 Red Hat Inc, Richard W.M. Jones, Xavier R. Guerin. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -17,8 +18,6 @@ * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - * - * $Id$ *) (* This file contains general configuration settings, set by the diff --git a/src/bitstring_fastpath.c b/src/bitstring_fastpath.c new file mode 100644 index 0000000..5d9f2a7 --- /dev/null +++ b/src/bitstring_fastpath.c @@ -0,0 +1,195 @@ +/* + * Bitstring library. + * + * Copyright (C) 2008-2016 Red Hat Inc., Richard W.M. Jones + * Copyright (C) 2016 Red Hat Inc, Richard W.M. Jones, Xavier R. Guerin. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version, + * with the OCaml linking exception described in COPYING.LIB. + * + * This library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/* This file contains hand-coded, optimized C implementations of + * certain very frequently used functions. + */ + +#if defined(__APPLE__) +#include <machine/endian.h> +#else +#include <endian.h> +#endif + +#include <stdio.h> +#include <stdlib.h> +#include <stdint.h> +#include <byteswap.h> +#include <string.h> + +#include <caml/mlvalues.h> +#include <caml/fail.h> +#include <caml/memory.h> +#include <caml/alloc.h> + +/* + * Prefix fastpath functions. + */ + +static char prefix_mask_lookup[8] = { + 0x00, 0x80, 0xC0, 0xE0, + 0xF0, 0xF8, 0xFC, 0xFE +}; + +static +int match_partial_left(int len, char source, char prefix) +{ + register char mask = ~prefix_mask_lookup[len]; + return (source & mask) == (prefix & mask); +} + +static +int match_partial_right(int len, char source, char prefix) +{ + register char mask = prefix_mask_lookup[len]; + return (source & mask) == (prefix & mask); +} + +CAMLprim value +ocaml_bitstring_is_prefix_fastpath(value b1, value o1, value b2, value o2, value l2) +{ + int il2 = Int_val(l2); + /* + * Find the beginning of the bitstrings. + */ + int bo1 = Int_val(o1) >> 3; + int bo2 = Int_val(o2) >> 3; + char * ptr1 = &((char *)String_val(b1))[bo1]; + char * ptr2 = &((char *)String_val(b2))[bo2]; + /* + * Compute the left partial match if the offset mod 8 != 0. + */ + int sh = Int_val(o2) & 0x7; + if (sh != 0) { + if (!match_partial_left(sh, *ptr1, *ptr2)) { + return Val_false; + } + il2 -= 8 - sh; + ptr1++, ptr2++; + } + /* + * Check the part of the prefix that fits in bytes using memcmp. + */ + int bl2 = il2 >> 3; + if (memcmp(ptr1, ptr2, bl2) != 0) { + return Val_false; + } + /* + * Check the remainder of the prefix if there is any. + */ + int rem = il2 & 0x7; + if (rem) { + int res = match_partial_right(rem, ptr1[bl2], ptr2[bl2]); + return Val_bool(res); + } + /* + * The prefix exists. + */ + return Val_true; +} + +/* + * Extract fastpath functions. + * + * These are used in the common case for reading ints where the following + * conditions are known to be true: + * (a) the int size is a whole number of bytes (eg. 16, 24, 32, etc bits) + * (b) the access in the match is byte-aligned + * (c) the access in the underlying bitstring is byte-aligned + * + * These functions used to all be "noalloc" meaning they must not perform any + * OCaml allocations. However starting with OCaml 4.02, a compiler optimization + * means that unforunately we now have to use ordinary alloc functions in some + * cases. + * + * The final offset in the string is calculated by the OCaml (caller) code. All + * we need to do is to read the string+offset and byteswap, sign-extend as + * necessary. + * + * There is one function for every combination of: + * (i) int size: 16, 32, 64 bits + * (ii) endian: bigendian, littleendian, nativeendian + * (iii) signed and unsigned + * + * XXX Future work: Expand this to 24, 40, 48, 56 bits. This + * requires some extra work because sign-extension won't "just happen". + */ + +#if BYTE_ORDER == BIG_ENDIAN +#define swap_be(size,v) +#define swap_le(size,v) v = bswap_##size (v) +#define swap_ne(size,v) +#else +#define swap_be(size,v) v = bswap_##size (v) +#define swap_le(size,v) +#define swap_ne(size,v) +#endif + +#define extract_fastpath_zero_copy(size, endian, sign, type) \ + CAMLprim value \ + ocaml_bitstring_extract_fastpath_int##size##_##endian##_##sign \ + (value strv, value offv) \ +{ \ + type *ptr = (type *)((char *)String_val(strv) + Int_val(offv)); \ + type r; \ + memcpy(&r, ptr, sizeof(r)); \ + swap_##endian(size,r); \ + return Val_int(r); \ +} + +#define extract_fastpath_with_copy(size, endian, sign, type) \ + CAMLprim value \ + ocaml_bitstring_extract_fastpath_int##size##_##endian##_##sign \ + (value strv, value offv) \ +{ \ + CAMLparam2 (strv, offv); \ + CAMLlocal1 (rv); \ + type *ptr = (type *)((char *)String_val(strv) + Int_val(offv)); \ + type r; \ + memcpy(&r, ptr, sizeof(r)); \ + swap_##endian(size,r); \ + rv = caml_copy_int##size(r); \ + CAMLreturn(rv); \ +} + +extract_fastpath_zero_copy(16, be, unsigned, uint16_t) +extract_fastpath_zero_copy(16, le, unsigned, uint16_t) +extract_fastpath_zero_copy(16, ne, unsigned, uint16_t) +extract_fastpath_zero_copy(16, be, signed , int16_t ) +extract_fastpath_zero_copy(16, le, signed , int16_t ) +extract_fastpath_zero_copy(16, ne, signed , int16_t ) + +extract_fastpath_with_copy(32, be, unsigned, uint32_t) +extract_fastpath_with_copy(32, le, unsigned, uint32_t) +extract_fastpath_with_copy(32, ne, unsigned, uint32_t) +extract_fastpath_with_copy(32, be, signed , int32_t ) +extract_fastpath_with_copy(32, le, signed , int32_t ) +extract_fastpath_with_copy(32, ne, signed , int32_t ) + +extract_fastpath_with_copy(64, be, unsigned, uint64_t) +extract_fastpath_with_copy(64, le, unsigned, uint64_t) +extract_fastpath_with_copy(64, ne, unsigned, uint64_t) +extract_fastpath_with_copy(64, be, signed , int64_t ) +extract_fastpath_with_copy(64, le, signed , int64_t ) +extract_fastpath_with_copy(64, ne, signed , int64_t ) + +// vim: ts=2:sts=2:sw=2:et diff --git a/src/bitstring_types.ml b/src/bitstring_types.ml index 459bc4f..6dc7b7b 100644 --- a/src/bitstring_types.ml +++ b/src/bitstring_types.ml @@ -1,6 +1,9 @@ -(* Bitstring library. - * Copyright (C) 2008 Red Hat Inc., Richard W.M. Jones +(* + * Bitstring library. * + * Copyright (C) 2008-2016 Red Hat Inc., Richard W.M. Jones + * Copyright (C) 2016 Red Hat Inc, Richard W.M. Jones, Xavier R. Guerin. + * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either @@ -16,7 +19,6 @@ * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * - * $Id$ *) type endian = BigEndian | LittleEndian | NativeEndian diff --git a/src/byteswap.h b/src/byteswap.h index 5e4652e..3affb9f 100644 --- a/src/byteswap.h +++ b/src/byteswap.h @@ -1,54 +1,63 @@ -/* byteswap.h - Byte swapping - Copyright (C) 2005, 2007 Free Software Foundation, Inc. - Written by Oskar Liljeblad <oskar@osk.mine.nu>, 2005. - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 3 of the License, or - (at your option) any later version. - - This program 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. - - You should have received a copy of the GNU General Public License - along with this program. If not, see <http://www.gnu.org/licenses/>. -*/ +/* + * byteswap.h - Byte swapping + * Copyright (C) 2005, 2007 Free Software Foundation, Inc. + * Written by Oskar Liljeblad <oskar@osk.mine.nu>, 2005. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program 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. + + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ /* NB: - - This file is from Gnulib, and in accordance with the convention - there, the real license of this file comes from the module - definition. It is really LGPLv2+. - - - RWMJ. 2008/08/23 -*/ + * + * This file is from Gnulib, and in accordance with the convention there, the + * real license of this file comes from the module definition. It is really + * LGPLv2+. + * + * - RWMJ. 2008/08/23 + */ #ifndef _GL_BYTESWAP_H #define _GL_BYTESWAP_H -/* Given an unsigned 16-bit argument X, return the value corresponding to - X with reversed byte order. */ +/* + * Given an unsigned 16-bit argument X, return the value corresponding to + * X with reversed byte order. + */ #define bswap_16(x) ((((x) & 0x00FF) << 8) | \ - (((x) & 0xFF00) >> 8)) + (((x) & 0xFF00) >> 8)) -/* Given an unsigned 32-bit argument X, return the value corresponding to - X with reversed byte order. */ +/* + * Given an unsigned 32-bit argument X, return the value corresponding to + * X with reversed byte order. + */ #define bswap_32(x) ((((x) & 0x000000FF) << 24) | \ - (((x) & 0x0000FF00) << 8) | \ - (((x) & 0x00FF0000) >> 8) | \ - (((x) & 0xFF000000) >> 24)) - -/* Given an unsigned 64-bit argument X, return the value corresponding to - X with reversed byte order. */ + (((x) & 0x0000FF00) << 8) | \ + (((x) & 0x00FF0000) >> 8) | \ + (((x) & 0xFF000000) >> 24)) + +/* + * Given an unsigned 64-bit argument X, return the value corresponding to X + * with reversed byte order. + */ #define bswap_64(x) ((((x) & 0x00000000000000FFULL) << 56) | \ - (((x) & 0x000000000000FF00ULL) << 40) | \ - (((x) & 0x0000000000FF0000ULL) << 24) | \ - (((x) & 0x00000000FF000000ULL) << 8) | \ - (((x) & 0x000000FF00000000ULL) >> 8) | \ - (((x) & 0x0000FF0000000000ULL) >> 24) | \ - (((x) & 0x00FF000000000000ULL) >> 40) | \ - (((x) & 0xFF00000000000000ULL) >> 56)) + (((x) & 0x000000000000FF00ULL) << 40) | \ + (((x) & 0x0000000000FF0000ULL) << 24) | \ + (((x) & 0x00000000FF000000ULL) << 8) | \ + (((x) & 0x000000FF00000000ULL) >> 8) | \ + (((x) & 0x0000FF0000000000ULL) >> 24) | \ + (((x) & 0x00FF000000000000ULL) >> 40) | \ + (((x) & 0xFF00000000000000ULL) >> 56)) #endif /* _GL_BYTESWAP_H */ + +// vim: ts=2:sts=2:sw=2:et @@ -3,7 +3,7 @@ (library ((name bitstring) (public_name bitstring) - (c_names (bitstring_c)) + (c_names (bitstring_fastpath)) (c_flags (-I.)) - (libraries (unix)) + (libraries (str unix)) )) diff --git a/tests/.merlin b/tests/.merlin deleted file mode 100644 index 96fa256..0000000 --- a/tests/.merlin +++ /dev/null @@ -1,9 +0,0 @@ -B ../_build/default/src -B ../_build/default/tests -FLG -ppx '/Users/xguerin/Workspace/bitstring/_build/default/.ppx/ppx_bitstring/ppx.exe --as-ppx' -FLG -w -40 -PKG bytes -PKG oUnit -PKG oUnit.advanced -PKG unix -S ../src diff --git a/tests/BitstringConstructorTest.ml b/tests/BitstringConstructorTest.ml index 3fb6061..a548017 100644 --- a/tests/BitstringConstructorTest.ml +++ b/tests/BitstringConstructorTest.ml @@ -130,6 +130,16 @@ let str_item_test context = assert_equal str result (* + * Subtyping. + *) + +let subtype_test context = + let x = 42 in + let%bitstring b = {| x : 6 |} in + let%bitstring c = {| (x :> int) : 6 |} in + assert (Bitstring.equals b c) + +(* * Test suite definition *) @@ -140,7 +150,8 @@ let suite = "BitstringConstructorTest" >::: [ "external_value_test" >:: external_value_test; "int_parser_test" >:: int_parser_test; "int32_parser_test" >:: int32_parser_test; - "str_item_test" >:: str_item_test + "str_item_test" >:: str_item_test; + "subtype_test" >:: subtype_test; ] let () = run_test_tt_main suite diff --git a/tests/BitstringLegacyTest.ml b/tests/BitstringLegacyTest.ml index 09f13aa..0fded37 100644 --- a/tests/BitstringLegacyTest.ml +++ b/tests/BitstringLegacyTest.ml @@ -1221,6 +1221,57 @@ let concat_regression_test _ = if !errors <> 0 then exit 1 +(* + * Prefix tests. + *) + +let is_prefix_basic_aligned_test _ = + (* Match mod8 bitstrings *) + let%bitstring bs1 = {| 0x1234 : 16 : bigendian |} in + let%bitstring bs2 = {| 0x12 : 8 |} in + assert_bool "Prefix failed" (Bitstring.is_prefix bs1 bs2); + (* Match other bitstrings *) + let%bitstring bs1 = {| 0x1A2 : 11 : bigendian |} in + let%bitstring bs2 = {| 0x1A : 7 |} in + assert_bool "Prefix failed" (Bitstring.is_prefix bs1 bs2) + +let is_prefix_nested_aligned_test _ = + (* Match mod8 bitstrings *) + let%bitstring bs1 = {| 0x12345678l : 32 : bigendian |} in + let%bitstring bs2 = {| 0x56 : 8 |} in + begin match%bitstring bs1 with + | {| _ : 16; n : -1 : bitstring |} -> + assert_bool "Prefix failed" (Bitstring.is_prefix n bs2) + | {| _ |} -> + assert_failure "Invalid bitstring" + end; + (* Match other bitstrings *) + begin match%bitstring bs1 with + | {| _ : 18; n : -1 : bitstring |} -> + begin match%bitstring bs2 with + | {| _ : 2; m : -1 : bitstring |} -> + assert_bool "Prefix failed" (Bitstring.is_prefix n m) + | {| _ |} -> assert_failure "Invalid bitstring" + end + | {| _ |} -> assert_failure "Invalid bitstring" + end + +let is_prefix_basic_unaligned_test _ = + let%bitstring bs1 = {| 0x1234 : 15 : bigendian |} in + let%bitstring bs2 = {| 0x12 : 7 |} in + assert_bool "Prefix failed" (Bitstring.is_prefix bs1 bs2) + +let is_prefix_nested_unaligned_test _ = + let%bitstring bs1 = {| 0x12345678l : 32 : bigendian |} in + let%bitstring bs2 = {| 0x8A : 8 |} in + match%bitstring bs1 with + | {| _ : 13; + nested : -1 : bitstring + |} -> + assert_bool "Prefix failed" (Bitstring.is_prefix nested bs2) + | {| _ |} -> + assert_failure "Invalid bitstring" + let suite = "BitstringLegacyTests" >::: [ "load_test" >:: load_test; "run_test" >:: run_test; @@ -1248,4 +1299,8 @@ let suite = "BitstringLegacyTests" >::: [ "check_bind_test" >:: check_bind_test; "as_binding_bug_test" >:: as_binding_bug_test; "concat_regression_test" >:: concat_regression_test; + "is_prefix_basic_aligned_test" >:: is_prefix_basic_aligned_test; + "is_prefix_nested_aligned_test" >:: is_prefix_nested_aligned_test; + "is_prefix_basic_unaligned_test" >:: is_prefix_basic_unaligned_test; + "is_prefix_nested_unaligned_test" >:: is_prefix_nested_unaligned_test; ] diff --git a/tests/bitstring_tests.ml b/tests/bitstring_tests.ml index 6a12316..57d2016 100644 --- a/tests/bitstring_tests.ml +++ b/tests/bitstring_tests.ml @@ -4,6 +4,7 @@ let () = [ BitstringLegacyTest.suite; BitstringParserTest.suite; + BitstringConstructorTest.suite; BitstringQualifierTest.suite; ] |> List.iter (fun t -> run_test_tt_main t) |