提交 58890a3c 编写于 作者: A AlekseyTs

Use default OS encoding to decode non-Unicode/non-UTF8 text, if available,...

Use default OS encoding to decode non-Unicode/non-UTF8 text, if available, instead of unconditionally falling back to cp1252 or Latin1.

This brings Roslyn behavior close to VS2013. Fixes #4264. I also believe this change should address #4255, #4222 and #4022.
上级 280d6922
......@@ -19,11 +19,11 @@ internal static class EncodedStringText
private static readonly Encoding s_fallbackEncoding = new UTF8Encoding(encoderShouldEmitUTF8Identifier: false, throwOnInvalidBytes: true);
/// <summary>
/// Encoding to use when UTF-8 fails. If available, we use CodePage 1252. If not, we use Latin1.
/// Encoding to use when UTF-8 fails and default OS ANSI encoding is not available. If available, we use CodePage 1252. If not, we use Latin1.
/// </summary>
private static readonly Encoding s_defaultEncoding = GetDefaultEncoding();
private static readonly Encoding s_defaultFallbackEncoding = GetDefaultFallbackEncoding();
private static Encoding GetDefaultEncoding()
private static Encoding GetDefaultFallbackEncoding()
{
try
{
......@@ -35,6 +35,20 @@ private static Encoding GetDefaultEncoding()
}
}
private static Encoding GetDefaultAnsiEncoding()
{
try
{
// To get the encoding associated with the default ANSI code page in the operating system's regional and language settings,
// supply a value 0 for the codepage argument
return PortableShim.Encoding.GetEncoding(0) ?? s_defaultFallbackEncoding;
}
catch
{
return s_defaultFallbackEncoding;
}
}
/// <summary>
/// Initializes an instance of <see cref="SourceText"/> from the provided stream. This version differs
/// from <see cref="SourceText.From(Stream, Encoding, SourceHashAlgorithm, bool)"/> in two ways:
......@@ -74,7 +88,7 @@ internal static SourceText Create(Stream stream, Encoding defaultEncoding = null
try
{
return Decode(stream, defaultEncoding ?? s_defaultEncoding, checksumAlgorithm, throwIfBinaryDetected: detectEncoding);
return Decode(stream, defaultEncoding ?? GetDefaultAnsiEncoding(), checksumAlgorithm, throwIfBinaryDetected: detectEncoding);
}
catch (DecoderFallbackException e)
{
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册