Skip to content

Commit 4cb1f83

Browse files
authored
Eliminates exceptions when characters are outside ISO-8859-1 range (#672)
1 parent dd5596f commit 4cb1f83

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

QRCoder/QRCodeGenerator.cs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -961,22 +961,21 @@ private static int GetDataLength(EncodingMode encoding, string plainText, BitArr
961961
bool IsUtf8() => (encoding == EncodingMode.Byte && (forceUtf8 || !IsValidISO(plainText)));
962962
}
963963

964-
private static readonly Encoding _iso88591ExceptionFallback = Encoding.GetEncoding(28591, new EncoderExceptionFallback(), new DecoderExceptionFallback()); // ISO-8859-1
965964
/// <summary>
966965
/// Checks if the given string can be accurately represented and retrieved in ISO-8859-1 encoding.
967966
/// </summary>
968967
private static bool IsValidISO(string input)
969968
{
970-
// No heap allocations if the string is ISO-8859-1
971-
try
969+
// ISO-8859-1 contains the same characters as UTF-16 for the range 0x00-0xFF.
970+
// 0x00-0x7F: ASCII (0-127)
971+
// 0x80-0x9F: C1 control characters (128-159)
972+
// 0xA0-0xFF: Extended Latin (160-255)
973+
foreach (char c in input)
972974
{
973-
_ = _iso88591ExceptionFallback.GetByteCount(input);
974-
return true;
975-
}
976-
catch (EncoderFallbackException) // The exception is a heap allocation and not ideal
977-
{
978-
return false;
975+
if (c > 0xFF)
976+
return false;
979977
}
978+
return true;
980979
}
981980

982981
/// <summary>

0 commit comments

Comments
 (0)