Open UnixJunkie opened 12 years ago
I think there is even a possible problem in case ~prfx contains some regexp special characters. The following might be preferable:
(* does the string starts with prefix ? *) let starts_with ~str ~prfx = Str.string_match (Str.regexp_string prfx) str 0
I am in some other code using this same function, that's why I notice problems with it (like swapping its parameters inadvertantly or using mistakenly some special charcters in the ~prfx while you did not intended them to be compiled in the regexp but matched as-is).
On 11/6/2012 1:52 AM, Francois Berenger wrote:
I think there is even a possible problem in case ~prfx contains some regexp special characters. The following might be preferable:
(* does the string starts with prefix ? *) let starts_with ~str ~prfx = Str.string_match (Str.regexp_string prfx) str 0
— Reply to this email directly or view it on GitHub https://github.com/thelema/odb/issues/97#issuecomment-10100875.
I think this might do unanchored matching; searching for
prfx
anywhere instr
. A correct solution is to just do a string compare against the correct length prefix of str:
let starts_width ~str ~prefix = if String.length str < String.length prefix then false else String.sub str 0 (String.length prefix) = prefix
E.
No, the matched substring must start at index 0 in the code I gave. That's correct and I tried it extensively in a toplevel. I'll send a pull request.
The documentation of the Str module is not that crystal clear.
It should be like this to be less error prone at use:
(* does the string starts with prefix ? *) let starts_with ~str ~prfx = Str.string_match (Str.regexp ("^" ^ prfx)) str 0