File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -13,8 +13,18 @@ function Get-BaseRef {
1313function Test-HasUtf8Bom {
1414 param ([Parameter (Mandatory )][string ]$Path )
1515
16- $bytes = [System.IO.File ]::ReadAllBytes($Path )
17- return $bytes.Length -ge 3 -and $bytes [0 ] -eq 0xEF -and $bytes [1 ] -eq 0xBB -and $bytes [2 ] -eq 0xBF
16+ # Read only the first three bytes to check for a UTF-8 BOM to avoid loading the entire file.
17+ $buffer = [byte []]::new(3 )
18+ $stream = [System.IO.File ]::OpenRead($Path )
19+ try {
20+ $bytesRead = $stream.Read ($buffer , 0 , 3 )
21+ }
22+ finally {
23+ $stream.Dispose ()
24+ }
25+
26+ if ($bytesRead -lt 3 ) { return $false }
27+ return $buffer [0 ] -eq 0xEF -and $buffer [1 ] -eq 0xBB -and $buffer [2 ] -eq 0xBF
1828}
1929
2030function Write-Utf8Text {
@@ -31,8 +41,8 @@ function Write-Utf8Text {
3141function Format-FileWhitespace {
3242 param ([Parameter (Mandatory )][string ]$Path )
3343 if (-not (Test-Path - LiteralPath $Path )) { return }
34- $hasUtf8Bom = Test-HasUtf8Bom - Path $Path
3544 try {
45+ $hasUtf8Bom = Test-HasUtf8Bom - Path $Path
3646 $raw = Get-Content - LiteralPath $Path - Raw - Encoding utf8
3747 }
3848 catch {
You can’t perform that action at this time.
0 commit comments