X-Git-Url: http://git.annexia.org/?a=blobdiff_plain;f=src%2Futils.ml;h=50adfb42ab72d555e799777083b6f51ad9fb6269;hb=881b2e9b7bad0da8f44418e9e6558710db5ce690;hp=d1b0efac4e56e1e0e7e52b57d7e7424d3c8f10c8;hpb=6afdc65fcdb592dccb751849f65b1f482ef97cd6;p=goals.git diff --git a/src/utils.ml b/src/utils.ml index d1b0efa..50adfb4 100644 --- a/src/utils.ml +++ b/src/utils.ml @@ -58,6 +58,30 @@ let rec string_find s sub = in loop 0 +let rec split sep str = + let len = String.length sep in + let seplen = String.length str in + let i = string_find str sep in + if i = -1 then str, "" + else ( + String.sub str 0 i, String.sub str (i + len) (seplen - i - len) + ) + +and nsplit ?(max = 0) sep str = + if max < 0 then + invalid_arg "String.nsplit: max parameter should not be negative"; + + (* If we reached the limit, OR if the pattern does not match the string + * at all, return the rest of the string as a single element list. + *) + if max = 1 || string_find str sep = -1 then + [str] + else ( + let s1, s2 = split sep str in + let max = if max = 0 then 0 else max - 1 in + s1 :: nsplit ~max sep s2 + ) + let isspace c = c = ' ' (* || c = '\f' *) || c = '\n' || c = '\r' || c = '\t' (* || c = '\v' *)