Merge branch 'master' of vapour:hop

This commit is contained in:
Tony Garnock-Jones 2012-05-25 19:43:21 +01:00
commit 7fbe8b9109
1 changed files with 32 additions and 7 deletions

View File

@ -38,24 +38,49 @@ let str_to_string s =
let rec to_string j =
match j with
| Num f ->
| Num f ->
if float_of_int (int_of_float f) = f
then string_of_int (int_of_float f)
else string_of_float f
| Str s ->
| Str s ->
str_to_string s
| Arr js ->
| Arr js ->
"[" ^ String.concat "," (List.map to_string js) ^ "]"
| Rec kvs ->
| Rec kvs ->
"{" ^ String.concat "," (List.map kv_to_string kvs) ^ "}"
| Flg b ->
| Flg b ->
if b then "true" else "false"
| Nil ->
| Nil ->
"null"
and kv_to_string (k, v) =
str_to_string k ^ ":" ^ to_string v
let rec pretty_string ?indent:(indent=2) j =
let rec walk current j =
match j with
| Num f ->
if float_of_int (int_of_float f) = f
then string_of_int (int_of_float f)
else string_of_float f
| Str s ->
str_to_string s
| Arr js ->
"[" ^ String.concat ", " (List.map (walk current) js) ^ "]"
| Rec [] ->
"{}"
| Rec [(k, v)] ->
"{" ^ str_to_string k ^ ": " ^ walk current v ^ "}"
| Rec kvs ->
"{" ^ String.concat "," (List.map (kv_walk (current + indent)) kvs) ^
"\n" ^ String.make current ' ' ^ "}"
| Flg b ->
if b then "true" else "false"
| Nil ->
"null"
and kv_walk current (k, v) =
"\n" ^ String.make current ' ' ^ str_to_string k ^ ": " ^ walk current v
in walk 0 j
let accumulate_utf8 codepoint (acc, len) =
(* Of course, at the moment, the codepoint is limited to 16 bits... *)
if codepoint < 0x80 then