From: malc Date: Sun, 3 Feb 2013 12:23:11 +0000 (+0400) Subject: DJB's maxim - don't parse, but sometimes you do... X-Git-Tag: v14~41 X-Git-Url: https://repo.or.cz/w/llpp.git/commitdiff_plain/71484846d4d068b0b150257c7e8aa795ec254953 DJB's maxim - don't parse, but sometimes you do... --- diff --git a/main.ml b/main.ml index 19d43ea..6fd3905 100644 --- a/main.ml +++ b/main.ml @@ -7039,51 +7039,48 @@ let remote = let scratch = String.create 80 in let buf = Buffer.create 80 in fun fd -> - let rec loop () = - let rec tempfr () = - try Some (Unix.read fd scratch 0 80) - with - | Unix.Unix_error (Unix.EAGAIN, _, _) -> None - | Unix.Unix_error (Unix.EINTR, _, _) -> tempfr () - | exn -> raise exn - in - match tempfr () with - | None -> Some fd - | Some n -> - if n = 0 + let rec tempfr () = + try Some (Unix.read fd scratch 0 80) + with + | Unix.Unix_error (Unix.EAGAIN, _, _) -> None + | Unix.Unix_error (Unix.EINTR, _, _) -> tempfr () + | exn -> raise exn + in + match tempfr () with + | None -> Some fd + | Some n -> + if n = 0 + then ( + Unix.close fd; + if Buffer.length buf > 0 then ( - Unix.close fd; - if Buffer.length buf > 0 - then ( - let s = Buffer.contents buf in - Buffer.clear buf; - ract s; - ); - None - ) - else + let s = Buffer.contents buf in + Buffer.clear buf; + ract s; + ); + None + ) + else + let rec eat ppos = let nlpos = try - let pos = String.index scratch '\n' in + let pos = String.index_from scratch ppos '\n' in if pos >= n then -1 else pos with Not_found -> -1 in if nlpos >= 0 then ( - Buffer.add_substring buf scratch 0 nlpos; + Buffer.add_substring buf scratch ppos (nlpos-ppos); let s = Buffer.contents buf in Buffer.clear buf; - if n - nlpos - 1 > 0 - then Buffer.add_substring buf scratch (nlpos+1) (n-nlpos-1); ract s; - loop () + eat (nlpos+1); ) else ( - Buffer.add_substring buf scratch 0 n; - loop (); + Buffer.add_substring buf scratch ppos (n-ppos); + Some fd ) - in - loop (); + in eat 0 ;; let remoteopen path =