text
stringlengths 0
601k
|
---|
module Make ( Text : UnicodeString . Type ) Type = struct let test ( ? char_gen = ( fun _ -> uchar_of_int ( Random . int 0x8000000 ) 0x8000000 ) 0x8000000 ) 0x8000000 ~ desc ~ log ( ) = random_test ~ desc ~ log ~ data ( : fun size -> Array . init size char_gen ) char_gen ~ body ( : fun a -> expect_pass ~ body ( : fun ( ) -> let s = Text . init ( Array . length a ) a ( Array . get a ) a in let len = Text . length s in expect_equal ~ msg ( : lazy " init ) " len ( Array . length a ) a ; for _ = 0 to 50 do let i = Random . int len in expect_equal ~ msg ( : lazy ( sprintf " set / get : location % d " i ) i ) i ( Text . get s i ) i a ( . i ) i done ; let r = ref 0 in Text . iter ( fun u -> expect_equal ~ msg ( : lazy ( sprintf " iter : location % d " ! r ) r ) r u a ( . ! r ) r ; incr r ) r s ; expect_equal ~ msg ( : lazy ( sprintf " iter : count % d " ! r ) r ) r ! r len ; let cur = ref ( Text . nth s 0 ) 0 in let r = ref 0 in while not ( Text . out_of_range s ! cur ) cur do expect_equal ~ msg ( : lazy ( sprintf " index look : location % d " ! r ) r ) r ( Text . look s ! cur ) cur a ( . ! r ) r ; cur := Text . next s ! cur ; incr r done ; expect_equal ~ msg ( : lazy ( sprintf " index : count % d " ! r ) r ) r ! r len ; for _ = 0 to 100 do let pos = Random . int len in let cur = Text . nth s pos in expect_equal ~ msg ( : lazy ( sprintf " nth : location % d " pos ) pos ) pos ( Text . look s cur ) cur a ( . pos ) pos ; if pos = 0 then ( ) else let cur ' = Text . prev s cur in expect_equal_app ~ msg ( : lazy ( sprintf " cursor prev : location % d " pos ) pos ) pos ( fun ( ) -> Text . look s cur ' ) cur ' ( ) ( fun i -> a ( . i ) i ) i ( pos - 1 ) 1 ; if pos = len - 1 then ( ) else let cur ' = Text . next s cur in expect_equal_app ~ msg ( : lazy ( sprintf " cursor next : location % d " pos ) pos ) pos ( fun ( ) -> Text . look s cur ' ) cur ' ( ) ( fun i -> a ( . i ) i ) i ( pos + 1 ) 1 ; done ; let b = Text . Buf . create 0 in let p = Random . int len in let s1 = Text . init p ( Array . get a ) a in let s2 = Text . init ( len - p ) p ( fun x -> Array . get a ( p + x ) x ) x in Text . Buf . add_string b s1 ; Text . Buf . add_string b s2 ; let s ' = Text . Buf . contents b in expect_true ~ msg ( : lazy " step 1 ) " ( Text . compare s s ' = 0 ) 0 ; Text . Buf . clear b ; Text . iter ( Text . Buf . add_char b ) b s ; let s ' = Text . Buf . contents b in expect_true ~ msg ( : lazy " step 2 ) " ( Text . compare s s ' = 0 ) 0 ; Text . Buf . clear b ; let b ' = Text . Buf . create 16 in let pos = Random . int len in for i = 0 to len - 1 do if i < pos then Text . Buf . add_char b a ( . i ) i else Text . Buf . add_char b ' a ( . i ) i done ; Text . Buf . add_buffer b b ' ; let s ' = Text . Buf . contents b in expect_true ~ msg ( : lazy " step 3 ) " ( Text . compare s s ' = 0 ) 0 ; Text . Buf . reset b ; Text . Buf . add_string b s ; let s ' = Text . Buf . contents b in expect_true ~ msg ( : lazy " step 4 ) " ( Text . compare s s ' = 0 ) 0 ) ) ; end |
let location Location . { loc_start = b ; loc_end = e ; _ } = Loc . extract ( b , e ) |
let mk_id ( ? id_ats = [ ] ) ( ? id_loc = dummy_loc ) id_str = { id_str ; id_ats ; id_loc } |
let mk_term ( ? term_loc = dummy_loc ) term_desc = { term_desc ; term_loc } |
let mk_pattern ( ? pat_loc = dummy_loc ) pat_desc = { pat_desc ; pat_loc } |
let ident_of_tvsymbol Ty . { tv_name = name } = mk_id name . id_str ~ id_loc ( : location name . id_loc ) |
let ident_of_lsymbol Symbols . { ls_name = name ; _ } = mk_id name . id_str ~ id_loc ( : location name . id_loc ) |
let constant = function | P . Pconst_integer ( s , _ ) -> if s . [ 0 ] = ' ' - then let s = String . sub s 1 ( String . length s - 1 ) in let n = Number . int_literal ILitDec ~ neg : false s in Constant . ( ConstInt ( Number . neg_int n ) ) else let n = Number . int_literal ILitDec ~ neg : false s in Constant . ConstInt n | P . Pconst_string ( s , _ , _ ) -> Constant . ConstStr s | Pconst_float _ -> assert false | _ -> assert false |
let preid Preid . { pid_str ; pid_loc ; _ } = let pid_str = match query_syntax pid_str with Some s -> s | _ -> pid_str in mk_id ~ id_loc ( : location pid_loc ) pid_str |
let rec qualid = function | Uast . Qpreid id -> Qident ( preid id ) | Uast . Qdot ( q , id ) -> Qdot ( qualid q , preid id ) |
let rec pty = function | Uast . PTtyvar id -> PTtyvar ( preid id ) | Uast . PTtuple pty_list -> PTtuple ( List . map pty pty_list ) | Uast . PTarrow ( _ , pty1 , pty2 ) -> PTarrow ( pty pty1 , pty pty2 ) | Uast . PTtyapp ( q , pty_list ) -> PTtyapp ( qualid q , List . map pty pty_list ) |
let rec ty Ty . { ty_node } = match ty_node with | Ty . Tyvar { tv_name } -> PTtyvar ( mk_id tv_name . id_str ~ id_loc ( : location tv_name . id_loc ) ) | Ty . Tyapp ( ts , tyl ) when Ty . is_ts_tuple ts -> PTtuple ( List . map ty tyl ) | Ty . Tyapp ( ts , tyl ) when Ty . is_ts_arrow ts -> let rec arrow_of_pty_list = function | [ ] -> assert false | [ pty ] -> pty | arg :: ptyl -> PTarrow ( arg , arrow_of_pty_list ptyl ) in arrow_of_pty_list ( List . map ty tyl ) | Ty . Tyapp ( { ts_ident ; _ } , tyl ) -> let id_loc = location ts_ident . id_loc in let id_str = match query_syntax ts_ident . id_str with | None -> ts_ident . id_str | Some s -> s in let qualid = mk_id id_str ~ id_loc in PTtyapp ( Qident qualid , List . map ty tyl ) |
let quant = function | Uast . Tforall -> D . DTforall | Uast . Texists -> D . DTexists | Uast . Tlambda -> D . DTlambda |
let rec pattern Uast . { pat_desc = p_desc ; pat_loc } = let pat_loc = location pat_loc in let mk_pat p = mk_pattern ~ pat_loc p in let qualid_pattern ( q , pat ) = ( qualid q , pattern pat ) in let pat_desc = function | Uast . Pwild -> Pwild | Uast . Pvar id -> Pvar ( preid id ) | Uast . Papp ( q , pat_list ) -> Papp ( qualid q , List . map pattern pat_list ) | Uast . Prec q_pat_list -> Prec ( List . map qualid_pattern q_pat_list ) | Uast . Ptuple pat_list -> Ptuple ( List . map pattern pat_list ) | Uast . Pas ( pat , id ) -> Pas ( pattern pat , preid id , false ) | Uast . Por ( pat1 , pat2 ) -> Por ( pattern pat1 , pattern pat2 ) | Uast . Pcast ( pat , ty ) -> Pcast ( pattern pat , pty ty ) in mk_pat ( pat_desc p_desc ) |
let binder ( id , ty ) = ( location id . Preid . pid_loc , Some ( preid id ) , false , Opt . map pty ty ) |
let rec term in_post Uast . { term_desc = t_desc ; term_loc } = let term_loc = location term_loc in let mk_term t = mk_term ~ term_loc t in let binop = function | Uast . Tand -> D . DTand | Uast . Tand_asym -> D . DTand_asym | Uast . Tor -> D . DTor | Uast . Tor_asym -> D . DTor_asym | Uast . Timplies -> D . DTimplies | Uast . Tiff -> D . DTiff in let attr a = ATstr ( Why3 . Ident . create_attribute a ) in let pat_term ( pat , t ) = ( pattern pat , term in_post t ) in let qualid_term ( q , t ) = ( qualid q , term in_post t ) in let term_desc = function | Uast . Ttrue -> Ttrue | Uast . Tfalse -> Tfalse | Uast . Tconst c -> Tconst ( constant c ) | Uast . Tpreid id -> Tident ( qualid id ) | Uast . Tidapp ( q , tl ) -> Tidapp ( qualid q , List . map ( term in_post ) tl ) | Uast . Tfield ( t , q ) -> Tidapp ( qualid q , [ term in_post t ] ) | Uast . Tapply ( t1 , t2 ) -> Tapply ( term in_post t1 , term in_post t2 ) | Uast . Tnot t -> Tnot ( term in_post t ) | Uast . Tattr ( a , t ) -> Tattr ( attr a , term in_post t ) | Uast . Tcast ( t , ty ) -> Tcast ( term in_post t , pty ty ) | Uast . Ttuple t_list -> Ttuple ( List . map ( term in_post ) t_list ) | Uast . Trecord q_t_list -> Trecord ( List . map qualid_term q_t_list ) | Uast . Tscope ( q , t ) -> Tscope ( qualid q , term in_post t ) | Uast . Tcase ( t , pt_list ) -> Tcase ( term in_post t , List . map pat_term pt_list ) | Uast . Tlet ( id , t1 , t2 ) -> Tlet ( preid id , term in_post t1 , term in_post t2 ) | Uast . Tinfix ( t1 , id , t2 ) -> Tinfix ( term in_post t1 , preid id , term in_post t2 ) | Uast . Tbinop ( t1 , op , t2 ) -> Tbinop ( term in_post t1 , binop op , term in_post t2 ) | Uast . Told t -> let lbl = if in_post then Dexpr . old_label else " ' Old " in Tat ( term in_post t , mk_id ~ id_loc : term_loc lbl ) | Uast . Tif ( t1 , t2 , t3 ) -> Tif ( term in_post t1 , term in_post t2 , term in_post t3 ) | Uast . Tupdate ( t , q_t_list ) -> Tupdate ( term in_post t , List . map qualid_term q_t_list ) | Uast . Tquant ( q , bl , t ) -> Tquant ( quant q , List . map binder bl , [ ] , term in_post t ) in mk_term ( term_desc t_desc ) |
type mutability = [ ` Mutable | ` Immutable ] |
type ' a text = UChar . t array |
type utext = [ ` Imutable ] Imutable text |
type ustring = [ ` Mutable ] Mutable text |
let look s i = get s i |
let nth _ i = i |
let first _ = 0 |
let last s = Array . length s - 1 |
let out_of_range s i = i < 0 || i >= Array . length s |
let next _ i = i + 1 |
let prev _ i = i - 1 |
let move _ i n = i + n |
let compare_index _ ( i : int ) int ( j : int ) int = i - j |
let make len init = Array . make len init |
let of_string s = init ( String . length s ) s ( fun i -> UChar . of_char s [ . i ] i ) i |
let rec compare_aux i t1 t2 = if i >= length t1 then if i >= length t2 then 0 else ~- 1 else if i >= length t2 then 1 else match UChar . compare ( get t1 i ) i ( get t2 i ) i with 0 -> compare_aux ( i + 1 ) 1 t1 t2 | sgn -> sgn |
let compare t1 t2 = compare_aux 0 t1 t2 |
module Buf = struct include XArray type buf = UChar . t xarray let create bufsize = XArray . make ~ bufsize 0 ( UChar . chr_of_uint 0 ) 0 let contents = array_of let contents_string = array_of let add_char = add_element let add_string = add_array let add_buffer = add_xarray end |
type t = ( int , int16_unsigned_elt , c_layout ) c_layout Array1 . t |
let rec validate_aux ( a : t ) t i = if i >= Array1 . dim a then ( ) else let n = a { . i } i in if n < 0xd800 || n >= 0xe000 && n < 0xfffe then validate_aux a ( i + 1 ) 1 else if n >= 0xd800 && n < 0xdc00 then if i + 1 >= Array1 . dim a then raise Malformed_code else let n ' = a { . i + 1 } 1 in if n ' < 0xdc00 || n ' >= 0xe000 then raise Malformed_code else validate_aux a ( i + 2 ) 2 else raise Malformed_code |
let validate ( a : t ) t = validate_aux a 0 |
let look ( a : t ) t i : UChar . t = let n0 = a { . i } i in if n0 < 0xd800 || n0 >= 0xe000 then UChar . chr_of_uint n0 else if n0 < 0xdc00 then let n1 = a { . i + 1 } 1 in UChar . chr_of_uint ( ( ( n0 - 0xd800 ) 0xd800 lsl 10 ) 10 + ( n1 - 0xdc00 ) 0xdc00 + 0x10000 ) 0x10000 else invalid_arg " UTF16 . look " |
let rec length_aux ( a : t ) t c i = if i >= Array1 . dim a then c else let n = a { . i } i in if n < 0xd800 || n >= 0xe000 then length_aux a ( c + 1 ) 1 ( i + 1 ) 1 else length_aux a ( c + 1 ) 1 ( i + 2 ) 2 |
let length ( a : t ) t = length_aux a 0 0 |
let next ( a : t ) t i = let n = a { . i } i in if n < 0xd800 || n >= 0xdc00 then i + 1 else i + 2 |
let prev ( a : t ) t i = let i ' = i - 1 in let n = a { . i ' } i ' in if n < 0xdc00 || n >= 0xe000 then i ' else i ' - 1 |
let rec move_forward ( a : t ) t i c = if c > 0 then move_forward a ( next a i ) i ( c - 1 ) 1 else i |
let rec move_backward ( a : t ) t i c = if c < 0 then move_backward a ( prev a i ) i ( c + 1 ) 1 else i |
let move ( a : t ) t i c = if c > 0 then move_forward a i c else if c < 0 then move_backward a i c else i |
let first _ = 0 |
let last ( a : t ) t = prev a ( Array1 . dim a ) a |
let out_of_range ( a : t ) t i = i < 0 || i >= Array1 . dim a |
let compare_index _ i j = i - j |
let nth ( a : t ) t c = move_forward a 0 c |
let get ( a : t ) t c = look a ( nth a c ) c |
let rec iter_aux proc ( a : t ) t i = if i >= Array1 . dim a then ( ) else begin proc ( look a i ) i ; iter_aux proc a ( next a i ) i end |
let iter proc ( a : t ) t = iter_aux proc a 0 |
module Buf = struct let set ( a : t ) t i u = let n = UChar . uint_code u in if n < 0 then raise Out_of_range else if n < 0xd800 || n >= 0xe000 && n <= 0xfffd then begin a { . i } i <- n ; 1 end else if n >= 0x10000 && n <= 0x10ffff then begin a { . i } i <- ( ( n - 0x10000 ) 0x10000 lsr 10 ) 10 + 0xd800 ; a { . i + 1 } 1 <- ( ( n - 0x10000 ) 0x10000 land 0x3ff ) 0x3ff + 0xdc00 ; 2 end else raise Out_of_range type buf = { init_size : int ; mutable pos : index ; mutable contents : t } t let create n = let contents = Array1 . create int16_unsigned c_layout n in { init_size = n ; pos = 0 ; contents = contents } contents let clear buf = buf . pos <- 0 let reset buf = buf . contents <- Array1 . create int16_unsigned c_layout buf . init_size ; buf . pos <- 0 let contents buf = let a = Array1 . create int16_unsigned c_layout buf . pos in let src = Array1 . sub buf . contents 0 buf . pos in Array1 . blit src a ; a let resize buf n = if Array1 . dim buf . contents >= n then ( ) else let a = Array1 . create int16_unsigned c_layout ( 2 * n ) n in let a ' = Array1 . sub a 0 ( Array1 . dim buf . contents ) contents in Array1 . blit buf . contents a ' ; buf . contents <- a let add_char buf u = resize buf ( buf . pos + 2 ) 2 ; buf . pos <- buf . pos + set buf . contents buf . pos u let add_string buf ( a : t ) t = let len = buf . pos + Array1 . dim a in resize buf len ; let b = Array1 . sub buf . contents buf . pos ( Array1 . dim a ) a in Array1 . blit a b ; buf . pos <- len let add_buffer buf1 buf2 = let len = buf1 . pos + buf2 . pos in resize buf1 len ; let a = Array1 . sub buf2 . contents 0 buf2 . pos in let b = Array1 . sub buf1 . contents buf1 . pos buf2 . pos in Array1 . blit a b ; buf1 . pos <- len end |
let init len f = let buf = Buf . create ( len + 1 ) 1 in for i = 0 to len - 1 do Buf . add_char buf ( f i ) i done ; Buf . contents buf |
let rec compare_aux ( a : t ) t b i = if i >= Array1 . dim a then 0 else let n1 = a { . i } i in let n2 = b { . i } i in if n1 = n2 then compare_aux a b ( i + 1 ) 1 else ( if n1 < 0xd800 || n1 >= 0xdc00 then n1 else 0x10000 lor n1 ) n1 - ( if n2 < 0xd800 || n2 >= 0xdc00 then n2 else 0x10000 lor n2 ) n2 |
let compare ( a : t ) t b = let sgn = Array1 . dim a - Array1 . dim b in if sgn = 0 then compare_aux a b 0 else sgn |
module UTF16Conv = CharEncoding . Make ( UTF16 ) UTF16 |
module UTF16Test = UStorageTest . Make ( UTF16 ) UTF16 |
let char_gen _ = let n = match Random . int 3 with 0 -> Random . int 0xd800 | 1 -> 0xe000 + Random . int ( 0xfffe - 0xe000 ) 0xe000 | 2 -> 0x10000 + Random . int ( 0x110000 - 0x10000 ) 0x10000 | _ -> assert false in uchar_of_int n |
let _ = UTF16Test . test ~ desc " : UTF16 test " ~ log " : base_utf16 " ~ char_gen ( ) |
let string_of_int16array a = let s = Bytes . create ( 2 + 2 * Bigarray . Array1 . dim a ) a in Bytes . set s 0 ( Char . chr 0xfe ) 0xfe ; Bytes . set s 1 ( Char . chr 0xff ) 0xff ; for i = 0 to Bigarray . Array1 . dim a - 1 do Bytes . set s ( 2 * i + 2 ) 2 ( Char . chr ( a { . i } i lsr 8 ) 8 ) 8 ; Bytes . set s ( 2 * i + 3 ) 3 ( Char . chr ( a { . i } i land 255 ) 255 ) 255 done ; Bytes . to_string s |
let _ = random_test ~ desc " : UTF16 validate " ~ log " : base_utf16_validate " ~ data ( : fun size -> Array . init size char_gen ) char_gen ~ body ( : fun a -> expect_pass ~ body ( : fun ( ) -> let s = UTF16 . init ( Array . length a ) a ( fun i -> a ( . i ) i ) i in let s ' = UTF16Conv . encode CharEncoding . utf16be s in expect_true ( s ' = string_of_int16array s ) s ; expect_equal_app UTF16 . validate s ( fun ( ) -> ( ) ) ( ) ) ) |
let _ = test ~ desc " : Malformed utf16 1 " ~ body ( : fun ( ) -> expect_pass ~ body ( : fun ( ) -> let a = Array1 . create int16_unsigned c_layout 2 in a { . 0 } 0 <- 0xdc01 ; a { . 1 } 1 <- 0xd801 ; expect_equal_app UTF16 . validate a ( fun _ -> raise UTF16 . Malformed_code ) Malformed_code ( ) ) ) |
let _ = test ~ desc " : Malformed utf16 2 " ~ body ( : fun ( ) -> expect_pass ~ body ( : fun ( ) -> let a = Array1 . create int16_unsigned c_layout 2 in a { . 0 } 0 <- 0xd801 ; a { . 1 } 1 <- 0x0300 ; expect_equal_app UTF16 . validate a ( fun _ -> raise UTF16 . Malformed_code ) Malformed_code ( ) ) ) |
let _ = test ~ desc " : Malformed utf16 3 " ~ body ( : fun ( ) -> expect_pass ~ body ( : fun ( ) -> let a = Array1 . create int16_unsigned c_layout 2 in a { . 0 } 0 <- 0xfffe ; a { . 1 } 1 <- 0xfeff ; expect_equal_app UTF16 . validate a ( fun _ -> raise UTF16 . Malformed_code ) Malformed_code ( ) ) ) |
let _ = test ~ desc " : Out_of_range utf16 1 " ~ body ( : fun ( ) -> expect_pass ~ body ( : fun ( ) -> expect_equal_app ( UTF16 . init 1 ) 1 ( fun _ -> uchar_of_int 0xd801 ) 0xd801 ( fun _ -> raise UTF16 . Out_of_range ) Out_of_range ( ) ) ) |
let _ = test ~ desc " : Out_of_range utf16 2 " ~ body ( : fun ( ) -> expect_pass ~ body ( : fun ( ) -> expect_equal_app ( UTF16 . init 1 ) 1 ( fun _ -> uchar_of_int 0xdfff ) 0xdfff ( fun _ -> raise UTF16 . Out_of_range ) Out_of_range ( ) ) ) |
let _ = test ~ desc " : Out_of_range utf16 3 " ~ body ( : fun ( ) -> expect_pass ~ body ( : fun ( ) -> expect_equal_app ( UTF16 . init 1 ) 1 ( fun _ -> uchar_of_int 0xfffe ) 0xfffe ( fun _ -> raise UTF16 . Out_of_range ) Out_of_range ( ) ) ) |
let _ = test ~ desc " : Out_of_range utf16 1 " ~ body ( : fun ( ) -> expect_pass ~ body ( : fun ( ) -> expect_equal_app ( UTF16 . init 1 ) 1 ( fun _ -> uchar_of_int 0x4fffffff ) 0x4fffffff ( fun _ -> raise UTF16 . Out_of_range ) Out_of_range ( ) ) ) |
let look s i = let n ' = let n = Char . code s [ . i ] i in if n < 0x80 then n else if n <= 0xdf then ( n - 0xc0 ) 0xc0 lsl 6 lor ( 0x7f land ( Char . code s [ . i + 1 ] 1 ) 1 ) 1 else if n <= 0xef then let n ' = n - 0xe0 in let m0 = Char . code s [ . i + 2 ] 2 in let m = Char . code ( String . unsafe_get s ( i + 1 ) 1 ) 1 in let n ' = n ' lsl 6 lor ( 0x7f land m ) m in n ' lsl 6 lor ( 0x7f land m0 ) m0 else if n <= 0xf7 then let n ' = n - 0xf0 in let m0 = Char . code s [ . i + 3 ] 3 in let m = Char . code ( String . unsafe_get s ( i + 1 ) 1 ) 1 in let n ' = n ' lsl 6 lor ( 0x7f land m ) m in let m = Char . code ( String . unsafe_get s ( i + 2 ) 2 ) 2 in let n ' = n ' lsl 6 lor ( 0x7f land m ) m in n ' lsl 6 lor ( 0x7f land m0 ) m0 else if n <= 0xfb then let n ' = n - 0xf8 in let m0 = Char . code s [ . i + 4 ] 4 in let m = Char . code ( String . unsafe_get s ( i + 1 ) 1 ) 1 in let n ' = n ' lsl 6 lor ( 0x7f land m ) m in let m = Char . code ( String . unsafe_get s ( i + 2 ) 2 ) 2 in let n ' = n ' lsl 6 lor ( 0x7f land m ) m in let m = Char . code ( String . unsafe_get s ( i + 3 ) 3 ) 3 in let n ' = n ' lsl 6 lor ( 0x7f land m ) m in n ' lsl 6 lor ( 0x7f land m0 ) m0 else if n <= 0xfd then let n ' = n - 0xfc in let m0 = Char . code s [ . i + 5 ] 5 in let m = Char . code ( String . unsafe_get s ( i + 1 ) 1 ) 1 in let n ' = n ' lsl 6 lor ( 0x7f land m ) m in let m = Char . code ( String . unsafe_get s ( i + 2 ) 2 ) 2 in let n ' = n ' lsl 6 lor ( 0x7f land m ) m in let m = Char . code ( String . unsafe_get s ( i + 3 ) 3 ) 3 in let n ' = n ' lsl 6 lor ( 0x7f land m ) m in let m = Char . code ( String . unsafe_get s ( i + 4 ) 4 ) 4 in let n ' = n ' lsl 6 lor ( 0x7f land m ) m in n ' lsl 6 lor ( 0x7f land m0 ) m0 else invalid_arg " UTF8 . look " in Obj . magic n ' |
let rec search_head s i = if i >= String . length s then i else let n = Char . code ( String . unsafe_get s i ) i in if n < 0x80 || n >= 0xc2 then i else search_head s ( i + 1 ) 1 |
let next s i = let n = Char . code s [ . i ] i in if n < 0x80 then i + 1 else if n < 0xc0 then search_head s ( i + 1 ) 1 else if n <= 0xdf then i + 2 else if n <= 0xef then i + 3 else if n <= 0xf7 then i + 4 else if n <= 0xfb then i + 5 else if n <= 0xfd then i + 6 else invalid_arg " UTF8 . next " |
let rec search_head_backward s i = if i < 0 then - 1 else let n = Char . code s [ . i ] i in if n < 0x80 || n >= 0xc2 then i else search_head_backward s ( i - 1 ) 1 |
let prev s i = search_head_backward s ( i - 1 ) 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 ) i ( n - 1 ) 1 in loop i n else let rec loop i n = if n >= 0 then i else loop ( prev s i ) i ( n + 1 ) 1 in loop i n |
let rec nth_aux s i n = if n = 0 then i else nth_aux s ( next s i ) i ( n - 1 ) 1 |
let nth s n = nth_aux s 0 n |
let last s = search_head_backward s ( String . length s - 1 ) 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 ) n |
let add_uchar buf u = let masq = 0b111111 in let k = int_of_uchar u in if k < 0 || k >= 0x4000000 then begin Buffer . add_char buf ( Char . chr ( 0xfc + ( k lsr 30 ) 30 ) 30 ) 30 ; Buffer . add_char buf ( Char . unsafe_chr ( 0x80 lor ( ( k lsr 24 ) 24 land masq ) masq ) masq ) masq ; Buffer . add_char buf ( Char . unsafe_chr ( 0x80 lor ( ( k lsr 18 ) 18 land masq ) masq ) masq ) masq ; Buffer . add_char buf ( Char . unsafe_chr ( 0x80 lor ( ( k lsr 12 ) 12 land masq ) masq ) masq ) masq ; Buffer . add_char buf ( Char . unsafe_chr ( 0x80 lor ( ( k lsr 6 ) 6 land masq ) masq ) masq ) masq ; Buffer . add_char buf ( Char . unsafe_chr ( 0x80 lor ( k land masq ) masq ) masq ) masq ; end else if k <= 0x7f then Buffer . add_char buf ( Char . unsafe_chr k ) k else if k <= 0x7ff then begin Buffer . add_char buf ( Char . unsafe_chr ( 0xc0 lor ( k lsr 6 ) 6 ) 6 ) 6 ; Buffer . add_char buf ( Char . unsafe_chr ( 0x80 lor ( k land masq ) masq ) masq ) masq end else if k <= 0xffff then begin Buffer . add_char buf ( Char . unsafe_chr ( 0xe0 lor ( k lsr 12 ) 12 ) 12 ) 12 ; Buffer . add_char buf ( Char . unsafe_chr ( 0x80 lor ( ( k lsr 6 ) 6 land masq ) masq ) masq ) masq ; Buffer . add_char buf ( Char . unsafe_chr ( 0x80 lor ( k land masq ) masq ) masq ) masq ; end else if k <= 0x1fffff then begin Buffer . add_char buf ( Char . unsafe_chr ( 0xf0 + ( k lsr 18 ) 18 ) 18 ) 18 ; Buffer . add_char buf ( Char . unsafe_chr ( 0x80 lor ( ( k lsr 12 ) 12 land masq ) masq ) masq ) masq ; Buffer . add_char buf ( Char . unsafe_chr ( 0x80 lor ( ( k lsr 6 ) 6 land masq ) masq ) masq ) masq ; Buffer . add_char buf ( Char . unsafe_chr ( 0x80 lor ( k land masq ) masq ) masq ) masq ; end else begin Buffer . add_char buf ( Char . unsafe_chr ( 0xf8 + ( k lsr 24 ) 24 ) 24 ) 24 ; Buffer . add_char buf ( Char . unsafe_chr ( 0x80 lor ( ( k lsr 18 ) 18 land masq ) masq ) masq ) masq ; Buffer . add_char buf ( Char . unsafe_chr ( 0x80 lor ( ( k lsr 12 ) 12 land masq ) masq ) masq ) masq ; Buffer . add_char buf ( Char . unsafe_chr ( 0x80 lor ( ( k lsr 6 ) 6 land masq ) masq ) masq ) masq ; Buffer . add_char buf ( Char . unsafe_chr ( 0x80 lor ( k land masq ) masq ) masq ) masq ; end |
let init len f = let buf = Buffer . create len in for c = 0 to len - 1 do add_uchar buf ( f c ) c done ; Buffer . contents buf |
let rec length_aux s c i = if i >= String . length s then c else let n = Char . code ( String . unsafe_get s i ) i in let k = if n < 0x80 then 1 else if n < 0xc0 then invalid_arg " UTF8 . length " else if n < 0xe0 then 2 else if n < 0xf0 then 3 else if n < 0xf8 then 4 else if n < 0xfc then 5 else if n < 0xfe then 6 else invalid_arg " UTF8 . length " in length_aux s ( c + 1 ) 1 ( i + k ) 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 ) i |
let iter proc s = iter_aux proc s 0 |
let compare s1 s2 = Pervasives . compare s1 s2 |
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 ) i in if n < 0x80 || n >= 0xc0 then raise Malformed_code else trail ( c - 1 ) 1 ( i + 1 ) 1 ( a lsl 6 lor ( n - 0x80 ) 0x80 ) 0x80 in let rec main i = if i >= String . length s then ( ) else let n = Char . code ( String . unsafe_get s i ) i in if n < 0x80 then main ( i + 1 ) 1 else if n < 0xc2 then raise Malformed_code else if n <= 0xdf then if trail 1 ( i + 1 ) 1 ( n - 0xc0 ) 0xc0 < 0x80 then raise Malformed_code else main ( i + 2 ) 2 else if n <= 0xef then if trail 2 ( i + 1 ) 1 ( n - 0xe0 ) 0xe0 < 0x800 then raise Malformed_code else main ( i + 3 ) 3 else if n <= 0xf7 then if trail 3 ( i + 1 ) 1 ( n - 0xf0 ) 0xf0 < 0x10000 then raise Malformed_code else main ( i + 4 ) 4 else if n <= 0xfb then if trail 4 ( i + 1 ) 1 ( n - 0xf8 ) 0xf8 < 0x200000 then raise Malformed_code else main ( i + 5 ) 5 else if n <= 0xfd then let n = trail 5 ( i + 1 ) 1 ( n - 0xfc ) 0xfc in if n lsr 16 < 0x400 then raise Malformed_code else main ( i + 6 ) 6 else raise Malformed_code in main 0 |
module Buf = struct include Buffer type buf = t let add_char = add_uchar end |
let pp_pos ppf d = pp ppf " % d . % d ( :% d , % 06X ) " ( Uutf . decoder_line d ) ( Uutf . decoder_col d ) ( Uutf . decoder_count d ) ( Uutf . decoder_byte_count d ) |
let pp_decode inf d ppf v = pp ppf " [ @< h >% s :% a % a ] @@\ n " inf pp_pos d Uutf . pp_decode v |
let exec = Filename . basename Sys . executable_name |
let log f = Format . eprintf ( " % s : " ^^ f ^^ " " ) @? exec |
let input_malformed = ref false |
let log_malformed inf d v = input_malformed := true ; log " % a " ( pp_decode inf d ) v |
let rec unix_read fd s j l = try Unix . read fd s j l with |
let rec unix_write fd s j l = let rec write fd s j l = try Unix . single_write fd s j l with | Unix . Unix_error ( Unix . EINTR , _ , _ ) -> write fd s j l in let wc = write fd s j l in if wc < l then unix_write fd s ( j + wc ) ( l - wc ) else ( ) |
let string_of_channel use_unix ic = let b = Buffer . create unix_buffer_size in let input , s = if use_unix then unix_read ( Unix . descr_of_in_channel ic ) , Bytes . create unix_buffer_size else input ic , Bytes . create io_buffer_size in let rec loop b input s = let rc = input s 0 ( Bytes . length s ) in if rc = 0 then Buffer . contents b else ( Buffer . add_substring b ( Bytes . unsafe_to_string s ) 0 rc ; loop b input s ) in loop b input s |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.