summaryrefslogtreecommitdiffstats
path: root/source/core
diff options
context:
space:
mode:
authorcheneym2 <acheney@nvidia.com>2024-05-14 14:05:58 -0400
committerGitHub <noreply@github.com>2024-05-14 11:05:58 -0700
commit291b4cd82cebeed39d8c06c8208fc415dfa32a48 (patch)
treeaa3f61e2fbfa760e61f00e015800939579961dd4 /source/core
parent9ab24cfa4ef2a5b4572a28eff5a65266416b0a88 (diff)
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 <yonghe@outlook.com>
Diffstat (limited to 'source/core')
-rw-r--r--source/core/slang-char-encode.cpp25
1 files changed, 12 insertions, 13 deletions
diff --git a/source/core/slang-char-encode.cpp b/source/core/slang-char-encode.cpp
index 687040fa2..105cfac7f 100644
--- a/source/core/slang-char-encode.cpp
+++ b/source/core/slang-char-encode.cpp
@@ -92,17 +92,17 @@ public:
Index index = 0;
while (index < length)
{
- const Char32 codePoint = getUnicodePointFromUTF16([&]() -> Byte
+ auto readByte = [&]() -> Byte
{
- if (index < length)
- return bytes[index++];
- else
- return Byte(0);
- });
+ return (index < length) ? bytes[index++] : Byte(0);
+ };
+ const Char32 codePoint = m_reverseOrder ?
+ getUnicodePointFromUTF16Reversed(readByte) :
+ getUnicodePointFromUTF16(readByte);
char buf[5];
int count = encodeUnicodePointToUTF8(codePoint, buf);
- ioBuffer.addRange((const char*)buf, count);
+ ioBuffer.addRange((const char*)buf, count);
}
}
@@ -134,11 +134,10 @@ private:
outOffset = 2;
return CharEncodeType::UTF16Reversed;
}
- }
- else
- {
- // If we don't have a 'mark' byte then we are bit stumped. We'll look for a null bytes and assume they mean we have a 16 bit encoding
- for (size_t i = 0; i < bytesCount; i += 2)
+
+ // If we don't have a 'mark' byte then we are bit stumped. We'll look for
+ // null (non-terminator) bytes and assume they mean we have a 16-bit encoding
+ for(size_t i = 0; i < (bytesCount-1); i += 2)
{
#if SLANG_LITTLE_ENDIAN
const auto low = bytes[i];
@@ -146,7 +145,7 @@ private:
#else
const auto low = bytes[i + 1];
const auto high = bytes[i];
-#endif
+#endif
if ((low == 0) ^ (high == 0))
{
outOffset = 2;