@@ -65,13 +65,6 @@ public class InvokeFormatterCommand : PSCmdlet, IOutputWriter
6565 [ ValidateCount ( 4 , 4 ) ]
6666 public int [ ] Range { get ; set ; }
6767
68- /// <summary>
69- /// The encoding to use when reading files without a byte-order mark.
70- /// </summary>
71- [ Parameter ( Mandatory = false ) ]
72- [ ValidateNotNullOrEmpty ]
73- public string Encoding { get ; set ; } = "utf-8" ;
74-
7568#if DEBUG
7669 /// <summary>
7770 /// Attaches to an instance of a .Net debugger
@@ -161,14 +154,22 @@ private void FormatFile(string path)
161154 {
162155 Encoding encoding ;
163156 string scriptDefinition ;
164- var requestedEncoding = System . Text . Encoding . GetEncoding (
165- Encoding ,
166- EncoderFallback . ExceptionFallback ,
167- DecoderFallback . ExceptionFallback ) ;
168- using ( var reader = new StreamReader ( path , requestedEncoding , true ) )
157+ try
158+ {
159+ using ( var reader = new StreamReader ( path , new UTF8Encoding ( false , true ) , true ) )
160+ {
161+ scriptDefinition = reader . ReadToEnd ( ) ;
162+ encoding = reader . CurrentEncoding ;
163+ }
164+ }
165+ catch ( DecoderFallbackException ) when ( ! HasByteOrderMark ( path ) )
169166 {
170- scriptDefinition = reader . ReadToEnd ( ) ;
171- encoding = reader . CurrentEncoding ;
167+ encoding = GetLegacyFallbackEncoding ( ) ;
168+ using ( var reader = new StreamReader ( path , encoding , true ) )
169+ {
170+ scriptDefinition = reader . ReadToEnd ( ) ;
171+ encoding = reader . CurrentEncoding ;
172+ }
172173 }
173174
174175 bool inferIndentationKind = inputSettings . RuleArguments . TryGetValue ( IndentationRule , out var indentationSettings )
@@ -199,6 +200,39 @@ private void FormatFile(string path)
199200 WriteObject ( formattedScriptDefinition ) ;
200201 }
201202
203+ private static bool HasByteOrderMark ( string path )
204+ {
205+ using ( var stream = File . OpenRead ( path ) )
206+ {
207+ int firstByte = stream . ReadByte ( ) ;
208+ int secondByte = stream . ReadByte ( ) ;
209+ int thirdByte = stream . ReadByte ( ) ;
210+ int fourthByte = stream . ReadByte ( ) ;
211+
212+ return firstByte == 0xEF && secondByte == 0xBB && thirdByte == 0xBF
213+ || firstByte == 0xFF && secondByte == 0xFE
214+ || firstByte == 0xFE && secondByte == 0xFF
215+ || firstByte == 0x00 && secondByte == 0x00 && thirdByte == 0xFE && fourthByte == 0xFF
216+ || firstByte == 0xFF && secondByte == 0xFE && thirdByte == 0x00 && fourthByte == 0x00 ;
217+ }
218+ }
219+
220+ private static Encoding GetLegacyFallbackEncoding ( )
221+ {
222+ if ( Encoding . Default . CodePage != 65001 )
223+ {
224+ return Encoding . GetEncoding (
225+ Encoding . Default . CodePage ,
226+ EncoderFallback . ExceptionFallback ,
227+ DecoderFallback . ExceptionFallback ) ;
228+ }
229+
230+ #if CORECLR
231+ Encoding . RegisterProvider ( CodePagesEncodingProvider . Instance ) ;
232+ #endif
233+ return Encoding . GetEncoding ( 1252 , EncoderFallback . ExceptionFallback , DecoderFallback . ExceptionFallback ) ;
234+ }
235+
202236 private static string GetIndentationKind ( string scriptDefinition )
203237 {
204238 int tabIndentedLines = 0 ;
0 commit comments