|
| 1 | +(* OCamlScript compiler |
| 2 | + * Copyright (C) 2015-2016 Bloomberg Finance L.P. |
| 3 | + * |
| 4 | + * This program is free software; you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU Lesser General Public License as published by |
| 6 | + * the Free Software Foundation, with linking exception; |
| 7 | + * either version 2.1 of the License, or (at your option) any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU Lesser General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU Lesser General Public License |
| 15 | + * along with this program; if not, write to the Free Software |
| 16 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | + *) |
| 18 | + |
| 19 | +(* Author: Hongbo Zhang *) |
| 20 | + |
| 21 | + |
| 22 | +external string_unsafe_set : string -> int -> char -> unit |
| 23 | + = "%string_unsafe_set" |
| 24 | + |
| 25 | +external string_create: int -> string = "caml_create_string" |
| 26 | + |
| 27 | +external unsafe_chr: int -> char = "%identity" |
| 28 | + |
| 29 | +(** {!Char.escaped} is locale sensitive in 4.02.3, fixed in the trunk, |
| 30 | + backport it here |
| 31 | + *) |
| 32 | +let escaped = function |
| 33 | + | '\'' -> "\\'" |
| 34 | + | '\\' -> "\\\\" |
| 35 | + | '\n' -> "\\n" |
| 36 | + | '\t' -> "\\t" |
| 37 | + | '\r' -> "\\r" |
| 38 | + | '\b' -> "\\b" |
| 39 | + | ' ' .. '~' as c -> |
| 40 | + let s = string_create 1 in |
| 41 | + string_unsafe_set s 0 c; |
| 42 | + s |
| 43 | + | c -> |
| 44 | + let n = Char.code c in |
| 45 | + let s = string_create 4 in |
| 46 | + string_unsafe_set s 0 '\\'; |
| 47 | + string_unsafe_set s 1 (unsafe_chr (48 + n / 100)); |
| 48 | + string_unsafe_set s 2 (unsafe_chr (48 + (n / 10) mod 10)); |
| 49 | + string_unsafe_set s 3 (unsafe_chr (48 + n mod 10)); |
| 50 | + s |
0 commit comments