Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion src/ir/text.ml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,26 @@ type indice =

let pp_id fmt id = pf fmt "$%s" id

let pp_name_inner fmt s =
let pp_hex_char fmt c = pf fmt "\\%02x" (Char.code c) in
let pp_char fmt = function
| '\n' -> string fmt "\\n"
| '\r' -> string fmt "\\r"
| '\t' -> string fmt "\\t"
| '\'' -> string fmt "\\'"
| '"' -> string fmt "\\\""
| '\\' -> string fmt "\\\\"
| '\x20' .. '\x7e' as c -> char fmt c
| c -> pp_hex_char fmt c
in
let pp_unicode_char fmt = function
| ('\t' | '\n' | '\x20' .. '\x7e') as c -> pp_char fmt c
| c -> pf fmt "\\u{%02x}" (Char.code c)
in
String.iter (pp_unicode_char fmt) s

let pp_name fmt s = pf fmt {|"%a"|} pp_name_inner s

let pp_id_opt fmt = function None -> () | Some i -> pf fmt " %a" pp_id i

let pp_indice fmt = function Raw u -> int fmt u | Text i -> pp_id fmt i
Expand Down Expand Up @@ -761,7 +781,7 @@ module Data = struct
}

let pp fmt (d : t) =
pf fmt {|(data%a %a %S)|} pp_id_opt d.id Mode.pp d.mode d.init
pf fmt {|(data%a %a %a)|} pp_id_opt d.id Mode.pp d.mode pp_name d.init
end

module Tag = struct
Expand Down
4 changes: 4 additions & 0 deletions test/fmt/data_bytes.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
(module
(memory 1)
(data (i32.const 0) "Hello\00World\01\02\03\ff")
)
8 changes: 8 additions & 0 deletions test/fmt/data_roundtrip.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
test data special chars round-trip:
$ owi fmt data_special_chars.wat > ./owi_test_output.wat
$ owi fmt ./owi_test_output.wat
(module
(memory 1)
(data (memory 0) (offset i32.const 0) "hello\n\t\u{0d}\"\'\\world")
)
$ rm ./owi_test_output.wat
4 changes: 4 additions & 0 deletions test/fmt/data_special_chars.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
(module
(memory 1)
(data (i32.const 0) "hello\n\t\r\"'\\world")
)
2 changes: 2 additions & 0 deletions test/fmt/dune
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,7 @@
m.wat
locals.wat
locals_drop.wat
data_special_chars.wat
data_bytes.wat
script.wast
script.t))
12 changes: 12 additions & 0 deletions test/fmt/print.t
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,15 @@ print simplified:
)
(start 1)
)
print data with special chars:
$ owi fmt data_special_chars.wat
(module
(memory 1)
(data (memory 0) (offset i32.const 0) "hello\n\t\u{0d}\"\'\\world")
)
print data with raw bytes:
$ owi fmt data_bytes.wat
(module
(memory 1)
(data (memory 0) (offset i32.const 0) "Hello\u{00}World\u{01}\u{02}\u{03}\u{ff}")
)
Loading