summaryrefslogtreecommitdiffstats
path: root/stdlib/string.ml
diff options
context:
space:
mode:
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