Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/libraries/System.Console/src/System/ConsolePal.Windows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -679,8 +679,13 @@ public static void Beep()
{
if (!Console.IsOutputRedirected)
{
ReadOnlySpan<byte> 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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there other encodings that are not UnicodeCodePage and that may end up the bell characters into more than one byte? Would it make more sense to just encode the bell character using the OutputEncoding instead of trying to guess what the encoding may do here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right. Moreover, this method should never be on a hot path so we don't really gain anything by hardcoding the encoded bytes. I've changed the implementation, PTAL


// Windows doesn't use terminfo, so the codepoint is hardcoded.
ReadOnlySpan<byte> bell = isUnicode ? stackalloc byte[2] { 7, 0 } : stackalloc byte[1] { 7 };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume we don't need to worry about endianness here?

Debug.Assert(!isUnicode || bell.SequenceEqual(Encoding.Unicode.GetBytes("\u0007")));

int errorCode = WindowsConsoleStream.WriteFileNative(OutputHandle, bell, useFileAPIs: !isUnicode);
if (errorCode == Interop.Errors.ERROR_SUCCESS)
{
return;
Expand Down