text
stringlengths
12
786k
let _composite u1 u2 = if 0x1100 <= u1 && u1 <= 0x1112 then begin if u2 < 0x1161 || 0x1175 < u2 then ux_none else let l = u1 - H . lbase in let v = u2 - H . vbase in H . sbase + l * H . ncount + v * H . tcount end else if 0xAC00 <= u1 && u1 <= 0xD788 && ( u1 - 0x0AC00 ) mod H ....
let composite u1 u2 = let u = _composite ( Uchar . to_int u1 ) ( Uchar . to_int u2 ) in if u = ux_none then None else Some ( Uchar . unsafe_of_int u )
type form = [ ` NFC | ` NFD | ` NFKC | ` NFKD ]
type t = { form : form ; compat : bool ; compose : bool ; boundary : int -> bool ; mutable state : state ; mutable uc : [ ` Uchar of Uchar . t ] ; mutable acc : int array ; mutable first : int ; mutable last : int ; mutable is_end : bool ; }
let create_acc ( ) = Array . make 35 ux_none
let create form = let boundary , compat , compose = match form with | ` NFC -> nfc_boundary , false , true | ` NFD -> nfd_boundary , false , false | ` NFKC -> nfkc_boundary , true , true | ` NFKD -> nfkd_boundary , true , false in { form = ( form :> form ) ; compat ; co...
let get_u n = let ` Uchar u = n . uc in Uchar . to_int u
let acc_empty n = n . first > n . last
let form n = n . form
let copy n = { n with acc = Array . copy n . acc }
let reset n = n . state <- Start ; n . uc <- u_dumb ; n . acc <- create_acc ( ) ; n . first <- 0 ; n . last <- - 1 ; n . is_end <- false
let grow_acc n = let len = Array . length n . acc in let acc ' = Array . make ( 2 * len ) ux_none in Array . blit n . acc 0 acc ' 0 len ; n . acc <- acc '
let ordered_add n u = n . last <- n . last + 1 ; if n . last = Array . length n . acc then grow_acc n ; let c = _ccc u in if c = 0 then n . acc . ( n . last ) <- u else begin let i = ref ( n . last - 1 ) in while ( ! i >= 0 && _ccc ( n . acc . ( ! i ) ) > ...
let rec add n u = if 0xAC00 <= u && u <= 0xD7A3 then begin let sindex = u - H . sbase in let l = H . lbase + ( sindex / H . ncount ) in let v = H . vbase + ( sindex mod H . ncount ) / H . tcount in let t = H . tbase + ( sindex mod H . tcount ) in if t = H . tbase then ( ...
let compose n = let rec loop ~ last_starter ~ prev_ccc i = if i > n . last then ( ) else let ccc_i = _ccc n . acc . ( i ) in let u_comp = _composite n . acc . ( last_starter ) n . acc . ( i ) in match ( u_comp = ux_none || ( ccc_i = 0 && last_starter <> i - 1 ) ) w...
let flush_next n = let ret = ` Uchar ( Uchar . unsafe_of_int n . acc . ( n . first ) ) in if n . first = n . last then ( n . first <- 0 ; n . last <- - 1 ) else ( n . first <- n . first + 1 ) ; ret
let flush_start n = if n . compose then compose n ; flush_next n
let add n = function let u = Uchar . to_int u in begin match n . state with | Boundary -> if n . boundary u then ( let prev = n . uc in n . uc <- uc ; ( prev :> ret ) ) else ( n . state <- Acc ; add n ( get_u n ) ; add n u ; ` Await ) | Acc -> if n . boundary u then ( ...
let string_X ppf s = Format . pp_open_vbox ppf 1 ; string ppf " " " ; \ for i = 0 to String . length s - 1 do if i mod 16 = 0 && i > 0 then pf ppf " \\@\ n " ; pf ppf " \\ x % 02x " ( Char . code s . [ i ] ) done ; string ppf " " " ; \ Format . pp_close_...
let string_XN ppf = function " " -> string ppf " snil " | x -> string_X ppf x
let semi ppf ( ) = string ppf " ; " ; sp ppf ( )
let iter i ( ? sep = sp ) pp ppf x = let fst = ref true in i ( fun v -> ( if ! fst then fst := false else sep ppf ( ) ) ; pp ppf v ) x
let as_array i pp ppf = pf ppf " [ @< 2 [ >|% a ] ] " |@ ( iter i ~ sep : semi pp )
let array pp = as_array Array . iter pp
let array_N pp ppf = function [ ] || -> string ppf " nil " | x -> array pp ppf x
module R = struct type _ record = | [ ] : unit record | ( ) :: : ( string * ( Format . formatter -> ' a -> unit ) ) * ' b record -> ( ' a -> ' b ) record end
let record record ppf = let field name pp_v ppf v = pf ppf " [ @< 1 >% s =@ % a ] " @ name pp_v v in let open R in let rec go : type a . ( unit -> unit ) -> a R . record -> a = fun k -> function | [ ] -> pf ppf " [ @< 2 { > % a } ] " @ ( fun _ -> k ) ( ) |...
type ' a t = { default : ' a ; l0 : ' a array array array }
let get m u = let l1 = Array . unsafe_get m . l0 ( u lsr l0_shift ) in if l1 == nil then m . default else let l2 = Array . unsafe_get l1 ( u lsr l1_shift land l1_mask ) in if l2 == nil then m . default else Array . unsafe_get l2 ( u land l2_mask )
let create default = { default ; l0 = Array . make l0_size nil }
let set m u v = if v = m . default then ( ) else let i = u lsr l0_shift in if m . l0 . ( i ) == nil then m . l0 . ( i ) <- Array . make l1_size nil ; let j = u lsr l1_shift land l1_mask in if m . l0 . ( i ) . ( j ) == nil then m . l0 . ( i ) . ( j ) <- Array...
let size v_size m = match m . l0 with let size = ref ( 3 + v_size m . default + 1 + Array . length l0 ) in for i = 0 to Array . length l0 - 1 do match l0 . ( i ) with | [ ] || -> ( ) | l1 -> size := ! size + ( 1 + Array . length l1 ) ; for j = 0 to Array . leng...
let dump pp_v ppf m = let open Uunf_fmt in record [ " default " , pp_v ; " l0 " , pp_v |> array_N |> array_N |> array ] ppf m . default m . l0
type t = { default : bool ; l0 : string array array }
let get m u = let l1 = Array . unsafe_get m . l0 ( u lsr l0_shift ) in if l1 == nil then m . default else let l2 = Array . unsafe_get l1 ( u lsr l1_shift land l1_mask ) in if l2 == snil then m . default else let k = u land l2_mask in let byte_num = k lsr 3 in let bit_num = k land 7 in let by...
let create default = { default ; l0 = Array . make l0_size nil }
let set m u b = let l2_make m = Bytes . make l2_size ( if m . default then ' \ xFF ' else ' \ x00 ' ) in if b = m . default then ( ) else let i = u lsr l0_shift in if m . l0 . ( i ) == nil then m . l0 . ( i ) <- Array . make l1_size snil ; let j = u lsr l1_shift land l...
let size m = match m . l0 with let size = ref ( 3 + 1 + Array . length l0 ) in for i = 0 to Array . length l0 - 1 do match l0 . ( i ) with | [ ] || -> ( ) | l1 -> size := ! size + 1 + Array . length l1 ; for j = 0 to Array . length l1 - 1 do size := ! size + 1 ...
let iter_blobs i m = Array . ( iter ( iter i ) ) m . l0
let dump_pp pp_v ppf m = let open Uunf_fmt in record [ " default " , bool ; " l0 " , pp_v |> array_N |> array ] ppf m . default m . l0
let dump = dump_pp pp_v
type t = { default : int ; l0 : string array array }
let get m u = let l1 = Array . get m . l0 ( u lsr l0_shift ) in if l1 == nil then m . default else let l2 = Array . unsafe_get l1 ( u lsr l1_shift land l1_mask ) in if l2 == snil then m . default else Char . code ( String . unsafe_get l2 ( u land l2_mask ) )
let create default = { default ; l0 = Array . make l0_size nil }
let set m u byte = let l2_make m = Bytes . make l2_size ( Char . chr m . default ) in if byte = m . default then ( ) else let i = u lsr l0_shift in if m . l0 . ( i ) == nil then m . l0 . ( i ) <- Array . make l1_size snil ; let j = u lsr l1_shift land l1_mask in if m . l0 . ...
let size m = match m . l0 with let size = ref ( 3 + 1 + Array . length l0 ) in for i = 0 to Array . length l0 - 1 do match l0 . ( i ) with | [ ] || -> ( ) | l1 -> size := ! size + 1 + Array . length l1 ; for j = 0 to Array . length l1 - 1 do size := ! size + 1 ...
let iter_blobs i m = Array . ( iter ( iter i ) ) m . l0
let dump_pp pp_v ppf m = let open Uunf_fmt in record [ " default " , int ; " l0 " , pp_v |> array_N |> array ] ppf m . default m . l0
let dump = dump_pp pp_v
let invalid_encode ( ) = invalid_arg " expected ` Await encode "
let invalid_bounds j l = invalid_arg ( Printf . sprintf " invalid bounds ( index % d , length % d ) " j l )
let unsafe_byte s j = Char . code ( Bytes . unsafe_get s j )
let unsafe_set_byte s j byte = Bytes . unsafe_set s j ( Char . unsafe_chr byte )
let u_bom = Uchar . unsafe_of_int 0xFEFF
let u_rep = Uchar . unsafe_of_int 0xFFFD
type encoding = [ ` UTF_8 | ` UTF_16 | ` UTF_16BE | ` UTF_16LE ]
type decoder_encoding = [ encoding | ` US_ASCII | ` ISO_8859_1 ]
let encoding_of_string s = match String . uppercase_ascii s with Some ` US_ASCII Some ` ISO_8859_1
let malformed s j l = ` Malformed ( Bytes . sub_string s j l )
let malformed_pair be hi s j l = let bs1 = Bytes . ( sub s j l ) in let bs0 = Bytes . create 2 in let j0 , j1 = if be then ( 0 , 1 ) else ( 1 , 0 ) in unsafe_set_byte bs0 j0 ( hi lsr 8 ) ; unsafe_set_byte bs0 j1 ( hi land 0xFF ) ; ` Malformed Bytes . ( unsafe_to_string...
let r_us_ascii s j = let b0 = unsafe_byte s j in if b0 <= 127 then ` Uchar ( Uchar . unsafe_of_int b0 ) else malformed s j 1
let r_iso_8859_1 s j = ` Uchar ( Uchar . unsafe_of_int @@ unsafe_byte s j )
let utf_8_len = [ | 1 ; 1 ; 1 ; 1 ; 1 ; 1 ; 1 ; 1 ; 1 ; 1 ; 1 ; 1 ; 1 ; 1 ; 1 ; 1 ; 1 ; 1 ; 1 ; 1 ; 1 ; 1 ; 1 ; 1 ; 1 ; 1 ; 1 ; 1 ; 1 ; 1 ; 1 ; 1 ; 1 ; 1 ; 1 ; 1 ; 1 ; 1 ; 1 ; 1 ; 1 ; 1 ; 1 ...
let r_utf_8 s j l = let uchar c = ` Uchar ( Uchar . unsafe_of_int c ) in match l with | 1 -> uchar ( unsafe_byte s j ) | 2 -> let b0 = unsafe_byte s j in let b1 = unsafe_byte s ( j + 1 ) in if b1 lsr 6 != 0b10 then malformed s j l else uchar ( ( ( b0 land 0x1F ) lsl 6 ) lor ( ...
let r_utf_16 s j0 j1 = let b0 = unsafe_byte s j0 in let b1 = unsafe_byte s j1 in let u = ( b0 lsl 8 ) lor b1 in if u < 0xD800 || u > 0xDFFF then ` Uchar ( Uchar . unsafe_of_int u ) else if u > 0xDBFF then malformed s ( min j0 j1 ) 2 else ` Hi u
let r_utf_16_lo hi s j0 j1 = let b0 = unsafe_byte s j0 in let b1 = unsafe_byte s j1 in let lo = ( b0 lsl 8 ) lor b1 in if lo < 0xDC00 || lo > 0xDFFF then malformed_pair ( j0 < j1 ) hi s ( min j0 j1 ) 2 else ` Uchar ( Uchar . unsafe_of_int ( ( ( ( hi land 0x3FF ) lsl 10 ) lor ...
let r_encoding s j l = let some i = if i < l then Some ( unsafe_byte s ( j + i ) ) else None in match ( some 0 ) , ( some 1 ) , ( some 2 ) with | Some 0xEF , Some 0xBB , Some 0xBF -> ` UTF_8 ` BOM | Some 0xFE , Some 0xFF , _ -> ` UTF_16BE ` BOM | Some 0xFF , ...
type src = [ ` Channel of in_channel | ` String of string | ` Manual ]
type nln = [ ` ASCII of Uchar . t | ` NLF of Uchar . t | ` Readline of Uchar . t ]
type decode = [ ` Await | ` End | ` Malformed of string | ` Uchar of Uchar . t ]
let pp_decode ppf = function let l = String . length bs in pp ppf " [ ` @ Malformed ( " ; if l > 0 then pp ppf " % 02X " ( Char . code ( bs . [ 0 ] ) ) ; for i = 1 to l - 1 do pp ppf " % 02X " ( Char . code ( bs . [ i ] ) ) done ; pp ppf " ) ] ...
type decoder = { src : src ; mutable encoding : decoder_encoding ; nln : nln option ; nl : Uchar . t ; mutable i : Bytes . t ; mutable i_pos : int ; mutable i_max : int ; t : Bytes . t ; mutable t_len : int ; mutable t_need : int ; mutable removed_bom : bool ; mutable last_cr ...
let i_rem d = d . i_max - d . i_pos + 1
let eoi d = d . i <- Bytes . empty ; d . i_pos <- 0 ; d . i_max <- min_int
let src d s j l = if ( j < 0 || l < 0 || j + l > Bytes . length s ) then invalid_bounds j l else if ( l = 0 ) then eoi d else ( d . i <- s ; d . i_pos <- j ; d . i_max <- j + l - 1 )
let refill k d = match d . src with let rc = input ic d . i 0 ( Bytes . length d . i ) in ( src d d . i 0 rc ; k d )
let t_need d need = d . t_len <- 0 ; d . t_need <- need
let rec t_fill k d = let blit d l = unsafe_blit d . i d . i_pos d . t d . t_len l ; d . i_pos <- d . i_pos + l ; d . t_len <- d . t_len + l ; in let rem = i_rem d in if rem < 0 then k d else let need = d . t_need - d . t_len in if rem < need then ( blit d rem ; refill ( t_fil...
let ret k v byte_count d = d . k <- k ; d . byte_count <- d . byte_count + byte_count ; d . pp d v
let rec decode_us_ascii d = let rem = i_rem d in if rem <= 0 then ( if rem < 0 then ` End else refill decode_us_ascii d ) else let j = d . i_pos in d . i_pos <- d . i_pos + 1 ; ret decode_us_ascii ( r_us_ascii d . i j ) 1 d
let rec decode_iso_8859_1 d = let rem = i_rem d in if rem <= 0 then ( if rem < 0 then ` End else refill decode_iso_8859_1 d ) else let j = d . i_pos in d . i_pos <- d . i_pos + 1 ; ret decode_iso_8859_1 ( r_iso_8859_1 d . i j ) 1 d
let rec t_decode_utf_8 d = if d . t_len < d . t_need then ret decode_utf_8 ( malformed d . t 0 d . t_len ) d . t_len d else ret decode_utf_8 ( r_utf_8 d . t 0 d . t_len ) d . t_len d let rem = i_rem d in if rem <= 0 then ( if rem < 0 then ` End else refill decode_utf_8 d ) else le...
let rec t_decode_utf_16be_lo hi d = let bcount = d . t_len + 2 in if d . t_len < d . t_need then ret decode_utf_16be ( malformed_pair true hi d . t 0 d . t_len ) bcount d else ret decode_utf_16be ( r_utf_16_lo hi d . t 0 1 ) bcount d if d . t_len < d . t_need then ret decode_utf_16be ( ...
let rec t_decode_utf_16le_lo hi d = let bcount = d . t_len + 2 in if d . t_len < d . t_need then ret decode_utf_16le ( malformed_pair false hi d . t 0 d . t_len ) bcount d else ret decode_utf_16le ( r_utf_16_lo hi d . t 1 0 ) bcount d if d . t_len < d . t_need then ret decode_utf_16le (...
let guessed_utf_8 d = let b3 d = let b3 = unsafe_byte d . t 2 in match utf_8_len . ( b3 ) with | 0 -> ret decode_utf_8 ( malformed d . t 2 1 ) 1 d | n -> d . t_need <- n ; d . t_len <- 1 ; unsafe_set_byte d . t 0 b3 ; t_fill t_decode_utf_8 d in let b2 d = let b2 = unsafe_byte...
let guessed_utf_16 d be v = let decode_utf_16 , t_decode_utf_16 , t_decode_utf_16_lo , j0 , j1 = if be then decode_utf_16be , t_decode_utf_16be , t_decode_utf_16be_lo , 0 , 1 else decode_utf_16le , t_decode_utf_16le , t_decode_utf_16le_lo , 1 , 0 in let b3 k d = if d . t_len < 3 then ...
let guess_encoding d = let setup d = match r_encoding d . t 0 d . t_len with | ` UTF_8 r -> d . encoding <- ` UTF_8 ; d . k <- decode_utf_8 ; begin match r with | ` BOM -> ret decode_utf_8 ( ` Uchar u_bom ) 3 d | ` Decode -> guessed_utf_8 d | ` End -> ` End end | ` UTF_16B...
let nline d = d . col <- 0 ; d . line <- d . line + 1
let ncol d = d . col <- d . col + 1
let ncount d = d . count <- d . count + 1
let cr d b = d . last_cr <- b
let pp_remove_bom utf16 pp d = function match Uchar . to_int u with | 0xFEFF -> if utf16 then ( d . encoding <- ` UTF_16BE ; d . k <- decode_utf_16be ) ; d . removed_bom <- true ; d . pp <- pp ; d . k d | 0xFFFE when utf16 -> d . encoding <- ` UTF_16LE ; d . k <- decode_ut...
let pp_nln_none d = function match Uchar . to_int u with | 0x000A -> let last_cr = d . last_cr in cr d false ; ncount d ; if last_cr then v else ( nline d ; v ) | 0x000D -> cr d true ; ncount d ; nline d ; v | ( 0x0085 | 0x000C | 0x2028 | 0x2029 ) -> cr d false ; ncount d ; ...
let pp_nln_readline d = function match Uchar . to_int u with | 0x000A -> let last_cr = d . last_cr in cr d false ; if last_cr then d . k d else ( ncount d ; nline d ; ` Uchar d . nl ) | 0x000D -> cr d true ; ncount d ; nline d ; ` Uchar d . nl | ( 0x0085 | 0x000C | 0x2028 ...
let pp_nln_nlf d = function match Uchar . to_int u with | 0x000A -> let last_cr = d . last_cr in cr d false ; if last_cr then d . k d else ( ncount d ; nline d ; ` Uchar d . nl ) | 0x000D -> cr d true ; ncount d ; nline d ; ` Uchar d . nl | 0x0085 -> cr d false ; ncount d ; ...
let pp_nln_ascii d = function match Uchar . to_int u with | 0x000A -> let last_cr = d . last_cr in cr d false ; if last_cr then d . k d else ( ncount d ; nline d ; ` Uchar d . nl ) | 0x000D -> cr d true ; ncount d ; nline d ; ` Uchar d . nl | ( 0x0085 | 0x000C | 0x2028 | 0...
let decoder ? nln ? encoding src = let pp , nl = match nln with | None -> pp_nln_none , Uchar . unsafe_of_int 0x000A | Some ( ` ASCII nl ) -> pp_nln_ascii , nl | Some ( ` NLF nl ) -> pp_nln_nlf , nl | Some ( ` Readline nl ) -> pp_nln_readline , nl in let encoding , k = matc...
let decode d = d . k d
let decoder_line d = d . line
let decoder_col d = d . col