fix fix_string

Ignore-this: 58f31abea9193b83b24d22a659e203e4

darcs-hash:20120215204740-c41ad-b6a63ec253a8b796a82bfb8a9db0b0e098fac131
This commit is contained in:
Jeremie Dimino 2012-02-15 21:47:40 +01:00
parent bd9d70ecfc
commit 02e8648628
1 changed files with 17 additions and 21 deletions

View File

@ -169,29 +169,25 @@ end
let fix_string str = let fix_string str =
let len = String.length str in let len = String.length str in
let ofs = ref 0 in let ofs, _, _ = Zed_utf8.next_error str 0 in
try if ofs = len then
while !ofs < len do
ofs := Zed_utf8.unsafe_next str !ofs
done;
str str
with Zed_utf8.Invalid _ -> else begin
let buf = Buffer.create (len + 128) in let buf = Buffer.create (len + 128) in
if !ofs > 0 then Buffer.add_substring buf str 0 !ofs; if ofs > 0 then Buffer.add_substring buf str 0 ofs;
Printf.bprintf buf "\\y%02x" (Char.code (String.unsafe_get str !ofs)); let rec loop ofs =
incr ofs; Printf.bprintf buf "\\y%02x" (Char.code (String.unsafe_get str ofs));
let ofs2 = ref !ofs in let ofs1 = ofs + 1 in
while !ofs2 < len do let ofs2, _, _ = Zed_utf8.next_error str ofs1 in
try if ofs1 < ofs2 then
ofs2 := Zed_utf8.unsafe_next str !ofs2 Buffer.add_substring buf str ofs1 (ofs2 - ofs1);
with Zed_utf8.Invalid _ -> if ofs2 < len then
if !ofs < !ofs2 then Buffer.add_substring buf str !ofs (!ofs2 - !ofs); loop ofs2
Printf.bprintf buf "\\y%02x" (Char.code (String.unsafe_get str !ofs2)); else
incr ofs2; Buffer.contents buf
ofs := !ofs2 in
done; loop ofs
if !ofs < len then Buffer.add_substring buf str !ofs (len - !ofs); end
Buffer.contents buf
let print_out_phrase term printer pp out_phrase = let print_out_phrase term printer pp out_phrase =
flush stdout; flush stdout;