From 291b4cd82cebeed39d8c06c8208fc415dfa32a48 Mon Sep 17 00:00:00 2001 From: cheneym2 Date: Tue, 14 May 2024 14:05:58 -0400 Subject: Slang: Support UTF-8 with Byte Order Markers (#4135) Slang APIs are documented as taking UTF-8 encoded shader source, though it's not explicitly documented whether it is allowed to include a BOM (Byte Order Marker). This change adds support for UTF-8 BOM markers by virtue of disposing of BOM data. As a bonus, UTF-16 input which can cleanly decode to UTF-8 is now also accepted. Throwing out the BOM on input is done by leveraging existing functionality in "determineEncoding()", however a bug exists there for null-terminated single character input, where the null byte caused a heuristic to guess UTF-16, even though the null byte isn't part of the string. The bug in "determineEncoding" is fixed by only guessing when bytes >= 2 and not looking past the end of the buffer. The 'implicit-cast' test was mistakenly relying on the bug to pass, as its expected file was being read as UTF16 and cropped to zero length due to the bug. The expected output of implicit-cast is updated to pass with the bug fix in place. The decoding of UTF-16 to UTF-8 is done through an existing 'decode' method. This change fixes a bug in UTF16-LE 'decode' where it was decoded as if it were Big-Endian. Adds 3 small tests to ensure the compiler doesn't choke on source files in UTF-8 (with BOM), UTF16-LE, or UTF16-BE. Bonus: Fixes a bug in diagnostic reporting where hex values were incorrectly translated to text, leading to incorrect, possibly truncated strings. Fixes #4046 Co-authored-by: Yong He --- tests/bugs/implicit-cast.slang.expected.txt | 5 ++++- tests/preprocessor/utf16_be_bom_crlf.slang | Bin 0 -> 70 bytes tests/preprocessor/utf16_le_bom_crlf.slang | Bin 0 -> 70 bytes tests/preprocessor/utf8_bom_crlf.slang | 4 ++++ 4 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 tests/preprocessor/utf16_be_bom_crlf.slang create mode 100644 tests/preprocessor/utf16_le_bom_crlf.slang create mode 100644 tests/preprocessor/utf8_bom_crlf.slang (limited to 'tests') diff --git a/tests/bugs/implicit-cast.slang.expected.txt b/tests/bugs/implicit-cast.slang.expected.txt index d8263ee98..5a4d7b6ab 100644 --- a/tests/bugs/implicit-cast.slang.expected.txt +++ b/tests/bugs/implicit-cast.slang.expected.txt @@ -1 +1,4 @@ -2 \ No newline at end of file +0 +1 +0 +0 diff --git a/tests/preprocessor/utf16_be_bom_crlf.slang b/tests/preprocessor/utf16_be_bom_crlf.slang new file mode 100644 index 000000000..eba197eff Binary files /dev/null and b/tests/preprocessor/utf16_be_bom_crlf.slang differ diff --git a/tests/preprocessor/utf16_le_bom_crlf.slang b/tests/preprocessor/utf16_le_bom_crlf.slang new file mode 100644 index 000000000..e0005e1e7 Binary files /dev/null and b/tests/preprocessor/utf16_le_bom_crlf.slang differ diff --git a/tests/preprocessor/utf8_bom_crlf.slang b/tests/preprocessor/utf8_bom_crlf.slang new file mode 100644 index 000000000..bc8a6b12b --- /dev/null +++ b/tests/preprocessor/utf8_bom_crlf.slang @@ -0,0 +1,4 @@ +void main() +{ +} +//TEST:SIMPLE: -- cgit v1.2.3