Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
24 changes: 23 additions & 1 deletion src/ir/text.ml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,28 @@ 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 "\\\\"
| c ->
let ci = Char.code c in
if 0x20 <= ci && ci < 0x7f then char fmt c else pp_hex_char fmt c
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be rewritten as:

| '\x20' .. '\x7e' as c -> char fmt c
| c -> pp_hex_char fmt c 

in
let pp_unicode_char fmt = function
| (0x09 | 0x0a) as c -> pp_char fmt (Char.chr c)
| uc when 0x20 <= uc && uc < 0x7f -> pp_char fmt (Char.chr uc)
Copy link
Copy Markdown
Member

@redianthus redianthus Mar 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, you could use a character interval for the pattern (and it's likely not necessary to use integers for this function, using char should be enough)

| uc -> pf fmt "\\u{%02x}" uc
in
String.iter (fun c -> pp_unicode_char fmt (Char.code c)) 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 +783,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 > /tmp/owi_test_output.wat
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Putting the file in /tmp is likely going to cause various issues. Doing ... > ./owi_test_output.wat is fine (dune takes care of having the files in the right place).

$ owi fmt /tmp/owi_test_output.wat
(module
(memory 1)
(data (memory 0) (offset i32.const 0) "hello\n\t\u{0d}\"\'\\world")
)
$ rm /tmp/owi_test_output.wat
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to do this if you put it in the current directory

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