1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
|
(******************************************************************************
* Core *
* *
* Copyright (C) 2008- Jane Street Holding, LLC *
* Contact: opensource@janestreet.com *
* WWW: http://www.janestreet.com/ocaml *
* *
* *
* 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. *
* *
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
* *
******************************************************************************)
(* A substring is a contiguous sequence of characters in a string. We use a
functor because we want substrings of [string] and [bigstring].
*)
open Std_internal
type bigstring = Bigstring.t
module Blit : sig
type ('src, 'dst) t =
src:'src -> src_pos:int -> dst:'dst -> dst_pos:int -> len:int -> unit
val string_string : (string, string) t
val bigstring_string : (bigstring, string) t
val string_bigstring : (string, bigstring) t
val bigstring_bigstring : (bigstring, bigstring) t
end = struct
type ('src, 'dst) t =
src:'src -> src_pos:int -> dst:'dst -> dst_pos:int -> len:int -> unit
let string_string : (string, string) t = Core_string.blit
let string_bigstring : (string, bigstring) t =
Bigstring.blit_string_bigstring
;;
let bigstring_bigstring : (bigstring, bigstring) t =
Bigstring.blit
;;
let bigstring_string : (bigstring, string) t =
Bigstring.blit_bigstring_string
;;
end
module type Base = sig
type t
val create : int -> t
val length : t -> int
val blit : (t, t) Blit.t
val blit_to_string : (t, string) Blit.t
val blit_to_bigstring : (t, bigstring) Blit.t
val blit_from_string : (string, t) Blit.t
val blit_from_bigstring : (bigstring, t) Blit.t
val of_bigstring : bigstring -> t
val of_string : string -> t
end
module type S = Substring_intf.S
module F (Base : Base) : S with type base = Base.t = struct
type base = Base.t
type t = {
base : Base.t;
pos : int;
len : int;
}
let invariant t =
assert (0 <= t.pos);
assert (0 <= t.len);
assert (t.pos + t.len <= Base.length t.base);
;;
let base t = t.base
let pos t = t.pos
let length t = t.len
let create ?pos ?len base =
let (pos, len) =
Ordered_collection_common.get_pos_len_exn ?pos ?len
~length:(Base.length base)
in
{ base = base; pos = pos; len = len; }
;;
let drop_prefix t n =
if n > t.len then
failwith "Substring.drop_prefix"
else {
base = t.base;
pos = t.pos + n;
len = t.len - n;
}
;;
let drop_suffix t n =
if n > t.len then
failwith "Substring.drop_suffix"
else {
base = t.base;
pos = t.pos;
len = t.len - n;
}
;;
let prefix t n =
if n > t.len then
failwith "Substring.prefix"
else {
base = t.base;
pos = t.pos;
len = n;
}
;;
let suffix t n =
if n > t.len then
failwith "Substring.suffix"
else {
base = t.base;
pos = t.pos + t.len - n;
len = n;
}
;;
let blit_to blit =
(); fun t ~dst ~dst_pos ->
blit ~src:t.base ~src_pos:t.pos ~dst ~dst_pos ~len:t.len
;;
let blit_to_string = blit_to Base.blit_to_string
let blit_to_bigstring = blit_to Base.blit_to_bigstring
let blit_base = blit_to Base.blit
let blit_from ~name blit =
(); fun t ~src ~src_pos ~len ->
if len > t.len then
failwithf "Substring.blit_from_%s len > substring length : %d > %d"
name len t.len ();
blit ~src ~src_pos ~dst:t.base ~dst_pos:t.pos ~len
;;
let blit_from_string = blit_from ~name:"string" Base.blit_from_string
let blit_from_bigstring = blit_from ~name:"bigstring" Base.blit_from_bigstring
let of_base base = { base = base; pos = 0; len = Base.length base }
let of_string x = of_base (Base.of_string x)
let of_bigstring x = of_base (Base.of_bigstring x)
let make create blit t =
let dst = create t.len in
blit ~src:t.base ~src_pos:t.pos ~dst ~dst_pos:0 ~len:t.len;
dst
;;
let to_string = make String.create Base.blit_to_string
let to_bigstring = make Bigstring.create Base.blit_to_bigstring
let concat_gen create_dst blit_dst ts =
let len = List.fold ts ~init:0 ~f:(fun len t -> len + length t) in
let dst = create_dst len in
ignore (List.fold ts ~init:0
~f:(fun dst_pos t ->
blit_dst t ~dst ~dst_pos;
dst_pos + length t));
dst
;;
let concat ts = of_base (concat_gen Base.create blit_base ts)
let concat_string ts = concat_gen String.create blit_to_string ts
let concat_bigstring ts = concat_gen Bigstring.create blit_to_bigstring ts
end
|