package batteries
A community-maintained standard library extension
Install
Dune Dependency
Authors
Maintainers
Sources
v3.9.0.tar.gz
md5=ea26b5c72e6731e59d856626049cca4d
sha512=55975b62c26f6db77433a3ac31f97af609fc6789bb62ac38b267249c78fd44ff37fe81901f1cf560857b9493a6046dd37b0d1c0234c66bd59e52843aac3ce6cb
doc/src/batteries.unthreaded/batUTF8.ml.html
Source file batUTF8.ml
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 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
(** UTF-8 encoded Unicode strings. The type is normal string. *) (* Copyright (C) 2002, 2003 Yamagata Yoriyuki. *) (* 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. *) (* As a special exception to the GNU Library General Public License, you *) (* may link, statically or dynamically, a "work that uses this library" *) (* with a publicly distributed version of this library to produce an *) (* executable file containing portions of this library, and distribute *) (* that executable file under terms of your choice, without any of the *) (* additional requirements listed in clause 6 of the GNU Library General *) (* Public License. By "a publicly distributed version of this library", *) (* we mean either the unmodified Library as distributed by the authors, *) (* or a modified version of this library that is distributed under the *) (* conditions defined in clause 3 of the GNU Library General Public *) (* License. This exception does not however invalidate any other reasons *) (* why the executable file might be covered by the GNU Library General *) (* Public License . *) (* 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 *) (* You can contact the authour by sending email to *) (* yoriyuki.y@gmail.com *) type t = string let empty = "" type index = int let length0 n = if n < 0x80 then 1 else if n < 0xe0 then 2 else if n < 0xf0 then 3 else 4 let look s i = let n' = let n = Char.code (String.unsafe_get s i) in if n < 0x80 then n else if n <= 0xdf then (n - 0xc0) lsl 6 lor (0x7f land (Char.code (String.unsafe_get s (i + 1)))) else if n <= 0xef then let n' = n - 0xe0 in let m = Char.code (String.unsafe_get s (i + 1)) in let n' = n' lsl 6 lor (0x7f land m) in let m = Char.code (String.unsafe_get s (i + 2)) in n' lsl 6 lor (0x7f land m) else let n' = n - 0xf0 in let m = Char.code (String.unsafe_get s (i + 1)) in let n' = n' lsl 6 lor (0x7f land m) in let m = Char.code (String.unsafe_get s (i + 2)) in let n' = n' lsl 6 lor (0x7f land m) in let m = Char.code (String.unsafe_get s (i + 3)) in n' lsl 6 lor (0x7f land m) in BatUChar.unsafe_chr n' let next s i = let n = Char.code s.[i] in if n < 0x80 then i + 1 else if n <= 0xdf then i + 2 else if n <= 0xef then i + 3 else i + 4 let rec search_head_backward s i = if i < 0 then -1 else let n = Char.code s.[i] in if n < 0x80 || n >= 0xc2 then i else search_head_backward s (i - 1) let prev s i = search_head_backward s (i - 1) let move s i n = if n >= 0 then let rec loop i n = if n <= 0 then i else loop (next s i) (n - 1) in loop i n else let rec loop i n = if n >= 0 then i else loop (prev s i) (n + 1) in loop i n let rec nth_aux s i n = if n = 0 then i else nth_aux s (next s i) (n - 1) let nth s n = nth_aux s 0 n let first _ = 0 let last s = search_head_backward s (String.length s - 1) let out_of_range s i = i < 0 || i >= String.length s let compare_index _ i j = i - j let get s n = look s (nth s n) let add_uchar buf u = let masq = 0b111111 in let k = BatUChar.code u in if k <= 0x7f then Buffer.add_char buf (Char.unsafe_chr k) else if k <= 0x7ff then begin Buffer.add_char buf (Char.unsafe_chr (0xc0 lor (k lsr 6))); Buffer.add_char buf (Char.unsafe_chr (0x80 lor (k land masq))) end else if k <= 0xffff then begin Buffer.add_char buf (Char.unsafe_chr (0xe0 lor (k lsr 12))); Buffer.add_char buf (Char.unsafe_chr (0x80 lor ((k lsr 6) land masq))); Buffer.add_char buf (Char.unsafe_chr (0x80 lor (k land masq))); end else begin Buffer.add_char buf (Char.unsafe_chr (0xf0 + (k lsr 18))); Buffer.add_char buf (Char.unsafe_chr (0x80 lor ((k lsr 12) land masq))); Buffer.add_char buf (Char.unsafe_chr (0x80 lor ((k lsr 6) land masq))); Buffer.add_char buf (Char.unsafe_chr (0x80 lor (k land masq))); end let init len f = let buf = Buffer.create len in for c = 0 to len - 1 do add_uchar buf (f c) done; Buffer.contents buf let make len u = init len (fun _ -> u) let of_char u = make 1 u let of_string_unsafe s = s let to_string_unsafe s = s let rec length_aux s c i = if i >= String.length s then c else let n = Char.code (String.unsafe_get s i) in let k = if n < 0x80 then 1 else if n < 0xe0 then 2 else if n < 0xf0 then 3 else 4 in length_aux s (c + 1) (i + k) let length s = length_aux s 0 0 let rec iter_aux proc s i = if i >= String.length s then () else let u = look s i in proc u; iter_aux proc s (next s i) let iter proc s = iter_aux proc s 0 let rec iteri_aux f s i count = if i >= String.length s then () else let u = look s i in f u count; iteri_aux f s (next s i) (count + 1) let iteri f s = iteri_aux f s 0 0 let compare s1 s2 = String.compare s1 s2 let sub s n len = let ipos = move s (first s) n in let jpos = move s ipos len in String.sub s ipos (jpos-ipos) exception Malformed_code let validate s = let rec trail c i a = if c = 0 then a else if i >= String.length s then raise Malformed_code else let n = Char.code (String.unsafe_get s i) in if n < 0x80 || n >= 0xc0 then raise Malformed_code else trail (c - 1) (i + 1) (a lsl 6 lor (0x7f land n)) in let rec main i = if i >= String.length s then () else let n = Char.code (String.unsafe_get s i) in if n < 0x80 then main (i + 1) else if n < 0xc2 then raise Malformed_code else if n <= 0xdf then if trail 1 (i + 1) (n - 0xc0) < 0x80 then raise Malformed_code else main (i + 2) else if n <= 0xef then let n' = trail 2 (i + 1) (n - 0xe0) in if n' < 0x800 then raise Malformed_code else if n' >= 0xd800 && n' <= 0xdfff then raise Malformed_code else main (i + 3) else if n <= 0xf4 then let n = trail 3 (i + 1) (n - 0xf0) in if n < 0x10000 || n > 0x10FFFF then raise Malformed_code else main (i + 4) else raise Malformed_code in main 0 let of_ascii s = for i = 0 to String.length s - 1 do if Char.code s.[i] >= 0x80 then raise Malformed_code; done; ##V<5## String.copy s ##V>=5## s let of_latin1 s = init (String.length s) (fun i -> BatUChar.of_char s.[i]) module Buf = struct include Buffer type buf = t let add_char = add_uchar end let map f us = let b = Buf.create (length us) in iter (fun c -> Buf.add_char b (f c)) us; Buf.contents b let filter_map f us = let b = Buf.create (length us) in iter (fun c -> match f c with None -> () | Some c -> Buf.add_char b c) us; Buf.contents b let filter p us = let b = Buf.create (length us) in iter (fun c -> if p c then Buf.add_char b c) us; Buf.contents b let fold f a s = let rec loop a i = if out_of_range s i then a else let a' = f a (look s i) in loop a' (next s i) in loop a 0 let enum s = let sl = String.length s in let i = (ref (first s)) in BatEnum.from (fun () -> if !i = sl then raise BatEnum.No_more_elements else begin let c = look s !i in i := next s !i; c end ) (*$T enum "" |> of_latin1 |> enum |> BatList.of_enum = [] "foo" |> of_latin1 |> enum |> BatList.of_enum = List.map BatUChar.of_char ['f'; 'o'; 'o'] let e = of_latin1 "abcdef" |> enum in \ for _i = 0 to 2 do BatEnum.junk e done; \ let e2 = BatEnum.clone e in \ let to_string en = BatList.of_enum en |> List.map BatUChar.char_of |> BatString.implode in \ to_string e = "def" && to_string e2 = "def" init 3 (fun i -> BatUChar.of_int (1211+i)) |> enum |> BatList.of_enum = List.map BatUChar.of_int [1211;1212;1213] *) (* The last test checks that we can make a round-trip of non-ASCII strings like "һҼҽ" *) let escaped = String.escaped module ByteIndex : sig type t = string type b_idx(* = private int*) type char_idx = int val of_int_unsafe : int -> b_idx val to_int : b_idx -> int val next : t -> b_idx -> b_idx val prev : t -> b_idx -> b_idx val of_char_idx : t -> char_idx -> b_idx val at_end : t -> b_idx -> bool val out_of_range : t -> b_idx -> bool val first : b_idx val last : t -> b_idx val move : t -> b_idx -> int -> b_idx val look : t -> b_idx -> BatUChar.t end = struct type t = string type b_idx = int type char_idx = int external of_int_unsafe : int -> b_idx = "%identity" external to_int : b_idx -> int = "%identity" let look = look let next = next let prev = prev let first = 0 let last us = prev us (String.length us) let at_end us bi = bi = String.length us let out_of_range us bi = bi < 0 || bi >= String.length us let move us bi n = (* faster moving positive than negative n *) let bi = ref bi in let step = if n > 0 then next else prev in for _j = 1 to abs n do bi := step us !bi done; !bi let of_char_idx us ci = move us first ci end (* Could be improved. *) let rindex us ch = let rec aux ci bi = if ByteIndex.out_of_range us bi then raise Not_found; if ByteIndex.look us bi = ch then ci else aux (ci-1) (ByteIndex.prev us bi) in aux 0 (ByteIndex.last us) let rec contains_aux step bi us ch = if ByteIndex.out_of_range us bi then false else if ByteIndex.look us bi = ch then true else contains_aux step (step us bi) us ch let contains us ch = contains_aux ByteIndex.next ByteIndex.first us ch
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>