text
stringlengths
12
786k
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 + 0x1...
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...
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 ...
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 . ...
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 _ = 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 . ...
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 . ...
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 . ...
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 ]...
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...
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 )...
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 ...
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 ...
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 . ...
let string_to_channel use_unix oc s = if not use_unix then output_string oc s else let s = Bytes . unsafe_of_string s in unix_write ( Unix . descr_of_out_channel oc ) s 0 ( Bytes . length s )
let dst_for sout = if sout then ` Buffer ( Buffer . create 512 ) else ` Channel stdout
let src_for inf sin use_unix = try let ic = if inf = " " - then stdin else open_in inf in if sin then ` String ( string_of_channel use_unix ic ) else ` Channel ic with Sys_error e -> log " % s \ n " e ; exit 1
let close_src src = try match src with ` Channel ic when ic <> stdin -> close_in ic | _ -> ( ) with | Sys_error e -> log " % s \ n " e ; exit 1
let src_for_unix inf = try if inf = " " - then Unix . stdin else Unix . ( openfile inf [ O_RDONLY ] 0 ) with | Unix . Unix_error ( e , _ , v ) -> log " % s : % s \ n " ( Unix . error_message e ) v ; exit 1
let close_src_unix fd = try if fd <> Unix . stdin then Unix . close fd with
let rec encode_unix fd s e v = match Uutf . encode e v with ` Ok -> ( ) unix_write fd s 0 ( Bytes . length s - Uutf . Manual . dst_rem e ) ; Uutf . Manual . dst e s 0 ( Bytes . length s ) ; encode_unix fd s e ` Await
let dump_decode inf d v = ( match v with ` Malformed _ -> input_malformed := true | _ -> ( ) ) ; ( pp_decode inf d ) Format . std_formatter v
let dump_ inf encoding nln src = let rec loop inf d = match Uutf . decode d with ` Await -> assert false | v -> dump_decode inf d v ; if v <> ` End then loop inf d in loop inf ( Uutf . decoder ? nln ? encoding src )
let dump_unix inf encoding nln usize fd = let rec loop fd s d = match Uutf . decode d with | ` Await -> let rc = unix_read fd s 0 ( Bytes . length s ) in Uutf . Manual . src d s 0 rc ; loop fd s d | v -> dump_decode inf d v ; if v <> ` End then loop fd s d in loop fd ( Bytes . create u...
let dump inf sin use_unix usize ie nln = if sin || not use_unix then dump_ inf ie nln ( src_for inf sin use_unix ) else dump_unix inf ie nln usize ( src_for_unix inf )
let guess inf = let d = Uutf . decoder ( src_for inf false false ) in ignore ( Uutf . decode d ) ; Format . printf " % s . " @ ( Uutf . encoding_to_string ( Uutf . decoder_encoding d ) )
let decode_ inf encoding nln src = let malformed = log_malformed inf in let rec loop d = match Uutf . decode d with ` Await -> assert false | ` Uchar _ -> loop d | ` End -> ( ) | ` Malformed _ as v -> malformed d v ; loop d in loop ( Uutf . decoder ? nln ? encoding src ) ; close_sr...
let decode_unix inf encoding nln usize fd = let malformed = log_malformed inf in let rec loop fd s d = match Uutf . decode d with | ` Uchar _ -> loop fd s d | ` End -> ( ) | ` Malformed _ as v -> malformed d v ; loop fd s d | ` Await -> let rc = unix_read fd s 0 ( Bytes . length s ) ...
let decode inf sin use_unix usize ie nln = if sin || not use_unix then decode_ inf ie nln ( src_for inf sin use_unix ) else decode_unix inf ie nln usize ( src_for_unix inf )
let u_surrogate_count = 0xDFFF - 0xD800 + 1
let uchar_count = ( 0x10FFFF + 1 ) - u_surrogate_count
let r_uchar ( ) = let n = Random . int uchar_count in Uchar . of_int ( if n > 0xD7FF then n + u_surrogate_count else n )
let r_text encoding encode_f rcount = encode_f ( ` Uchar Uutf . u_bom ) ; for i = 1 to rcount do encode_f ( ` Uchar ( r_uchar ( ) ) ) done ; encode_f ` End
let encode_f encoding dst = let e = Uutf . encoder encoding dst in fun v -> match Uutf . encode e v with ` Ok -> ( ) | ` Partial -> assert false
let encode_f_unix usize encoding fd = let e , s = Uutf . encoder encoding ` Manual , Bytes . create usize in Uutf . Manual . dst e s 0 ( Bytes . length s ) ; encode_unix fd s e
let r_encode sout use_unix usize rseed rcount oe = let rseed = match rseed with | None -> Random . self_init ( ) ; Random . int ( 1 lsl 30 - 1 ) | Some rseed -> rseed in let dst = dst_for sout in let oe = match oe with None -> ` UTF_8 | Some enc -> enc in let encode_f = if sout || not u...
let trip_ inf nln ie oe src dst = let malformed d v e = log_malformed inf d v ; ignore ( Uutf . encode e ( ` Uchar Uutf . u_rep ) ) in let rec loop d e = function ` Await -> assert false | ` Uchar _ as v -> ignore ( Uutf . encode e v ) ; loop d e ( Uutf . decode d ) | ` End ...
let trip_unix inf usize nln ie oe fdi fdo = let malformed d v e = log_malformed inf d v ; ignore ( Uutf . encode e ( ` Uchar Uutf . u_rep ) ) in let rec loop fdi fdo ds es d e = function | ` Uchar _ as v -> encode_unix fdo es e v ; loop fdi fdo ds es d e ( Uutf . decode d ) | ` End -...
let trip inf sin sout use_unix usize ie oe nln = let src = src_for inf sin use_unix in let dst = dst_for sout in if sin || sout || not use_unix then trip_ inf nln ie oe src dst else trip_unix inf usize nln ie oe ( src_for_unix inf ) Unix . stdout ; match dst with ` Channel _ -> ( ) | ` Buffer b ...
let do_cmd cmd inf sin sout use_unix usize ie oe nln rseed rcount = match cmd with | ` Ascii -> dump inf sin use_unix usize ie nln | ` Guess -> guess inf | ` Decode -> decode inf sin use_unix usize ie nln | ` Encode -> r_encode sout use_unix usize rseed rcount oe | ` Trip -> trip inf sin sout use_u...
let enc_enum = [ " UTF - 8 " , ` UTF_8 ; " UTF - 16 " , ` UTF_16 ; " UTF - 16LE " , ` UTF_16LE ; " UTF - 16BE " , ` UTF_16BE ; ]
let decode_enc_enum = ( " ASCII " , ` US_ASCII ) :: ( " latin1 " , ` ISO_8859_1 ) :: enc_enum
let ienc = let doc = str " Decoded ( input ) encoding , must % s . If unspecified the encoding is guessed . " ( Arg . doc_alts_enum decode_enc_enum ) in Arg . ( value & opt ( some ( enum decode_enc_enum ) ) None & info [ " d " ; " input - encoding " ] ~ doc )
let oenc = let doc = str " Encoded ( output ) encoding , must % s . If unspecified the output encoding is the same as the input encoding except for ASCII and latin1 where UTF - 8 is output . " ( Arg . doc_alts_enum enc_enum ) in Arg . ( value & opt ( some ( enum enc_enum ) ) None ...
let nln = let lf = Uchar . of_int 0x000A in let nln_enum = [ " ascii " , ` ASCII lf ; " nlf " , ` NLF lf ; " readline " , ` Readline lf ] in let doc = str " New line normalization to U + 000A , must % s . ascii normalizes CR ( U + 000D ) and CRLF ( < U + 000D ,...
let sin = let doc = " Input everything in a string and decode the string . " in Arg . ( value & flag & info [ " input - string " ] ~ doc )
let sout = let doc = " Encode everything in a string and output the string . " in Arg . ( value & flag & info [ " output - string " ] ~ doc )
let use_unix = let doc = " Use Unix IO . " in Arg . ( value & flag & info [ " use - unix " ] ~ doc )
let usize = let doc = " Unix IO buffer sizes in bytes . " in Arg . ( value & opt int unix_buffer_size & info [ " unix - size " ] ~ doc )
let nat = let parse s = try let v = int_of_string s in if v > 0 then ` Ok v else failwith ( str " % s must be > 0 " s ) with Failure e -> ` Error e in parse , Format . pp_print_int
let rseed = let doc = " Random seed . " in Arg . ( value & opt ( some nat ) None & info [ " rseed " ] ~ doc )