Set the console codepage to ensure correct text display regardless of encoding (#1337)

This commit is contained in:
はやしうめ authored and GitHub committed 2026-06-09 17:07:44 +02:00
1 parent f568f0e7b5
commit 9aedb90fc6
2 files changed
+7 -6

No files matched your search

@@ -41,7 +41,7 @@ internal partial class ConsoleEncoding : Encoding
public override int GetByteCount(char[] chars, int index, int count)
{
WriteCharBuffer(chars, index, count);
var result = WideCharToMultiByte(_codePage, 0, chars, count, _zeroByte, 0, IntPtr.Zero, IntPtr.Zero);
var result = WideCharToMultiByte(_codePage, 0, _charBuffer, count, _zeroByte, 0, IntPtr.Zero, IntPtr.Zero);
return result;
}
@@ -50,7 +50,7 @@ internal partial class ConsoleEncoding : Encoding
var byteCount = GetByteCount(chars, charIndex, charCount);
WriteCharBuffer(chars, charIndex, charCount);
ExpandByteBuffer(byteCount);
_ = WideCharToMultiByte(_codePage, 0, chars, charCount, _byteBuffer, byteCount, IntPtr.Zero,
_ = WideCharToMultiByte(_codePage, 0, _charBuffer, charCount, _byteBuffer, byteCount, IntPtr.Zero,
IntPtr.Zero);
var readCount = Math.Min(bytes.Length, byteCount);
ReadByteBuffer(bytes, byteIndex, readCount);
@@ -60,7 +60,7 @@ internal partial class ConsoleEncoding : Encoding
public override int GetCharCount(byte[] bytes, int index, int count)
{
WriteByteBuffer(bytes, index, count);
var result = MultiByteToWideChar(_codePage, 0, bytes, count, _zeroChar, 0);
var result = MultiByteToWideChar(_codePage, 0, _byteBuffer, count, _zeroChar, 0);
return result;
}
@@ -69,7 +69,7 @@ internal partial class ConsoleEncoding : Encoding
var charCount = GetCharCount(bytes, byteIndex, byteCount);
WriteByteBuffer(bytes, byteIndex, byteCount);
ExpandCharBuffer(charCount);
_ = MultiByteToWideChar(_codePage, 0, bytes, byteCount, _charBuffer, charCount);
_ = MultiByteToWideChar(_codePage, 0, _byteBuffer, byteCount, _charBuffer, charCount);
var readCount = Math.Min(chars.Length, charCount);
ReadCharBuffer(chars, charIndex, readCount);
return readCount;
@@ -92,8 +92,9 @@ internal class WindowsConsoleDriver : IConsoleDriver
// Make sure of ConsoleEncoding helper class because on some Monos
// Encoding.GetEncoding throws NotImplementedException on most codepages
// NOTE: We don't set Console.OutputEncoding because it resets any existing Console.Out writers
if (!useManagedEncoder)
ConsoleEncoding.ConsoleCodePage = codepage;
// Always set the console output codepage so the Windows console interprets bytes correctly,
// regardless of whether we use a managed encoder or ConsoleEncoding to produce those bytes.
ConsoleEncoding.ConsoleCodePage = codepage;
// If stdout exists, write to it, otherwise make it the same as console out
// Not sure if this is needed? Does the original Console.Out still work?