summaryrefslogtreecommitdiffstats
path: root/stdlib/string.ml
diff options
context:
space:
mode:
authorDamien Doligez <damien.doligez-inria.fr>2012-03-08 19:52:03 +0000
committerDamien Doligez <damien.doligez-inria.fr>2012-03-08 19:52:03 +0000
commit6c24f4f90b23e8c4536281d31461adfe5a15b739 (patch)
tree29f6c4af8052800cc7d0eafb9650c6be8e90a2e5 /stdlib/string.ml
parent1fb4007ece64b1d59e16d7a84639fce1dd69ed45 (diff)
merge version 3.12 from 3.12.1 to r12205
git-svn-id: http://caml.inria.fr/svn/ocaml/trunk@12210 f963ae5c-01c2-4b8c-9fe0-0dff7051ff02
Diffstat (limited to 'stdlib/string.ml')
-rw-r--r--stdlib/string.ml21
1 files changed, 21 insertions, 0 deletions
diff --git a/stdlib/string.ml b/stdlib/string.ml
index 7eafec02f..f3906f353 100644
--- a/stdlib/string.ml
+++ b/stdlib/string.ml
@@ -85,6 +85,27 @@ external is_printable: char -> bool = "caml_is_printable"
external char_code: char -> int = "%identity"
external char_chr: int -> char = "%identity"
+let is_space = function
+ | ' ' | '\012' | '\n' | '\r' | '\t' -> true
+ | _ -> false
+
+let trim s =
+ let len = length s in
+ let i = ref 0 in
+ while !i < len && is_space (unsafe_get s !i) do
+ incr i
+ done;
+ let j = ref (len - 1) in
+ while !j >= !i && is_space (unsafe_get s !j) do
+ decr j
+ done;
+ if !i = 0 && !j = len - 1 then
+ s
+ else if !j >= !i then
+ sub s !i (!j - !i + 1)
+ else
+ ""
+
let escaped s =
let n = ref 0 in
for i = 0 to length s - 1 do