~ubuntu-branches/ubuntu/utopic/ocamlbricks/utopic-proposed

« back to all changes in this revision

Viewing changes to EXTRA/stringExtra.ml

  • Committer: Package Import Robot
  • Author(s): Lucas Nussbaum
  • Date: 2014-07-08 11:09:39 UTC
  • mfrom: (3.1.7 sid)
  • Revision ID: package-import@ubuntu.com-20140708110939-xaex1s3nqepm56eh
Tags: 0.90+bzr400-1
New upstream snapshot.

Show diffs side-by-side

added added

removed removed

Lines of Context:
516
516
    (fun k src -> let l=(String.length src) in (blit src 0 dst k l); (k+l)) 0 xs in
517
517
 dst
518
518
 
 
519
(** Remove all occurrences of a character from a string. *) 
 
520
let rm d s = concat (split ~d s) ;;
 
521
 
519
522
(** Quote a string using a prefix [l] (by default [l="'"]) and a suffix [r] (by default [r="'"]). *)
520
523
let quote ?(l="'") ?(r="'") (x:string) = String.concat "" [l;x;r]
521
524
 
524
527
let assemble_if_not_empty ~prefix ~suffix x =
525
528
  if (x="") then "" else (String.concat "" [prefix;x;suffix])
526
529
 
527
 
(** [merge_map f l] maps the function [f] on the list [l]
 
530
(** [map_concat f l] maps the function [f] on the list [l]
528
531
    then merge the result with the separator ([sep=" "] by default). *)
529
532
let map_concat ?(sep=" ") f l = String.concat sep (List.map f l)
530
533
 
545
548
# ensure_cr_at_end "hello\n";;
546
549
  : string = "hello\n"]}*)
547
550
let ensure_cr_at_end x =
 
551
 if x="" then "\n" else (* continue *)
548
552
 let l    = (String.length x) in
549
553
 let last = (String.sub x (l-1) 1) in
550
554
 match last with "\n" -> x | _ -> x^"\n"
557
561
    The last line in the text may not terminate with a newline. *)
558
562
module Text = struct
559
563
 
560
 
(** A (line structured) text is a {b list} of strings. *)
561
 
type t = string list
562
 
 
563
564
(** In this context, a line is not structured, it's a flatten string. *)
564
565
type line = string
565
566
 
 
567
(** A (line structured) text is a {b list} of strings. *)
 
568
type t = line list
 
569
 
 
570
 
566
571
(** Convert a string list in a raw text.
567
572
    Each string in the input list is treated by the function [ensure_cr_at_end] in order to
568
573
    add a newline if needed, then the list is folded by a simple catenation ([^]).
641
646
  let s = String.concat (Char.escaped d) t in
642
647
  split ?do_not_squeeze ~d s
643
648
 
 
649
(** Merge fixed-length size groups of lines. *)  
 
650
let merge_lines ?(sep=" ") (n:int) xs =
 
651
  let xss = ArrayExtra.amass (n) (Array.of_list xs) in
 
652
  let zs  = Array.map (fun ys -> String.concat sep (Array.to_list ys)) xss in
 
653
  Array.to_list zs
 
654
 
644
655
(** Converting raw text to matrix (list of list) of strings (words) and vice-versa. *)
645
656
module Matrix = struct
646
657