Author: kornelpal
Date: 2006-08-27 15:43:26 -0400 (Sun, 27 Aug 2006)
New Revision: 64447

Modified:
   trunk/vbnc/changelog.txt
   trunk/vbnc/vbnc/source/General/CodeFile.vb
   trunk/vbnc/vbnc/source/General/CommandLine.vb
Log:
Decode source files using UTF-8 when they can be decoded using UTF-8.

Modified: trunk/vbnc/changelog.txt
===================================================================
--- trunk/vbnc/changelog.txt    2006-08-27 19:37:52 UTC (rev 64446)
+++ trunk/vbnc/changelog.txt    2006-08-27 19:43:26 UTC (rev 64447)
@@ -1,3 +1,6 @@
+2006-06-07  Kornél Pál  <[EMAIL PROTECTED]>
+       * Decode source files using UTF-8 when they can be decoded using UTF-8.
+
 2006-08-21  Rolf Bjarne Kvinge <[EMAIL PROTECTED]>
        * Added brackets to some identifiers that were causing problems if you 
had LINQ installed. (New keywords in LINQ)
        * Added support for /codepage (thanks to Kornél Pál)

Modified: trunk/vbnc/vbnc/source/General/CodeFile.vb
===================================================================
--- trunk/vbnc/vbnc/source/General/CodeFile.vb  2006-08-27 19:37:52 UTC (rev 
64446)
+++ trunk/vbnc/vbnc/source/General/CodeFile.vb  2006-08-27 19:43:26 UTC (rev 
64447)
@@ -35,6 +35,8 @@
 Public Class CodeFile
     Inherits BaseObject
 
+    Private Shared m_UTF8Throw As System.Text.Encoding
+
     ''' <summary>
     ''' The filename of the file.
     ''' </summary>
@@ -62,6 +64,15 @@
 
     Private m_SymbolDocument As 
System.Diagnostics.SymbolStore.ISymbolDocumentWriter
 
+    Private Shared ReadOnly Property UTF8Throw() As System.Text.Encoding
+        Get
+            ' Use no preamble to let StreamReader use a non-throwing decoder
+            ' when UTF-8 byte order mark found.
+            If m_UTF8Throw Is Nothing Then m_UTF8Throw = New 
System.Text.UTF8Encoding(False, True)
+            Return m_UTF8Throw
+        End Get
+    End Property
+
     ReadOnly Property SymbolDocument() As 
System.Diagnostics.SymbolStore.ISymbolDocumentWriter
         Get
             Return m_SymbolDocument
@@ -199,8 +210,34 @@
     ''' <remarks></remarks>
     ReadOnly Property Code() As String
         Get
+            Dim FileStream As System.IO.FileStream
+            Dim StreamReader As System.IO.StreamReader
+            Dim Encoding As System.Text.Encoding
+            Dim IsUTF8 As Boolean
+
             Try
-                Return IO.File.ReadAllText(FileName, 
Compiler.CommandLine.Encoding)
+                FileStream = New System.IO.FileStream(FileName, 
IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.Read)
+                Try
+                    Encoding = Compiler.CommandLine.Encoding
+                    IsUTF8 = Encoding.CodePage = 65001
+
+                    ' No UTF-8 detection required when default is UTF-8.
+                    If Not IsUTF8 Then
+                        ' Decode using UTF-8 when can be decoded using UTF-8.
+                        StreamReader = New System.IO.StreamReader(FileStream, 
UTF8Throw, True)
+                        Try
+                            Return StreamReader.ReadToEnd()
+                        Catch e As ArgumentException
+                            FileStream.Position = 0
+                        End Try
+                    End If
+
+                    ' Byte order mark was already detected when default is not 
UTF-8.
+                    StreamReader = New System.IO.StreamReader(FileStream, 
Encoding, IsUTF8)
+                    Return StreamReader.ReadToEnd()
+                Finally
+                    FileStream.Close()
+                End Try
             Catch e As Exception
                 Compiler.Report.ShowMessage(Messages.VBNC31007, FileName)
                 Return ""

Modified: trunk/vbnc/vbnc/source/General/CommandLine.vb
===================================================================
--- trunk/vbnc/vbnc/source/General/CommandLine.vb       2006-08-27 19:37:52 UTC 
(rev 64446)
+++ trunk/vbnc/vbnc/source/General/CommandLine.vb       2006-08-27 19:43:26 UTC 
(rev 64447)
@@ -623,7 +623,7 @@
     ''' </summary>
     ReadOnly Property Encoding() As System.Text.Encoding
         Get
-            If (m_Encoding Is Nothing) Then m_Encoding = 
System.Text.Encoding.Default
+            If m_Encoding Is Nothing Then m_Encoding = 
System.Text.Encoding.Default
             Return m_Encoding
         End Get
     End Property

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to