summaryrefslogtreecommitdiffstats
path: root/otherlibs/labltk/support/support.ml
blob: 72941b6565f9e53fb9897a0117952cf036396f28 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
(*************************************************************************)
(*                                                                       *)
(*                Objective Caml LablTk library                          *)
(*                                                                       *)
(*         Francois Rouaix, Francois Pessaux and Jun Furuse              *)
(*               projet Cristal, INRIA Rocquencourt                      *)
(*            Jacques Garrigue, Kyoto University RIMS                    *)
(*                                                                       *)
(*   Copyright 1999 Institut National de Recherche en Informatique et    *)
(*   en Automatique and Kyoto University.  All rights reserved.          *)
(*   This file is distributed under the terms of the GNU Library         *)
(*   General Public License.                                             *)
(*                                                                       *)
(*************************************************************************)

(* $Id$ *)

(* Parsing results of Tcl *)
(* List.split a string according to char_sep predicate *)
let split_str ~pred:char_sep str =
  let len = String.length str in
  let rec skip_sep cur =
    if cur >= len then cur
    else if char_sep str.[cur] then skip_sep (succ cur)
    else cur  in
  let rec split beg cur =
    if cur >= len then 
      if beg = cur then []
      else [String.sub str ~pos:beg ~len:(len - beg)]
    else if char_sep str.[cur] 
         then 
           let nextw = skip_sep cur in
            (String.sub str ~pos:beg ~len:(cur - beg))
              ::(split nextw nextw)
         else split beg (succ cur) in
  let wstart = skip_sep 0 in
  split wstart wstart

(* Very easy hack for option type *)
let may f = function
  Some x -> Some (f x)
| None -> None

let maycons f x l =
  match x with
    Some x -> f x :: l
  | None -> l