summaryrefslogtreecommitdiff
path: root/src/regex.urs
blob: d3b08150b68f699180e6e86b1b015262c9a59c6c (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
(* Copyright 2015 the Massachusetts Institute of Technology
Copyright 2015 Benjamin Barenblat

Licensed under the Apache License, Version 2.0 (the "License"); you may not use
this file except in compliance with the License.  You may obtain a copy of the
License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.  See the License for the
specific language governing permissions and limitations under the License. *)

(* Regular expression matching

This library implements ECMAScript regular expressions. *)

type match a = {Whole : a, Groups : list a}
type counted_substring = {Start : int, Len : int}

(* Searching *)

(* Matches a regular expression against any part of a string.  Returns
'Some match' if a match succeeds and 'None' otherwise. *)
val match : string (* needle *)
            -> string (* haystack *)
            -> option (match string)
val match' : string -> string -> option (match counted_substring)

(* Finds _all_ matches for a regular expression in a string. *)
val all_matches : string (* needle *)
                  -> string (* haystack *)
                  -> list (match string)
val all_matches' : string -> string -> list (match counted_substring)

(* Replacement *)

(* Replaces all substrings in 'haystack' that match 'needle' with the string
'replacement.' *)
val replace : string (* needle *)
              -> string (* replacement *)
              -> string (* haystack *)
              -> string

(* Transforms a string by applying a function to replace every match in the
string. *)
val transform_matches : string (* needle *)
                        -> (match string -> string) (* transformation *)
                        -> string (* haystack *)
                        -> string
val transform_matches' : string
                         -> (match counted_substring -> string)
                         -> string
                         -> string

(* Executes a general regex-guided transformation over a string.  Matches
'needle' against any part of 'haystack', splitting 'haystack' into matching and
nonmatching regions.  Then, runs the provided transformation functions over the
regions and concatenates the results.

The number of nonmatching regions is always exactly one more than the number of
matching regions.  If two matching regions abut or a matching region adjoins the
edge of a string, this function will insert an empty nonmatching region as
appropriate.

An example may make this a bit clearer:

    let
      val haystack "axbxax"
    in
      transform "x"
                (fn nm => "_" ^ String.substring haystack nm ^ "_")
                (fn  m => "*" ^ String.substring haystack  m ^ "_")
                haystack
    end

evaluates to

    "_a_*x*_b_*x*__"
*)
val transform : string (* needle *)
                -> (string -> string)  (* non-matching transformation *)
                -> (match string -> string)  (* matching transformation *)
                -> string (* haystack *)
                -> string
val transform' : string
                 -> (counted_substring -> string)
                 -> (match counted_substring -> string)
                 -> string
                 -> string