Use same Encoding for Asc() and Chr() (dotnet/corefx#39096)
authorCharles Stoner <chucks@microsoft.com>
Tue, 2 Jul 2019 04:13:57 +0000 (21:13 -0700)
committerGitHub <noreply@github.com>
Tue, 2 Jul 2019 04:13:57 +0000 (21:13 -0700)
Commit migrated from https://github.com/dotnet/corefx/commit/11fc2aa48748edb8cf2a9642d0ab0c3b84c393df

src/libraries/Microsoft.VisualBasic.Core/src/Microsoft/VisualBasic/Strings.vb
src/libraries/Microsoft.VisualBasic.Core/tests/StringsTests.cs

index b4ad704..e49fed8 100644 (file)
@@ -154,6 +154,10 @@ Namespace Global.Microsoft.VisualBasic
             Return (lcid And &H3FF)
         End Function
 
+        Private Function GetAscChrEncoding() As Encoding
+            Return Encoding.GetEncoding(GetLocaleCodePage())
+        End Function
+
         '============================================================================
         ' Character manipulation functions.
         '============================================================================
@@ -173,7 +177,7 @@ Namespace Global.Microsoft.VisualBasic
                 Dim c() As Char
                 Dim iByteCount As Integer
 
-                enc = GetFileIOEncoding()
+                enc = GetAscChrEncoding()
 
                 c = New Char() {[String]}
 
@@ -241,7 +245,7 @@ Namespace Global.Microsoft.VisualBasic
             Try
                 Dim enc As Encoding
 
-                enc = Encoding.GetEncoding(GetLocaleCodePage())
+                enc = GetAscChrEncoding()
 
                 If enc.IsSingleByte Then
                     If CharCode < 0 OrElse CharCode > 255 Then
index bf80010..a6af8d3 100644 (file)
@@ -115,6 +115,50 @@ namespace Microsoft.VisualBasic.Tests
         }
 
         [Theory]
+        [InlineData(0, 0)]
+        [InlineData(33, 33)]
+        [InlineData(172, 172)]
+        [InlineData(255, 255)]
+        public void Asc_Chr_Invariant(int charCode, int expected)
+        {
+            RemoteExecutor.Invoke((charCodeString, expectedString) =>
+            {
+                int charCode = int.Parse(charCodeString);
+                int expected = int.Parse(expectedString);
+
+                Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
+                CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
+                Assert.Equal(1252, CultureInfo.CurrentCulture.TextInfo.ANSICodePage);
+
+                Assert.Equal(expected, Strings.Asc(Strings.Chr(charCode)));
+            }, charCode.ToString(), expected.ToString()).Dispose();
+        }
+
+        [Theory]
+        [InlineData(0, 0)]
+        [InlineData(33, 33)]
+        [InlineData(172, 0)]
+        [InlineData(255, 255)]
+        [InlineData(256, 1)]
+        [InlineData(0x8141, 0x8141)]
+        [InlineData(0xC8FE, 0xC8FE)]
+        [InlineData(0xFFFF, 0xFF)]
+        public void Asc_Chr_DoubleByte(int charCode, int expected)
+        {
+            RemoteExecutor.Invoke((charCodeString, expectedString) =>
+            {
+                int charCode = int.Parse(charCodeString);
+                int expected = int.Parse(expectedString);
+
+                Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
+                CultureInfo.CurrentCulture = new CultureInfo("ko-KR");
+                Assert.Equal(949, CultureInfo.CurrentCulture.TextInfo.ANSICodePage);
+
+                Assert.Equal(expected, (ushort)Strings.Asc(Strings.Chr(charCode)));
+            }, charCode.ToString(), expected.ToString()).Dispose();
+        }
+
+        [Theory]
         [InlineData(new string[] { }, null, null)]
         [InlineData(new string[] { }, "", null)]
         public void Filter_WhenNoMatchArgument_ReturnsNull(string[] source, string match, string[] expected)