-
Notifications
You must be signed in to change notification settings - Fork 300
wsproxy: code removal and stylistic changes #7099
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -39,9 +39,16 @@ module Iteratee (IO : Monad) = struct | |
| | IE_done of 'a | ||
| | IE_cont of err option * (stream -> ('a t * stream) IO.t) | ||
|
|
||
| module IO_Ops = struct | ||
| let ( let* ) = IO.bind | ||
|
|
||
| let return = IO.return | ||
| end | ||
|
|
||
| let return x = IE_done x | ||
|
|
||
| let rec bind i f = | ||
| let open IO_Ops in | ||
| match i with | ||
| | IE_done result -> | ||
| f result | ||
|
|
@@ -52,12 +59,16 @@ module Iteratee (IO : Monad) = struct | |
| | IE_cont (None, k) -> | ||
| k stream | ||
| | x -> | ||
| IO.return (x, stream) | ||
| return (x, stream) | ||
| ) | ||
| | x, stream -> | ||
| IO.return (bind x f, stream) | ||
| return (bind x f, stream) | ||
| in | ||
| let go s = | ||
| let* p = k s in | ||
| docase p | ||
| in | ||
| IE_cont (e, fun s -> IO.bind (k s) docase) | ||
| IE_cont (e, go) | ||
|
|
||
| let ( >>= ) = bind | ||
|
|
||
|
|
@@ -67,14 +78,6 @@ module Iteratee (IO : Monad) = struct | |
|
|
||
| let ie_errM msg k x = IO.return (IE_cont (Some msg, k), x) | ||
|
|
||
| let state = function | ||
| | IE_done _ -> | ||
| "Done" | ||
| | IE_cont (None, _) -> | ||
| "Ready" | ||
| | IE_cont (Some e, _) -> | ||
| Printf.sprintf "Error (%s)" e | ||
|
|
||
| (* Simplest iteratees *) | ||
|
|
||
| let rec peek = | ||
|
|
@@ -107,14 +110,14 @@ module Iteratee (IO : Monad) = struct | |
| IE_cont (None, step) | ||
|
|
||
| let writer really_write _ = | ||
| let open IO_Ops in | ||
| let rec step st = | ||
| match st with | ||
| | Chunk s -> | ||
| IO.bind (really_write s) (fun () -> | ||
| IO.return (IE_cont (None, step), Chunk "") | ||
| ) | ||
| let* () = really_write s in | ||
| return (IE_cont (None, step), Chunk "") | ||
| | Eof _ -> | ||
| IO.return (IE_done (), st) | ||
| return (IE_done (), st) | ||
| in | ||
| IE_cont (None, step) | ||
|
|
||
|
|
@@ -188,11 +191,13 @@ module Iteratee (IO : Monad) = struct | |
| in | ||
| IE_cont (None, step "" n) | ||
|
|
||
| let read_int8 = readn 1 >>= fun s -> return (unmarshal_int8 s) | ||
| let ( >> ) f g x = g (f x) | ||
|
|
||
| let read_int8 = readn 1 >>= (unmarshal_int8 >> return) | ||
|
|
||
| let read_int16 = readn 2 >>= fun s -> return (unmarshal_int16 s) | ||
| let read_int16 = readn 2 >>= (unmarshal_int16 >> return) | ||
|
|
||
| let read_int32 = readn 4 >>= fun s -> return (unmarshal_int32 s) | ||
| let read_int32 = readn 4 >>= (unmarshal_int32 >> return) | ||
|
|
||
| let drop_while pred = | ||
| let rec step st = | ||
|
|
@@ -208,26 +213,6 @@ module Iteratee (IO : Monad) = struct | |
| in | ||
| IE_cont (None, step) | ||
|
|
||
| let accumulate = | ||
| let rec step acc st = | ||
| match st with | ||
| | Chunk s -> | ||
| ie_contM (step (acc ^ s)) (Chunk "") | ||
| | Eof _ -> | ||
| ie_doneM acc st | ||
| in | ||
| IE_cont (None, step "") | ||
|
|
||
| let apply f = | ||
| let rec step st = | ||
| match st with | ||
| | Chunk s -> | ||
| f s ; ie_contM step (Chunk "") | ||
| | Eof _ -> | ||
| ie_doneM () st | ||
| in | ||
| IE_cont (None, step) | ||
|
|
||
| let liftI m = | ||
| let step st i = | ||
| match i with | ||
|
|
@@ -245,145 +230,119 @@ module Iteratee (IO : Monad) = struct | |
| (* Simplest enumarator *) | ||
|
|
||
| let enum_eof i = | ||
| let open IO_Ops in | ||
| let result = | ||
| match i with | ||
| | IE_cont (None, f) -> | ||
| IO.bind (f (Eof None)) (fun x -> IO.return (fst x)) | ||
| let* i, _ = f (Eof None) in | ||
| return i | ||
| | _ -> | ||
| IO.return i | ||
| return i | ||
| in | ||
| IO.bind result (function | ||
| | IE_done _ -> | ||
| result | ||
| | IE_cont (Some _, _) -> | ||
| result | ||
| | _ -> | ||
| failwith "Divergent Iteratee" | ||
| ) | ||
| let* it = result in | ||
| match it with | ||
| | IE_done _ | IE_cont (Some _, _) -> | ||
| result | ||
| | _ -> | ||
| failwith "Divergent iteratee" | ||
|
|
||
| let enum_1chunk str = function | ||
| let enum_1chunk str = | ||
| let open IO_Ops in | ||
| function | ||
| | IE_cont (None, f) -> | ||
| IO.bind (f (Chunk str)) (fun x -> IO.return (fst x)) | ||
| let* i, _ = f (Chunk str) in | ||
| return i | ||
| | x -> | ||
| IO.return x | ||
| return x | ||
|
|
||
| let rec enum_nchunk str n = | ||
| if str = "" then | ||
| fun x -> | ||
| IO.return x | ||
| else | ||
| let str1, str2 = split str n in | ||
| function | ||
| | IE_cont (None, f) -> | ||
| IO.bind | ||
| (IO.bind (f (Chunk str1)) (fun x -> IO.return (fst x))) | ||
| (enum_nchunk str2 n) | ||
| | x -> | ||
| IO.return x | ||
|
|
||
| let extract_result_from_iteratee = function | ||
| | IE_done x -> | ||
| x | ||
| | _ -> | ||
| failwith "Not done!" | ||
|
|
||
| type 'a enumeratee = 'a t -> 'a t t | ||
| let open IO_Ops in | ||
| match str with | ||
| | "" -> | ||
| return | ||
| | _ -> ( | ||
| function | ||
| | IE_cont (None, f) -> | ||
| let s1, s2 = split str n in | ||
| let* i = | ||
| let* i, _ = f (Chunk s1) in | ||
| return i | ||
| in | ||
| enum_nchunk s2 n i | ||
| | x -> | ||
| return x | ||
| ) | ||
|
|
||
| let rec take = | ||
| let step n k s = | ||
| let open IO_Ops in | ||
| match s with | ||
| | Chunk str -> | ||
| let len = String.length str in | ||
| if len < n then | ||
| IO.bind (k s) (fun (i, _) -> IO.return (take (n - len) i, Chunk "")) | ||
| let* i, _ = k s in | ||
| return (take (n - len) i, Chunk "") | ||
| else | ||
| let str1, str2 = split str n in | ||
| IO.bind (k (Chunk str1)) (fun (i, _) -> | ||
| IO.return (IE_done i, Chunk str2) | ||
| ) | ||
| let s1, s2 = split str n in | ||
| let* i, _ = k (Chunk s1) in | ||
| return (IE_done i, Chunk s2) | ||
| | Eof _ -> | ||
| IO.bind (k s) (fun (i, _) -> IO.return (IE_done i, s)) | ||
| let* i, _ = k s in | ||
| return (IE_done i, s) | ||
| in | ||
| function | ||
| | 0 -> | ||
| return | ||
| | n -> ( | ||
| fun s -> | ||
| match s with | ||
| | IE_cont (None, k) -> | ||
| IE_cont (None, step n k) | ||
| | IE_cont (Some _, _) | IE_done _ -> | ||
| bind (drop n) (fun () -> return s) | ||
| function | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is neater. But to be honest, |
||
| | IE_cont (None, k) -> | ||
| IE_cont (None, step n k) | ||
| | (IE_cont (Some _, _) | IE_done _) as it -> | ||
| bind (drop n) (fun () -> return it) | ||
| ) | ||
|
|
||
| let stream_printer name = | ||
| let rec step k s = | ||
| let open IO_Ops in | ||
| Printf.printf "%s: %s\n" name (string_of_stream s) ; | ||
| IO.bind (k s) (fun i -> | ||
| match i with | ||
| | IE_cont (None, f), s -> | ||
| IO.return (IE_cont (None, step f), s) | ||
| | IE_cont (err, f), s -> | ||
| IO.return (IE_cont (err, step f), s) | ||
| | i, s -> | ||
| IO.return (IE_done i, s) | ||
| ) | ||
| let* i, s = k s in | ||
| match i with | ||
| | IE_cont (err, f) -> | ||
| return (IE_cont (err, step f), s) | ||
| | _ -> | ||
| return (IE_done i, s) | ||
| in | ||
| fun s -> | ||
| match s with | ||
| | IE_cont (None, k) -> | ||
| IE_cont (None, step k) | ||
| | IE_cont (Some _, _) | IE_done _ -> | ||
| return s | ||
| function | ||
| | IE_cont (None, k) -> | ||
| IE_cont (None, step k) | ||
| | (IE_cont (Some _, _) | IE_done _) as it -> | ||
| return it | ||
|
|
||
| let modify f = | ||
| let rec step k s = | ||
| let open IO_Ops in | ||
| match s with | ||
| | Chunk c -> | ||
| | Chunk c -> ( | ||
| let s = | ||
| try f c | ||
| with e -> | ||
| Printf.printf "got exception %s\n%!" (Printexc.to_string e) ; | ||
| raise e | ||
| in | ||
| IO.bind (k (Chunk s)) (fun i -> | ||
| match i with | ||
| | IE_cont (None, f), s -> | ||
| IO.return (IE_cont (None, step f), s) | ||
| | IE_cont (err, f), s -> | ||
| IO.return (IE_cont (err, step f), s) | ||
| | i, s -> | ||
| IO.return (IE_done i, s) | ||
| ) | ||
| let* i, s = k (Chunk s) in | ||
| match i with | ||
| | IE_cont (err, f) -> | ||
| return (IE_cont (err, step f), s) | ||
| | _ -> | ||
| return (IE_done i, s) | ||
| ) | ||
| | Eof _ -> | ||
| IO.bind (k s) (fun (i, _) -> IO.return (IE_done i, s)) | ||
| let* i, _ = k s in | ||
| return (IE_done i, s) | ||
| in | ||
| fun s -> | ||
| match s with | ||
| | IE_cont (None, k) -> | ||
| IE_cont (None, step k) | ||
| | IE_cont (Some _, _) -> | ||
| return s | ||
| | IE_done _ -> | ||
| return s | ||
|
|
||
| type 'a either = Left of 'a | Right of 'a | ||
|
|
||
| let read_lines = | ||
| let ( >>= ) = bind in | ||
| let iscrlf = function '\r' | '\n' -> true | _ -> false in | ||
| let terminators = | ||
| heads "\r\n" >>= function 0 -> heads "\n" | n -> return n | ||
| in | ||
| let rec lines' acc = break iscrlf >>= fun l -> terminators >>= check acc l | ||
| and check acc l n = | ||
| match (l, n) with | ||
| | _, 0 -> | ||
| return (Left (List.rev acc)) | ||
| | "", _ -> | ||
| return (Right (List.rev acc)) | ||
| | l, _ -> | ||
| lines' (l :: acc) | ||
| in | ||
| lines' [] | ||
| function | ||
| | IE_cont (None, k) -> | ||
| IE_cont (None, step k) | ||
| | (IE_cont (Some _, _) | IE_done _) as it -> | ||
| return it | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I need to keep this in mind: in the continuation
k, thereturnis alwaysIO.return; while inf, it isIteratee.return.