[wg-camlp4] getting bitstring to work with Core

David House dhouse at janestreet.com
Mon May 27 12:00:33 BST 2013


I think it's totally reasonable to expose String.unsafe_get.

Char.code is a bit uglier, since we already have Core.to_int. But, we
could probably put up with it, particularly if it were a temporary
measure.

On 27 May 2013 11:47, Anil Madhavapeddy <anil at recoil.org> wrote:
> Core currently hides two functions needed by the Bitstring syntax extension: Char.code and String.unsafe_get.  I'm not sure what the best way to work around this is. Possibilities are:
>
> - Core.Std.Caml has the original namespace from OCaml, so add a camlp4 flag to pa_bitstring to locally open Caml if the flag is specified.
>
> - Add Char.code and String.unsafe_get to Core -- but there will be more similar incompatibilities lurking.
>
> - Do nothing, and wait for the more explicit extension_points to show up.
>
> - The below hack to get it to compile. Ugh.
>
> --
> open Core.Std
>
> module Char = struct
>   include Char
>   let code = to_int
> end
>
> module String = struct
>   include String
>   let unsafe_get = Caml.String.unsafe_get
> end
>
> let show_gif_info filename () =
>   let bits = Bitstring.bitstring_of_file filename in
>
>   bitmatch bits with
>   | { ("GIF87a"|"GIF89a") : 6*8 : string; (* GIF magic. *)
>       width : 16 : littleendian;
>       height : 16 : littleendian;
>       colormap : 1;                     (* Has colormap? *)
>       colorbits : 3;                    (* Color res = colorbits+1 *)
>       sortflag : 1;
>       bps : 3;                          (* Bits/pixel = bps+1 *)
>       bg : 8;                           (* Background colour. *)
>       aspectratio : 8 } ->
>       printf "%s: GIF image:\n" filename;
>       printf "  size %d %d\n" width height;
>       printf "  has global colormap? %b\n" colormap;
>       printf "  colorbits %d\n" (colorbits+1);
>       printf "  global colormap is sorted? %b\n" sortflag;
>       printf "  bits/pixel %d\n" (bps+1);
>       printf "  background color index %d\n" bg;
>       printf "  aspect ratio %d\n" aspectratio
>
>   | { _ } ->
>       eprintf "%s: Not a GIF image\n" filename
>
> let () =
>   Command.basic ~summary:"Display information about GIF file"
>     Command.Spec.(
>       empty +>
>       anon ("filename" %: file)
>     )
>     show_gif_info
>   |> Command.run
>
> --
> You received this message because you are subscribed to the Google Groups "ocaml-core" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ocaml-core+unsubscribe at googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>


More information about the wg-camlp4 mailing list