Mitsuhiro Nakamura avatar Mitsuhiro Nakamura committed 45c13ab

Modified many things.

Comments (0)

Files changed (3)

 open Ext
 
-let permute_lines filenames =
-  (** Shuffle contents of filenames and print them by each line. *)
-  (match filenames with
-     []        -> [stdin]
-   | filenames -> List.map open_in filenames)
-  |! (fun cins ->
-        let ls = List.concat_map rev_input_lines cins in
-          List.iter close_in cins;
-          ls)
-  |! List.permute
-  |! List.iter print_endline
+let perform_file_lines_all args =
+  let cins = match args with
+             | [] -> [stdin]
+             | xs -> List.map open_in xs in
+  let ss = List.concat_map rev_input_lines cins in
+  let strm = Shufflee.of_list ss in
+  begin
+    List.iter close_in cins;
+    Shufflee.iter print_endline strm
+  end
 
-let permute_names names =
-  (** Shuffle names as symbols and print them. *)
-  List.permute names
-  |! String.concat " "
-  |! print_endline
+let perform_file_lines_limit k args =
+  let cins = match args with
+             | [] -> [stdin]
+             | xs -> List.map open_in xs in
+  let ss = List.concat_map rev_input_lines cins in
+  let strm = Shufflee.of_list ss in
+  begin
+    List.iter close_in cins;
+    let ss' = Shufflee.npeek k strm in
+    List.iter print_endline ss'
+  end
 
-let permute_numbers n =
-  (** Shuffle numbers [1..n] and print them. *)
+(*
+ * let perform_file_words_all args =
+ *   let cins = match args with
+ *              | [] -> [stdin]
+ *              | xs -> List.map open_in xs in
+ *   let ss = List.concat_map rev_input_words cins in
+ *   let strm = Shufflee.of_list ss in
+ *   begin
+ *     List.iter close_in cins;
+ *     Shufflee.iter print_endline strm
+ *   end
+ * 
+ *)
+
+(*
+ * let perform_file_words_limit k args =
+ *   let cins = match args with
+ *              | [] -> [stdin]
+ *              | xs -> List.map open_in xs in
+ *   let ss = List.concat_map rev_input_words cins in
+ *   let strm = Shufflee.of_list ss in
+ *   begin
+ *     List.iter close_in cins;
+ *     let ss' = Shufflee.npeek k strm in
+ *     List.iter print_endline ss'
+ *   end
+ *)
+
+let perform_file_chars_all args =
+  let cins = match args with
+             | [] -> [stdin]
+             | xs -> List.map open_in xs in
+  let s = String.concat_map input_all cins in
+  let strm = Shufflee.of_string s in
+  begin
+    List.iter close_in cins;
+    Shufflee.iter print_char strm;
+    print_newline ()
+  end
+
+let perform_file_chars_limit k args =
+  let cins = match args with
+             | [] -> [stdin]
+             | xs -> List.map open_in xs in
+  let s = String.concat_map input_all cins in
+  let strm = Shufflee.of_string s in
+  begin
+    List.iter close_in cins;
+    let ss' = Shufflee.npeek k strm in
+    List.iter print_char ss';
+    print_newline ()
+  end
+
+let perform_quoted_words_all args =
+  let strm = Shufflee.of_list args in
+  begin
+    Shufflee.iter (fun s -> print_string s; print_string " ") strm;
+    print_newline ()
+  end
+
+let perform_quoted_words_limit k args =
+  let strm = Shufflee.of_list args in
+  let ss' = Shufflee.npeek k strm in
+  begin
+    List.iter (fun s -> print_string s; print_string " ") ss';
+    print_newline ()
+  end
+
+let perform_natural_all n =
   if n < 0 then
     failwith "option `-n' must be positive"
   else
-    (List.init n (fun i -> i + 1)
-     |! List.permute
-     |! List.map string_of_int
-     |! String.concat " "
-     |! print_endline)
+    let strm = Shufflee.of_list (List.init n identity) in
+    begin
+      Shufflee.iter (fun i -> print_int i; print_string " ") strm;
+      print_newline ()
+    end
 
-let choose_numbers n k =
-  (** Shuffle numbers [1..n], choose k from them, and print them. *)
+let perform_natural_limit k n =
   if n < 0 then
     failwith "option `-n' must be positive"
   else if k < 0 then
     failwith "option `-k' must be positive"
-  else if n < k then
-    failwith "option `-n' must be bigger than or equal to option `-k'"
   else
-    (List.init n (fun i -> i + 1)
-     |! List.permute
-     |! (fun xs -> List.sub xs 0 k)
-     |! List.map string_of_int
-     |! String.concat " "
-     |! print_endline)
+    let strm = Shufflee.of_list (List.init n identity) in
+    let ns = Shufflee.npeek k strm in
+    begin
+      List.iter (fun i -> print_int i; print_string " ") ns;
+      print_newline ()
+    end
 
-(** val (|!) : 'a -> ('a -> 'b) -> 'b *)
-let (|!) x f = f x
+let identity x = x
 
-(** val rev_input_lines : in_channel -> string list *)
+(* Bollowed from caml-core *)
+let input_all cin =
+  (* We use 4096 because that is the size of OCaml's IO buffers. *)
+  let buf_size = 4096 in
+  let buf = String.create buf_size in
+  let buffer = Buffer.create buf_size in
+  let rec loop () =
+    let len = input cin buf 0 (String.length buf) in
+    if len > 0 then
+      begin
+        Buffer.add_substring buffer buf 0 len;
+        loop ()
+      end in
+  loop ();
+  Buffer.contents buffer
+
 let rev_input_lines cin =
   let may_eof f =
     try Some (f ())
   let rec loop ls =
     match may_eof (fun () -> input_line cin) with
       None -> ls
-    | Some l -> loop (l :: ls)
-  in
-    loop []
-
-module Array =
-struct
-  include Array
-
-  (** val permute : 'a array -> unit *)
-  let permute xs =
-    for n = Array.length xs downto 2 do
-      let i = n - 1 and j = Random.int n in
-      let x = xs.(i) in
-        xs.(i) <- xs.(j);
-        xs.(j) <- x
-    done
-end
+    | Some l -> loop (l :: ls) in
+  loop []
 
 module List =
 struct
   include List
 
-  (** val combine3 : 'a list -> 'b list -> 'c list -> ('a, 'b, 'c) list *)
-  let rec combine3 xs ys zs =
+  let combine3 xs ys zs =
     let rec loop xs ys zs result =
       match (xs, ys, zs) with
       | ([], [], []) -> rev result
       | (x :: xs, y :: ys, z :: zs) -> loop xs ys zs ((x, y, z) :: result)
-      | _ -> invalid_arg "List.combine3"
-    in
+      | _ -> invalid_arg "List.combine3" in
     loop xs ys zs []
 
-  (** val init : int -> (int -> 'a) -> 'a list *)
   let init n f =
     let rec loop i xs =
       if i > 0
       then loop (i - 1) (f (i - 1) :: xs)
-      else xs
-    in
-      loop n []
+      else xs in
+    loop n []
 
-  (** val sub : 'a list -> int -> int -> 'a list *)
-  let sub xs start len =
-    (* TODO: Implement without using Array *)
-    let a = Array.of_list xs in
-    let a' = Array.sub a start len in
-      Array.to_list a'
-
-  (** val concat_map : ('a -> 'b list) -> 'a list -> 'b list *)
   let concat_map f xs =
-    (* TODO: Optimization *)
+    (* TODO: Optimize it *)
     concat (map f xs)
-
-  (** val permute : 'a list -> 'a list *)
-  let permute xs =
-    let a = Array.of_list xs in
-      Array.permute a;
-      Array.to_list a
 end
 
+module String =
+struct
+  include String
+
+  let concat_map ?(sep = "") f ss =
+    (* TODO: Optimize it *)
+    concat sep (List.map f ss)
+end
+
 let _ =
   let opt = ArgOpt.create ()
-  and rest = ref [] in
-  let header = ArgOpt.header ()
-  and specs = ArgOpt.init_spec opt in
-  try begin
+  and args = ref [] in
+  let header = ArgOpt.init_header ()
+  and specs = ArgOpt.init_specs opt in
+  try
     Random.self_init ();
-    Arg.parse specs (fun r -> rest := r :: !rest) header;
+    Arg.parse specs (fun a -> args := a :: !args) header;
     ArgOpt.set_default opt;
     let open ArgOpt in
+    let open DoShuffle in
     begin
       match opt with
-      | { shufflee = `File; split_by = `Line; limit = `All } ->
-          DoShuffle.permute_lines !rest
-      | { shufflee = `File; split_by = _; limit = _ } ->
-          prerr_endline ("Error:\t" ^ "Not implemented")
-      | { shufflee = `Quoted; split_by = `Word; limit = `All } ->
-          DoShuffle.permute_names !rest
-      | { shufflee = `Quoted; split_by = _; limit = _ } ->
-          prerr_endline ("Error:\t" ^ "Not implemented")
-      | { shufflee = `Natural n; split_by = `Default; limit = `All } ->
-          DoShuffle.permute_numbers n
-      | { shufflee = `Natural n; split_by = `Default; limit = `Limit k } ->
-          DoShuffle.choose_numbers n k
-      | { shufflee = `Natural n; split_by = _; limit = _ } ->
-          prerr_endline ("Error:\t" ^ "option `-n' conflicts with option `-l', '-w', or '-c'")
-      | _ -> prerr_endline ("Error:\t" ^ "Not implemented");
+      | {source = `File; split_by = `Line; limit = `All} ->
+          perform_file_lines_all !args
+      | {source = `File; split_by = `Line; limit = `Limit k} ->
+          perform_file_lines_limit k !args
+      | {source = `File; split_by = `Word; limit = `All} ->
+          prerr_endline ("Error:\t" ^ "At the moment not available")
+      | {source = `File; split_by = `Word; limit = `Limit k} ->
+          prerr_endline ("Error:\t" ^ "At the moment not available")
+      | {source = `File; split_by = `Char; limit = `All} ->
+          perform_file_chars_all !args
+      | {source = `File; split_by = `Char; limit = `Limit k} ->
+          perform_file_chars_limit k !args
+      | {source = `Quoted; split_by = `Line; limit = `All} ->
+          prerr_endline ("Error:\t" ^ "At the moment not available")
+      | {source = `Quoted; split_by = `Line; limit = `Limit k} ->
+          prerr_endline ("Error:\t" ^ "At the moment not available")
+      | {source = `Quoted; split_by = `Word; limit = `All} ->
+          perform_quoted_words_all !args
+      | {source = `Quoted; split_by = `Word; limit = `Limit k} ->
+          perform_quoted_words_limit k !args
+      | {source = `Quoted; split_by = `Char; limit = `All} ->
+          prerr_endline ("Error:\t" ^ "At the moment not available")
+      | {source = `Quoted; split_by = `Char; limit = `Limit k} ->
+          prerr_endline ("Error:\t" ^ "At the moment not available")
+      | {source = `Natural n; split_by = `Default; limit = `All} ->
+          perform_natural_all n
+      | {source = `Natural n; split_by = `Default; limit = `Limit k} ->
+          perform_natural_limit k n
+      | {source = `Natural _} ->
+          prerr_endline ("Error:\t"
+          ^ "option `-n' conflicts with option `-l', '-w', or '-c'")
+      | _ ->
+          ignore (failwith "Undefined error");
       exit 0
     end
-  end with Sys_error msg | Failure msg | Arg.Bad msg ->
+  with Sys_error msg | Failure msg | Arg.Bad msg ->
     prerr_endline ("Error:\t" ^ msg)
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.