Skip to content
This repository
Newer
Older
100644 485 lines (390 sloc) 14.951 kb
fccc6851 » MLstate
2011-06-21 Initial open-source release
1 (*
2 Copyright © 2011 MLstate
3
4 This file is part of OPA.
5
6 OPA is free software: you can redistribute it and/or modify it under the
7 terms of the GNU Affero General Public License, version 3, as published by
8 the Free Software Foundation.
9
10 OPA is distributed in the hope that it will be useful, but WITHOUT ANY
11 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
13 more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with OPA. If not, see <http://www.gnu.org/licenses/>.
17 *)
18 (** {4 From OCaml Standard Library} *)
19 (**
20 This module includes the standard one
21 @see <http://caml.inria.fr/pub/docs/manual-ocaml/libref/String.html> Ocaml Manual
22 *)
23 (** *)
24 external length : string -> int = "%string_length"
25 external get : string -> int -> char = "%string_safe_get"
26 external set : string -> int -> char -> unit = "%string_safe_set"
27 val make : int -> char -> string
28 val copy : string -> string
29 val sub : string -> int -> int -> string
30 val fill : string -> int -> int -> char -> unit
31 val blit : string -> int -> string -> int -> int -> unit
32 val concat : string -> string list -> string
33 val iter : (char -> unit) -> string -> unit
34 val escaped : string -> string
35 val index : string -> char -> int
36 val rindex : string -> char -> int
37 val index_from : string -> int -> char -> int
38 val rindex_from : string -> int -> char -> int
39 val contains : string -> char -> bool
40 val contains_from : string -> int -> char -> bool
41 val rcontains_from : string -> int -> char -> bool
42 val uppercase : string -> string
43 val lowercase : string -> string
44 val capitalize : string -> string
45 val uncapitalize : string -> string
46 type t = string
47 val compare : t -> t -> int
48 external unsafe_get : string -> int -> char = "%string_unsafe_get"
49 external unsafe_set : string -> int -> char -> unit
50 = "%string_unsafe_set"
51 external unsafe_blit : string -> int -> string -> int -> int -> unit
52 = "caml_blit_string" "noalloc"
53 external unsafe_fill : string -> int -> int -> char -> unit
54 = "caml_fill_string" "noalloc"
55 val create : int -> string
56
57 (** {4 MLstate String} *)
58
59 (**
60 Classification (depending on the kind of returned type) :
61
62 + Allocation (returns a string)
63 + Iterators (e.g. fold)
64 + Condition (returns a bool)
65 + Search
66 + Decons
67 + IO
68 + Conversion and escaping (string -> string)
69 *)
70
71 (** {6 Allocation (returns a string)} *)
72
73 (**
74 [insert "insert in a string" 6 " something"]
75 returns ["insert something in a string"]
76 @raise Invalid_argument if [ofs > len]
77 *)
78 val insert : string -> int -> string -> string
79 val unsafe_sub : string -> int -> int -> string
80
81 (** Build a string of size [n] with a function. If [n<0], fill it in reverse order *)
82 val init : int -> (int -> char) -> string
83
84 (** Like init but with an [acc]. rev_fold_init if [n<0]
85 TODO: reverse ordre [acc * 'a] *)
86 val fold_init : int -> ('a -> char * 'a) -> 'a -> string
87 (* val rev_fold_init *)
88 val of_chars : char list -> string
89 val map : (char -> char) -> string -> string
90 (* val rev_map *)
91
92 (**
978b7c43 » akoprow
2011-07-09 [fix] typo: occurence->occurrence, occured->occurred
93 [multi_replace s (pat * repl) list] replace in [s] all occurrences of
fccc6851 » MLstate
2011-06-21 Initial open-source release
94 each [pat] by its corresponding [repl]. Returns an error if a [pat]
95 is exactly the beginning of another one (e.g: 'to' and 'toto'). If a
96 pattern is included elseway in another way, the longer will be applyed
97 in case of collision.
98 E.g; multi_replace "todo domino" ["todo","bibi"; "do","trio"]
99 >> "bibi triomino"
100 Implemented using [Buffer]. Returns a fresh string.
101 *)
102 val multi_replace : string -> (string * string) list -> string
103
104 (**
978b7c43 » akoprow
2011-07-09 [fix] typo: occurence->occurrence, occured->occurred
105 [replace s pat repl] replace in [s] all occurrence of [pat] by [repl].
fccc6851 » MLstate
2011-06-21 Initial open-source release
106 Implemented using multi_replace. Returns a fresh string.
107 *)
108 val replace : string -> string -> string -> string
109
110 (**
111 [citation sep length s] will return a list of maxlength [max length sep].
112 If the string is shorter than len, return the string [s], if not, return
113 a fresh string beginning and ending like [s] but with in the middle the
114 [sep] string.
115 {[
116 String.citation "[...]" 10 "this is a much too long string"
117 ]}
118 returns something like
119 {[
120 "this [...] string"
121 ]}
122 *)
123 val citation : string -> int -> string -> string
124
125 (** retuns a string of [len] containing random char from ['a'-'z'] *)
126 val random : int -> string
127
128 (**
129 [ltrim is_space " this has been ltrimed "]
130 returns
131 ["this string has been ltrimed "]
132 *)
133 val ltrim : ?is_space:(char -> bool) -> string -> string
134
135 (**
136 [rtrim is_space " this has been rtrimed "]
137 returns
138 [" this has been rtrimed"]
139 *)
140 val rtrim : ?is_space:(char -> bool) -> string -> string
141
142 (**
143 [trim is_space " this has been trimed "]
144 returns
145 ["this has been trimed"].
146 More effecient than (ltrim (rtrim s))
147 The default value for [is_space] is [Char.is_space]
148 *)
149 val trim : ?is_space:(char -> bool) -> string -> string
150
151 (**
152 Remove the quote of a string if it does have some.
153 This is done just once, this is not a [trim is_quote].
154 + [strip_quotes "\"toto\""] returns ["toto"]
155 + [strip_quotes "\"\"toto\"\""] returns ["\"toto\""]
156 *)
157 val strip_quotes : string -> string
158
159 (** [repeat "bala" 2] returns ["balabala"] *)
160 val repeat : string -> int -> string
161
162 (**
163 Returns the left part of a string.
164 Count from end if [i < 0], like in many languages.
165 + [left "hello" 2] returns ["he"]
166 + [left "hello" (-2)] returns ["hel"]
167
168 @raise Invalid_argument if [i] is not between [-len] and [len] included *)
169 val left : string -> int -> string
170
171 (**
172 Returns the right part of a string.
173 Count from begin if [i < 0], like in many languages.
174 + [left "hello" 2] returns ["lo"]
175 + [left "hello" (-2)] returns ["llo"]
176
177 @raise Invalid_argument if [i] is not between [-len] and [len] included *)
178 val right : string -> int -> string
179
180 (**
181 Returns the left part of a string until a char is found.
182 [left_at 'l' "hello"] returns ["he"].
183
184 This is identity if the char is not found.
185 *)
186 val left_at : string -> char -> string
187
188 (**
189 Returns the right part of a string until a char is found.
190 [right_at 'e' "hello"] returns ["llo"].
191
192 This is identity if the char is not found.
193 *)
194 val right_at : string -> char -> string
195
196 (** Removes trailing \n and \r at the end starting from the end of the string until an other char is detected *)
197 val remove_trail : string -> string
198
199 (**
200 [remove_prefix "foo" "foobar"] returns ["bar"]
201 @raise Not_found if the string does not have such prefix
202 *)
203 val remove_prefix : string -> string -> string
204
205 (** just like [remove_prefix], but returns the original string when
206 instead of raising not found
207 *)
208 val remove_prefix_if_possible : string -> string -> string
209
210 (**
211 [remove_suffix suffix string]
212 Same behaviour as [remove_prefix]
213 *)
214 val remove_suffix : string -> string -> string
215
216 (**
217 Same behaviour as [remove_prefix_if_possible]
218 *)
219 val remove_suffix_if_possible : string -> string -> string
220
221 (**
222 Completes a string to [n] chars using [c], or strip if [length s > n].
223 + [complete_left 8 'A' "babo"] returns ["AAAAbabo"]
224 + [complete_left 2 'A' "babo"] returns ["bo"]
225
226 <!> if [length s = i], this is identity (no copy)
227
228 @raise Invalid_argument if [n < 0]
229 *)
230 val complete_left : int -> char -> string -> string
231
232 (**
233 Completes a string to n chars using c, or strip if length s > n.
234 + [complete_right 8 'A' "babo"] returns ["baboAAAA"]
235 + [complete_right 2 'A' "babo"] returns ["ba"]
236
237 <!> if [length s = i], [s] is returned (no copy)
238
239 @raise Invalid_argument if [n < 0]
240 *)
241 val complete_right : int -> char -> string -> string
242
243 (** [complete] is an alias for [complete_right] *)
244 val complete : int -> char -> string -> string
245
246 (**
247 Like [String.concat] but with more flexibility
248 [sconcat ~left ~right ?nil separator elts].
249
250 1 allocation, 2 iterations on the list.
251 *)
252 val sconcat :
253 ?left:string ->
254 ?right:string -> ?nil:string -> string -> string list -> string
255
256 (**
257 Like [sconcat] combined with [List.rev] but more optimized.
258
259 1 allocation, 2 iterations on the list.
260 *)
261 val rev_sconcat :
262 ?left:string ->
263 ?right:string -> ?nil:string -> string -> string list -> string
264
265 (**
266 Like [sconcat] but with an extra [map] function.
267
268 1 allocation, 1 revmap, 2 iterations on the list (can be optimized to 1-1, next commit)
269 *)
270 val concat_map :
271 ?left:string ->
272 ?right:string ->
273 ?nil:string -> string -> ('a -> string) -> 'a list -> string
274
275 (**
276 Like [concat_map] combined with [List.rev] but more optimized.
277
278 1 allocation, 1 revmap, 2 iterations on the list (can be reduced to 1-1) *)
279 val rev_concat_map :
280 ?left:string ->
281 ?right:string ->
282 ?nil:string -> string -> ('a -> string) -> 'a list -> string
283
284 (** {6 Iterators} *)
285 (** *)
286 val fold : ('a -> char -> 'a) -> 'a -> string -> 'a
287 val rev_fold : ('a -> char -> 'a) -> 'a -> string -> 'a
288 val exists : (char -> bool) -> string -> bool
289 val for_all : (char -> bool) -> string -> bool
290
291 (** {6 Condition (returns a bool) } *)
292 (** *)
293 val equal_insensitive : string -> string -> bool
294 val compare_insensitive : string -> string -> int
295
296 (**
297 Check that each char is in ['a'-'z'|'A'-'Z'|'0'-'9'|'_']
298 @see "Char.is_alpha" (same test but ['_'])
299 *)
300 val is_word : string -> bool
301
302 (**
303 Check if each char is in ['0'-'9'].
304 The implementation returns [false] on negative int.
305 *)
306 val is_int : string -> bool
307
308 (**
309 All char are digits, with one optional dot.
310 + implies than [Pervasives.float_of_string] does not fail.
311 + is_int implies is_float
312 + including e.g. [".1"]
313
314 The implementation returns [false] on negative float.
315 *)
316 val is_float : string -> bool
317
318 (**
56308976 » François-Régis Sinot
2011-09-30 [feature] stdlib: more tolerant naming of files
319 Tell if a string is a valid universal identifier, working on most of languages.
320 This means that it uses only alphanumeric char, and [_] but does not start with
321 a numeric char
322 *)
323 val is_universal_ident : string -> bool
324
325 (**
fccc6851 » MLstate
2011-06-21 Initial open-source release
326 [is_substring "lo" "hello" 3] returns [true]
327
328 Does not fail if the prefix or the offset is longuer than the string, simply returns [false].
329
330 @raise Invalid_argument if [ofs < 0]
331 *)
332 val is_substring : string -> string -> int -> bool
333
334 (** Like [is_substring] but with a custom comparaison between char *)
335 val is_substring_compare : (char -> char -> int) -> string -> string -> int -> bool
336
337 (** Like [is_substring] but not case sensitive *)
338 val is_substring_insensitive : string -> string -> int -> bool
339
340 (** [is_contained "lo" "hello"] returns [true].
341
342 Implemented from right to left. Use [is_contained_from] or
343 [is_contained_until] if you need to be more specific.
344 *)
345 val is_contained : string -> string -> bool
346
347 (**
348 [is_contained_from pat s start]
349 right to left implemented.
350 The [start] ofset is the min index where the patern can start in s.
351 The function return the index where the [pat] starts in [s].
352
353 If [start < 0], the function ignore [start] and take [0] as [start]
354 *)
355 val is_contained_from : string -> string -> int -> int option
356
357 (**
358 [is_contained_from_until pat s start stop]
359 right to left implemented.
360 The [start] ofset is the min index where the patern can start, the
361 [stop] the index where [pat] should end before.
362 The function return the index where the [pat] starts in [s].
363
364 If [start < 0], the function ignore [start] and take [0] as [start].
365 If [stop < 0] returns [None]
366 *)
367 val is_contained_from_until : string -> string -> int -> int -> int option
368
369 (** [is_prefix "youpla" "youplaboum"] returns [true]
370
371 Does not fail if the prefix is longuer than the string, simply returns [false] *)
372 val is_prefix : string -> string -> bool
373
374 (** [is_suffix "boum" "youplaboum"] returns [true]
375
376 Does not fail if the suffix is longuer than the string, simply returns [false] *)
377 val is_suffix : string -> string -> bool
378
379 (** {6 Search} *)
380
381 (** Find the first char [c] satisfying a predicate and returns this char *)
382 val find_opt : (char -> bool) -> string -> char option
383
384 (**
385 Find the first char [c] satisfying a predicate and
386 returns the index in the string of [c]
387 *)
388 val findif : (char -> bool) -> string -> int option
389 val findi : char -> string -> int option
390
391 val rev_findif : (char -> bool) -> string -> int option
392 val rev_findi : char -> string -> int option
393
394 (** {6 Decons} *)
395
396 (**
397 Like the unix command line.
398 [tail ?(lines=10) text] returns the [lines] last lines of [text]
399 *)
400 val tail : ?lines:int -> string -> string
401
402 (** @raise Invalid_argument [last_char ""] *)
403 val last_char : string -> char
404
405 (**
406 [len_from p s ofs]
407 returns the length of the bigger prefix [p] of [s] starting at [ofs]
408 such that all char of [p] satisfy a predicate [f].
409 *)
410 val len_from : (char -> bool) -> string -> int -> int
411
412 (**
413 Same than [Hashtbl.hash]
414 *)
415 val hash : string -> int
416
417 val char_list_of_string : string -> char list
418 val rev_char_list_of_string : string -> char list
419
420
421 (** {6 Splitting} *)
422
423 (**
978b7c43 » akoprow
2011-07-09 [fix] typo: occurence->occurrence, occured->occurred
424 cuts a string at the occurrences of the given character. The separation
461365b0 » Louis Gesbert
2011-06-23 [cleanup] Base.String: changed String.split to a much simpler String.…
425 character is removed. Empty strings are'nt returned (that is,
426 [slice '/' "/path//x/"] returns [["path";"x"]]). Non tail-recursive.
fccc6851 » MLstate
2011-06-21 Initial open-source release
427 *)
461365b0 » Louis Gesbert
2011-06-23 [cleanup] Base.String: changed String.split to a much simpler String.…
428 val slice : char -> string -> string list
fccc6851 » MLstate
2011-06-21 Initial open-source release
429
430 (**
461365b0 » Louis Gesbert
2011-06-23 [cleanup] Base.String: changed String.split to a much simpler String.…
431 same as slice, but cuts at every character present in the string given as
432 first parameter.
fccc6851 » MLstate
2011-06-21 Initial open-source release
433 *)
461365b0 » Louis Gesbert
2011-06-23 [cleanup] Base.String: changed String.split to a much simpler String.…
434 val slice_chars : string -> string -> string list
fccc6851 » MLstate
2011-06-21 Initial open-source release
435
436 (**
978b7c43 » akoprow
2011-07-09 [fix] typo: occurence->occurrence, occured->occurred
437 split a string in two substrings, at the first occurrence of [char], which is removed.
461365b0 » Louis Gesbert
2011-06-23 [cleanup] Base.String: changed String.split to a much simpler String.…
438 If [char] doesn't appear in [string], returns [(string,"")]
fccc6851 » MLstate
2011-06-21 Initial open-source release
439 *)
440 val split_char : char -> string -> string * string
441
442 (**
978b7c43 » akoprow
2011-07-09 [fix] typo: occurence->occurrence, occured->occurred
443 split a string in two substrings, at the last occurrence of [char], which is removed.
461365b0 » Louis Gesbert
2011-06-23 [cleanup] Base.String: changed String.split to a much simpler String.…
444 If [char] doesn't appear in [string], returns [(string,"")]
fccc6851 » MLstate
2011-06-21 Initial open-source release
445 *)
446 val split_char_last : char -> string -> string * string
447
448
449 (** {6 UNDOCUMENTED : use it at your own risk} *)
450 (** TODO: remove from String *)
451 (** *)
452 val width : string -> int
453 val limit : int -> string -> string
454 val limit_width : int -> string -> string
455 val name_of_int : int -> string
456 val name_of_int_upper : int -> string
457 val remove_first_char : string -> string
458 val remove_accents : string -> string
459
460 val delete_trailing_whitespace : string -> string
461 val to_hex : string -> string
462 val from_hex : string -> string
463
464 (** {6 Conversion and escaping} *)
465
466 val base64encode : string -> string
467
468 val base64decode : string -> string
469 (** @raise Invalid_argument if the argument is not a valid base64 encoded string *)
470
471 (**
472 [escape ~valid_chars ~escape_chars str] returns a string
473 containing only chars that are valid ([String.for_all valid_chars]
474 holds on the output).
475 The output string may be empty.
476 [escape] is injective, ie, it never creates collision and it does not
477 depend on any global state (ie calling it with the same input always
478 gives the same output).
479
480 @precond not (valid_chars escape_char)
481 @precond 0-9 and a-f should be valid_chars
482 *)
483 val escape : valid_chars:(char -> bool) -> escape_char:char -> string -> string
484
485 val equal : string -> string -> bool
Something went wrong with that request. Please try again.