gstring.mli

   1: (* 
   2:    Gstring: mixed mutable and immutable string type based on the standard 
   3:    string type.
   4: 
   5:    This is just a demonstration, but anyone is welcome to use
   6:    and modify any part of this package.
   7: 
   8:    Author: Martin Jambon
   9:    Date: May 2006
  10: *)
  11: 
  12: (* Polymorphic string type which includes read-only, write-only and read-write
  13:    string types: *)
  14: type 'a gstring constraint 'a = [< `R | `W ]
  15: 
  16: (* Other polymorphic types. Readability refers to the content of the string,
  17:    not its length which is always accessible, even in the write-only type. *)
  18: type 'a readable = 'a gstring constraint 'a = [> `R]
  19: type 'a writable = 'a gstring constraint 'a = [> `W]
  20: 
  21: (* Non polymorphic types: read-only, read-write and write-only *)
  22: type rstring = [ `R ] gstring
  23: type rwstring = [ `R | `W ] gstring
  24: type wstring = [ `W ] gstring
  25: 
  26: (* The goal of this module is to provide constant strings, 
  27:    so it is the "default" type: *)
  28: type t = rstring
  29: 
  30: (* Type conversions from/to the standard string type (shared, no copy) *)
  31: external import : string -> 'a writable = "%identity"
  32: external export : 'a writable -> string = "%identity"
  33: 
  34: (* Conversions involving copy (safe) *)
  35: val freeze : 'a readable -> rstring
  36: val freeze_import : string -> rstring
  37: 
  38: (* Conversion without copy! The argument is physically the same
  39:    as the result. *)
  40: external unsafe_freeze : 'a readable -> rstring = "%identity"
  41: external unsafe_import : string -> rstring = "%identity"
  42: 
  43: (* Shortcuts *)
  44: module Op :
  45: sig
  46:   (* import + freeze *)
  47:   val ( !! ) : string -> rstring
  48:   (* import *)
  49:   external ( !% ) : string -> 'a writable = "%identity"
  50:   (* mixed append *)
  51:   val ( ^% ) : 'a readable -> 'b readable -> rstring
  52: end
  53: 
  54: (* Copy (export) *)
  55: val copy : 'a readable -> string
  56: 
  57: (* Stuff from Pervasives *)
  58: val output : out_channel -> 'a readable -> unit
  59: val print : 'a readable -> unit
  60: val print_endline : 'a readable -> unit
  61: val append : 'a readable -> 'a readable -> 'a readable
  62: 
  63: (* Modified interface of String *)
  64: external length : 'a gstring -> int = "%string_length"
  65: external get : 'a readable -> int -> char = "%string_safe_get"
  66: external set : 'a writable -> int -> char -> unit = "%string_safe_set"
  67: external create : int -> 'a writable = "caml_create_string"
  68: val make : int -> char -> rstring
  69: val sub : 'a readable -> int -> int -> 'a readable
  70: val fill : 'a writable -> int -> int -> char -> unit
  71: val blit : 'a readable -> int -> 'b writable -> int -> int -> unit
  72: val concat : 'a readable -> 'b readable list -> 'b readable
  73: val iter : (char -> unit) -> 'a readable -> unit
  74: val escaped : 'a readable -> 'a readable
  75: val index : 'a readable -> char -> int
  76: val rindex : 'a readable -> char -> int
  77: val index_from : 'a readable -> int -> char -> int
  78: val rindex_from : 'a readable -> int -> char -> int
  79: val contains : 'a readable -> char -> bool
  80: val contains_from : 'a readable -> int -> char -> bool
  81: val rcontains_from : 'a readable -> int -> char -> bool
  82: val uppercase : 'a readable -> 'a readable
  83: val lowercase : 'a readable -> 'a readable
  84: val capitalize : 'a readable -> 'a readable
  85: val uncapitalize : 'a readable -> 'a readable
  86: val compare : 'a readable -> 'a readable -> int
  87: external unsafe_get : 'a readable -> int -> char = "%string_unsafe_get"
  88: external unsafe_set : 'a writable -> int -> char -> unit = "%string_unsafe_set"
  89: external unsafe_blit : 'a readable -> int -> 'b writable -> int -> int -> unit
  90:   = "caml_blit_string" "noalloc"
  91: external unsafe_fill : 'a writable -> int -> int -> char -> unit
  92:   = "caml_fill_string" "noalloc"

gstring.ml

   1: type 'a gstring = string constraint 'a = [< `R | `W ]
   2: 
   3: type 'a readable = 'a gstring constraint 'a = [> `R]
   4: type 'a writable = 'a gstring constraint 'a = [> `W]
   5: 
   6: type rstring = [ `R ] gstring
   7: type rwstring = [ `R | `W ] gstring
   8: type wstring = [ `W ] gstring
   9: 
  10: external import : string -> rwstring = "%identity"
  11: external export : rwstring -> string = "%identity"
  12: 
  13: let freeze = String.copy
  14: let freeze_import = String.copy
  15: external unsafe_freeze : 'a readable -> rstring = "%identity"
  16: external unsafe_import : string -> rstring = "%identity"
  17: 
  18: module Op =
  19: struct
  20:   let ( !! ) = String.copy
  21:   external ( !% ) : string -> rwstring = "%identity"
  22:   let ( ^% ) = ( ^ )
  23: end
  24: 
  25: let output = output_string
  26: let print = print_string
  27: let print_endline = print_endline
  28: let append = ( ^ )
  29: 
  30: include String

pa_freeze.ml

   1: (* Camlp4 syntax extension which allows the conversion of string literals
   2:    without a copy *)
   3: 
   4: EXTEND
   5:   Pcaml.expr: LEVEL "apply" [
   6:     [ "!!"; e = [ s = STRING -> <:expr< Gstring.unsafe_import $str:s$ >>
   7:                 | e = Pcaml.expr -> <:expr< Gstring.freeze_import $e$ >> ] -> e ]
   8:   ];
   9: END

This document was generated using caml2html