Add binary field to deepcopy test
[hiphop-php.git] / hphp / hack / src / utils / php_escaping.ml
blob22a89abbe0ed0fc06ce4676b85a0199c84b533bf
1 (*
2 * Copyright (c) 2015, Facebook, Inc.
3 * All rights reserved.
5 * This source code is licensed under the MIT license found in the
6 * LICENSE file in the "hack" directory of this source tree.
8 *)
10 (* Implementation of string escaping stuff. Ugggggggh.
11 * See http://php.net/manual/en/language.types.string.php *)
13 open Hh_prelude
15 let is_printable c =
16 let open Char in
17 c >= ' ' && c <= '~'
19 let is_lit_printable c =
20 let open Char in
21 is_printable c && c <> '\\' && c <> '\"'
23 let escape_char = function
24 | '\n' -> "\\n"
25 | '\r' -> "\\r"
26 | '\t' -> "\\t"
27 | '\\' -> "\\\\"
28 | '"' -> "\\\""
29 | '$' -> "$"
30 | c when is_lit_printable c -> String.make 1 c
31 | c -> Printf.sprintf "\\%03o" (Char.to_int c)
33 let escape ?(f = escape_char) s =
34 let buf = Buffer.create (String.length s) in
35 Stdlib.String.iter (fun c -> Buffer.add_string buf @@ f c) s;
36 Buffer.contents buf