~ubuntu-branches/ubuntu/trusty/ocamlnet/trusty

« back to all changes in this revision

Viewing changes to src/netstring/netstring_pcre.mlip

  • Committer: Bazaar Package Importer
  • Author(s): Stéphane Glondu
  • Date: 2011-09-02 14:12:33 UTC
  • mfrom: (18.2.3 sid)
  • Revision ID: james.westby@ubuntu.com-20110902141233-zbj0ygxb92u6gy4z
Tags: 3.4-1
* New upstream release
  - add a new NetcgiRequire directive to ease dependency management
    (Closes: #637147)
  - remove patches that were applied upstream:
    + Added-missing-shebang-lines-in-example-shell-scripts
    + Try-also-ocamlc-for-POSIX-threads

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
(* $Id: netstring_pcre.mlip 1588 2011-04-28 13:59:54Z gerd $ *)
 
2
 
 
3
(** Wrapper for regexps with PCRE syntax
 
4
 *
 
5
 * This module is a version of [Str] with a thread-safe interface,
 
6
 * implemented using Pcre. 
 
7
 *
 
8
 * This modules processes PCRE-style regular expressions. If you like
 
9
 * to write [Str]-like regexps, you can also use {!Netstring_str} with
 
10
 * almost the same signature.
 
11
 *
 
12
 * {b The functions in this module are unavailable if Ocamlnet is built
 
13
 * with [Str] as implementation of regular expressions! They will always
 
14
 * raise [Invalid_argument]!}
 
15
 *)
 
16
 
 
17
(** Supported regexp syntax: See pcrepattern(3). *)
 
18
 
 
19
IFDEF HAVE_PCRE THEN
 
20
type regexp = Pcre.regexp
 
21
  (** The type of regular expressions; now based on [Pcre] *)
 
22
ELSE
 
23
type regexp
 
24
ENDIF
 
25
 
 
26
 
 
27
IFDEF HAVE_PCRE THEN
 
28
type split_result = Pcre.split_result = 
 
29
  | Text of string
 
30
  | Delim of string
 
31
  | Group of int * string
 
32
  | NoGroup
 
33
  (** Here we keep compatiblity with [Pcre] *)
 
34
ELSE
 
35
type split_result =
 
36
  | Text of string
 
37
  | Delim of string
 
38
  | Group of int * string
 
39
  | NoGroup
 
40
ENDIF
 
41
 
 
42
type result;;
 
43
  (** The type of matching results *)
 
44
 
 
45
val regexp: string -> regexp
 
46
  (** Parses a regexp *)
 
47
val regexp_case_fold: string -> regexp
 
48
  (** Parses a case-insensitive regexp *)
 
49
val quote: string -> string
 
50
  (** Quotes a string such that it can be included in a regexp *)
 
51
val regexp_string: string -> regexp
 
52
  (** Returns a regexp that matches exactly the string *)
 
53
val regexp_string_case_fold: string -> regexp
 
54
  (** Returns a case-insensitive regexp that matches exactly the string *)
 
55
 
 
56
val string_match: 
 
57
      regexp -> string -> int -> result option
 
58
  (** Matches the string at the position with the regexp. Returns
 
59
   * [None] if no match is found. Returns [Some r] on success,
 
60
   * and [r] describes the match.
 
61
   *)
 
62
 
 
63
val search_forward: 
 
64
      regexp -> string -> int -> (int * result)
 
65
  (** Searches a match of the string with the regexp, starting at
 
66
   * the position and in forward direction.
 
67
   * Raises [Not_found] if no match could be found.
 
68
   * Returns [(p,r)] when a match at position [p] is found,
 
69
   * described by [r].
 
70
   *)
 
71
 
 
72
val search_backward:
 
73
      regexp -> string -> int -> (int * result)
 
74
  (** Searches a match of the string with the regexp, starting at
 
75
   * the position and in backward direction.
 
76
   * Raises [Not_found] if no match could be found.
 
77
   * Returns [(p,r)] when a match at position [p] is found,
 
78
   * described by [r].
 
79
   *)
 
80
 
 
81
(*
 
82
  string_partial_match: not available
 
83
*)
 
84
 
 
85
(* The ~groups option is ignored *)
 
86
 
 
87
val matched_string : result -> string -> string
 
88
  (** Extracts the matched part from the string. The string argument
 
89
   * must be the same string passed to [string_match] or the search
 
90
   * functions, and the result argument must be the corresponding
 
91
   * result.
 
92
   *)
 
93
 
 
94
val match_beginning : result -> int
 
95
  (** Returns the position where the matched part begins *)
 
96
 
 
97
val match_end : result -> int
 
98
  (** Returns the position where the matched part ends *)
 
99
 
 
100
val matched_group : result -> int -> string -> string
 
101
  (** Extracts the substring the nth group matches from the whole
 
102
   * string. The string argument
 
103
   * must be the same string passed to [string_match] or the search
 
104
   * functions, and the result argument must be the corresponding
 
105
   * result.
 
106
   *)
 
107
 
 
108
val group_beginning : result -> int -> int
 
109
  (** Returns the position where the substring matching the nth
 
110
   * group begins 
 
111
   *)
 
112
 
 
113
val group_end : result -> int -> int
 
114
  (** Returns the position where the substring matching the nth
 
115
   * group ends 
 
116
   *)
 
117
 
 
118
 
 
119
val global_replace: regexp -> string -> string -> string
 
120
  (** [global_replace re templ s]: Replaces all matchings of [re] in
 
121
   * [s] by [templ].
 
122
   *
 
123
   * In [templ] one can refer to matched groups by the backslash notation:
 
124
   * [\1] refers to the first group, [\2] to the second etc.
 
125
   * [\0] is the whole match. [\\ ] is the backslash character.
 
126
   *)
 
127
 
 
128
val replace_first: regexp -> string -> string -> string
 
129
  (** [replace_first re templ s]: Replaces the first match of [re] in
 
130
   * [s] by [templ].
 
131
   *
 
132
   * In [templ] one can refer to matched groups by the backslash notation:
 
133
   * [\1] refers to the first group, [\2] to the second etc.
 
134
   * [\0] is the whole match. [\\ ] is the backslash character.
 
135
   *)
 
136
 
 
137
val global_substitute:
 
138
       regexp -> (result -> string -> string) -> string -> string
 
139
  (** [global_substitute re subst s]: Applies the substitution function
 
140
   * [subst] to all matchings of [re] in [s], and returns the 
 
141
   * transformed string. [subst] is called with the current [result]
 
142
   * of the match and the whole string [s].
 
143
   *)
 
144
 
 
145
val substitute_first:
 
146
       regexp -> (result -> string -> string) -> string -> string
 
147
  (** [substitute_first re subst s]: Applies the substitution function
 
148
   * [subst] to the first matching of [re] in [s], and returns the 
 
149
   * transformed string. [subst] is called with the current [result]
 
150
   * of the match and the whole string [s].
 
151
   *)
 
152
 
 
153
 
 
154
(* replace_matched: not available *)
 
155
 
 
156
val split: regexp -> string -> string list
 
157
  (** Splits the string according to the regexp in substrings.
 
158
   * Occurrences of the delimiter at the beginning and the end
 
159
   * are ignored.
 
160
   *)
 
161
 
 
162
val bounded_split: regexp -> string -> int -> string list
 
163
  (** Splits into at most [n] substrings, based on [split] *)
 
164
val split_delim: regexp -> string -> string list
 
165
  (** Same as [split], but occurrences of the delimiter at the beginning 
 
166
   * and the end are returned as empty strings
 
167
   *)
 
168
val bounded_split_delim: regexp -> string -> int -> string list
 
169
  (** Splits into at most [n] substrings, based on [split_delim] *)
 
170
val full_split: regexp -> string -> split_result list
 
171
  (** Like [split_delim], but returns the delimiters in the result *)
 
172
val bounded_full_split: regexp -> string -> int -> split_result list
 
173
  (** Splits into at most [n] substrings, based on [full_split] *)
 
174
 
 
175
val string_before: string -> int -> string
 
176
  (** The first [n] characters of a string *)
 
177
val string_after: string -> int -> string
 
178
  (** The last [n] characters of a string *)
 
179
val first_chars: string -> int -> string
 
180
  (** Same as [string_before] *)
 
181
val last_chars: string -> int -> string
 
182
  (** Same as [string_after] *)
 
183