Skip to content

Commit

Permalink
fix(compiler): Properly encode hex values in Bytes literals (#2088)
Browse files Browse the repository at this point in the history
  • Loading branch information
ospencer committed Apr 6, 2024
1 parent 8d157f3 commit 263993d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
4 changes: 3 additions & 1 deletion compiler/src/utils/literals.re
Original file line number Diff line number Diff line change
Expand Up @@ -385,8 +385,10 @@ let add_code_point = (buf, str, unicode) => {
| "x" => Scanf.sscanf(numstr, "%x", x => x)
| _ => Scanf.sscanf(esc ++ numstr, "%o", x => x)
};
if (Uchar.is_valid(code_point)) {
if (unicode && Uchar.is_valid(code_point)) {
Buffer.add_utf_8_uchar(buf, Uchar.of_int(code_point));
} else if (!unicode) {
Buffer.add_uint8(buf, code_point);
} else {
raise(IllegalUnicodeCodePoint(str));
};
Expand Down
5 changes: 5 additions & 0 deletions compiler/test/suites/strings.re
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,11 @@ bar", 1))|},

// Bytes literals
assertRun("bytes_literal", {|print(b"abc")|}, "<bytes: 61 62 63>\n");
assertRun(
"bytes_literal_hex",
{|print(b"\xc3\x81\x24\x24\x00\x24\x99\xc3")|},
"<bytes: c3 81 24 24 00 24 99 c3>\n",
);
assertRun(
"bytes_literal_long",
{|print(b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefg")|},
Expand Down

0 comments on commit 263993d

Please sign in to comment.