From 8ff10a1af01d4d32255828fff9a66752aa343d69 Mon Sep 17 00:00:00 2001 From: Adam Sitnik Date: Thu, 18 Jul 2024 18:06:04 +0200 Subject: [PATCH 1/3] handle Unicode encoding in Console.Beep --- .../System.Console/src/System/ConsolePal.Windows.cs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/libraries/System.Console/src/System/ConsolePal.Windows.cs b/src/libraries/System.Console/src/System/ConsolePal.Windows.cs index 49f2d42b97fe8..97fd450fd531e 100644 --- a/src/libraries/System.Console/src/System/ConsolePal.Windows.cs +++ b/src/libraries/System.Console/src/System/ConsolePal.Windows.cs @@ -679,8 +679,13 @@ public static void Beep() { if (!Console.IsOutputRedirected) { - ReadOnlySpan bell = "\u0007"u8; // Windows doesn't use terminfo, so the codepoint is hardcoded. - int errorCode = WindowsConsoleStream.WriteFileNative(OutputHandle, bell, useFileAPIs: Console.OutputEncoding.CodePage != UnicodeCodePage); + bool isUnicode = Console.OutputEncoding.CodePage == UnicodeCodePage; + + // Windows doesn't use terminfo, so the codepoint is hardcoded. + ReadOnlySpan bell = isUnicode ? stackalloc byte[2] { 7, 0 } : stackalloc byte[1] { 7 }; + Debug.Assert(!isUnicode || bell.SequenceEqual(Encoding.Unicode.GetBytes("\u0007"))); + + int errorCode = WindowsConsoleStream.WriteFileNative(OutputHandle, bell, useFileAPIs: !isUnicode); if (errorCode == Interop.Errors.ERROR_SUCCESS) { return; From 6a372c6f57c77979445fc1561ba02db58e55d8e0 Mon Sep 17 00:00:00 2001 From: Adam Sitnik Date: Fri, 19 Jul 2024 11:09:35 +0200 Subject: [PATCH 2/3] address code review feedback: handle any encoding and endianness --- .../src/System/ConsolePal.Windows.cs | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/libraries/System.Console/src/System/ConsolePal.Windows.cs b/src/libraries/System.Console/src/System/ConsolePal.Windows.cs index 97fd450fd531e..8e3bb34adc2cd 100644 --- a/src/libraries/System.Console/src/System/ConsolePal.Windows.cs +++ b/src/libraries/System.Console/src/System/ConsolePal.Windows.cs @@ -679,16 +679,18 @@ public static void Beep() { if (!Console.IsOutputRedirected) { - bool isUnicode = Console.OutputEncoding.CodePage == UnicodeCodePage; - - // Windows doesn't use terminfo, so the codepoint is hardcoded. - ReadOnlySpan bell = isUnicode ? stackalloc byte[2] { 7, 0 } : stackalloc byte[1] { 7 }; - Debug.Assert(!isUnicode || bell.SequenceEqual(Encoding.Unicode.GetBytes("\u0007"))); - - int errorCode = WindowsConsoleStream.WriteFileNative(OutputHandle, bell, useFileAPIs: !isUnicode); - if (errorCode == Interop.Errors.ERROR_SUCCESS) + Span bell = stackalloc byte[10]; + if (Console.OutputEncoding.TryGetBytes("\u0007", bell, out int bytesWritten)) { - return; + int errorCode = WindowsConsoleStream.WriteFileNative(OutputHandle, bell.Slice(0, bytesWritten), useFileAPIs: Console.OutputEncoding.CodePage != UnicodeCodePage); + if (errorCode == Interop.Errors.ERROR_SUCCESS) + { + return; + } + } + else + { + Debug.Fail("The input buffer was too small."); } } From 577f641f86447b4b66e00982bf09c587be2e18d8 Mon Sep 17 00:00:00 2001 From: Adam Sitnik Date: Mon, 22 Jul 2024 13:38:44 +0200 Subject: [PATCH 3/3] address code review feedback: call GetBytes when 10 bytes is not enough to encode a single character for custom encoding --- .../src/System/ConsolePal.Windows.cs | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/libraries/System.Console/src/System/ConsolePal.Windows.cs b/src/libraries/System.Console/src/System/ConsolePal.Windows.cs index 8e3bb34adc2cd..e9abcdb7991a3 100644 --- a/src/libraries/System.Console/src/System/ConsolePal.Windows.cs +++ b/src/libraries/System.Console/src/System/ConsolePal.Windows.cs @@ -679,18 +679,22 @@ public static void Beep() { if (!Console.IsOutputRedirected) { + const string BellString = "\u0007"; // Windows doesn't use terminfo, so the codepoint is hardcoded. + Span bell = stackalloc byte[10]; - if (Console.OutputEncoding.TryGetBytes("\u0007", bell, out int bytesWritten)) + if (Console.OutputEncoding.TryGetBytes(BellString, bell, out int bytesWritten)) { - int errorCode = WindowsConsoleStream.WriteFileNative(OutputHandle, bell.Slice(0, bytesWritten), useFileAPIs: Console.OutputEncoding.CodePage != UnicodeCodePage); - if (errorCode == Interop.Errors.ERROR_SUCCESS) - { - return; - } + bell = bell.Slice(0, bytesWritten); } else { - Debug.Fail("The input buffer was too small."); + bell = Console.OutputEncoding.GetBytes(BellString); + } + + int errorCode = WindowsConsoleStream.WriteFileNative(OutputHandle, bell, useFileAPIs: Console.OutputEncoding.CodePage != UnicodeCodePage); + if (errorCode == Interop.Errors.ERROR_SUCCESS) + { + return; } }