From: Stefano Z. <za...@bo...> - 2004-01-11 21:31:47
|
Just a suggestion for an useful function that IMO is missing in the String module: String.unescaped. This function should be the inverse function of String.escaped, it seems to me really stupid to have an escaping function but not its counterpart! I'm also puzzled by the name "escaped", why not "escape"? Mysteries of the ocaml standard libraray ... IMO is a good idea also to add the two aliases String.{escape,unescape}. On the implementation side I've used a dirty trick: the Genlex module. String.escaped claims to follow ocaml lexing conventions and Genlex claims the same. If its true, the implementation of String.unescaped could be: let unescape = let lexer = lazy (Genlex.make_lexer []) in fun s -> let tok_stream = Lazy.force lexer (Stream.of_string ("\"" ^ s ^ "\"")) in match Stream.peek tok_stream with | Some (Genlex.String s) -> s | _ -> assert false The only drawback of this implementation is that Genlex doesn't raises exception for the cases in which the compiler outputs warnings. The string "a\b" will be unescaped to "ab" ignoring the invalid "\". This is not a problem if you unescape string produced by escape but it can be a problem in other cases. Cheers. -- ^Stefano Zacchiroli -- Master in Computer Science @ Uni. Bologna, Italy$ ^zack@{cs.unibo.it,debian.org,bononia.it} -- http://www.bononia.it/zack$ ^Frequentando il mio maestro mi ero reso conto [.] che la logica poteva$ ^servire a molto a condizione di entrarci dentro e poi di uscirne -Adso$ |