From 0c9a563435067119060ed60a07318cfae597748f Mon Sep 17 00:00:00 2001 From: Shane Krueger Date: Sun, 12 Oct 2025 21:41:14 -0400 Subject: [PATCH 1/2] Eliminates exceptions when characters are outside ISO-8859-1 range --- QRCoder/QRCodeGenerator.cs | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/QRCoder/QRCodeGenerator.cs b/QRCoder/QRCodeGenerator.cs index 09b681aa..879842a5 100644 --- a/QRCoder/QRCodeGenerator.cs +++ b/QRCoder/QRCodeGenerator.cs @@ -995,22 +995,21 @@ private static int GetDataLength(EncodingMode encoding, string plainText, BitArr bool IsUtf8() => (encoding == EncodingMode.Byte && (forceUtf8 || !IsValidISO(plainText))); } - private static readonly Encoding _iso88591ExceptionFallback = Encoding.GetEncoding(28591, new EncoderExceptionFallback(), new DecoderExceptionFallback()); // ISO-8859-1 /// /// Checks if the given string can be accurately represented and retrieved in ISO-8859-1 encoding. /// private static bool IsValidISO(string input) { - // No heap allocations if the string is ISO-8859-1 - try + // ISO-8859-1 contains the same characters as UTF-16 for the range 0x00-0xFF. + // 0x00-0x7F: ASCII (0-127) + // 0x80-0x9F: C1 control characters (128-159) + // 0xA0-0xFF: Extended Latin (160-255 + foreach (char c in input) { - _ = _iso88591ExceptionFallback.GetByteCount(input); - return true; - } - catch (EncoderFallbackException) // The exception is a heap allocation and not ideal - { - return false; + if (c > 0xFF) + return false; } + return true; } /// From f50cc48b93a82f16f1375538a421b5b1e7629f33 Mon Sep 17 00:00:00 2001 From: Shane Krueger Date: Sun, 12 Oct 2025 21:50:14 -0400 Subject: [PATCH 2/2] fix comment --- QRCoder/QRCodeGenerator.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/QRCoder/QRCodeGenerator.cs b/QRCoder/QRCodeGenerator.cs index 879842a5..2f4e596c 100644 --- a/QRCoder/QRCodeGenerator.cs +++ b/QRCoder/QRCodeGenerator.cs @@ -1003,7 +1003,7 @@ private static bool IsValidISO(string input) // ISO-8859-1 contains the same characters as UTF-16 for the range 0x00-0xFF. // 0x00-0x7F: ASCII (0-127) // 0x80-0x9F: C1 control characters (128-159) - // 0xA0-0xFF: Extended Latin (160-255 + // 0xA0-0xFF: Extended Latin (160-255) foreach (char c in input) { if (c > 0xFF)