Add signed int extract and construction functions, and test.
[ocaml-bitstring.git] / pa_bitstring.ml
index 71eabed..a5f7c46 100644 (file)
@@ -623,7 +623,7 @@ let output_bitmatch _loc bs cases =
             * be known at runtime) but we may be able to directly access
             * the bytes in the string.
             *)
-         | P.Int, Some 8, Some field_byte_offset, _, _ ->
+         | P.Int, Some 8, Some field_byte_offset, _, signed ->
              let extract_fn = int_extract_const 8 endian signed in
 
               (* The fast-path code when everything is aligned. *)
@@ -631,13 +631,13 @@ let output_bitmatch _loc bs cases =
                <:expr<
                   let o =
                    ($lid:original_off$ lsr 3) + $`int:field_byte_offset$ in
-                  Char.code (String.unsafe_get $lid:data$ o)              
+                  Bitstring.char_code (String.unsafe_get $lid:data$ o)              
                 >> in
 
               <:expr<
                if $lid:len$ >= 8 then (
                   let v =
-                    if $lid:off_aligned$ then
+                    if not $`bool:signed$ && $lid:off_aligned$ then
                       $fastpath$
                     else
                       $extract_fn$ $lid:data$ $lid:off$ $lid:len$ 8 in
@@ -666,9 +666,17 @@ let output_bitmatch _loc bs cases =
                  | 16 ->
                      <:expr< Bitstring.$lid:name$ $lid:data$ o >>
                  | 32 ->
-                     <:expr< Bitstring.$lid:name$ $lid:data$ o 0l >>
+                     <:expr<
+                       (* must allocate a new zero each time *)
+                       let zero = Bitstring.int32_of_int 0 in
+                       Bitstring.$lid:name$ $lid:data$ o zero
+                     >>
                  | 64 ->
-                     <:expr< Bitstring.$lid:name$ $lid:data$ o 0L >>
+                     <:expr<
+                       (* must allocate a new zero each time *)
+                       let zero = Int64.of_int 0 in
+                       Bitstring.$lid:name$ $lid:data$ o zero
+                     >>
                  | _ -> assert false in
                <:expr<
                  (* Starting offset within the string. *)
@@ -981,7 +989,8 @@ let output_bitmatch _loc bs cases =
        match bind with
        | Some name ->
            <:expr<
-             let $lid:name$ = ($lid:data$, $lid:off$, $lid:len$) in
+             let $lid:name$ = ($lid:data$,
+                                $lid:original_off$, $lid:original_len$) in
              $inner$
              >>
        | None -> inner in
@@ -1115,6 +1124,10 @@ EXTEND Gram
       fields = LIST0 patt_field SEP ";";
       "}" ->
        List.concat fields
+    | "{";
+      "_";
+      "}" ->
+       []
     ]
   ];