Skip to content

Commit d8c8d4c

Browse files
papehCopilot
andauthored
Apply AI suggestions from code review
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
1 parent fc536e0 commit d8c8d4c

1 file changed

Lines changed: 13 additions & 3 deletions

File tree

Build/Agent/fix-whitespace.ps1

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,18 @@ function Get-BaseRef {
1313
function 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

2030
function Write-Utf8Text {
@@ -31,8 +41,8 @@ function Write-Utf8Text {
3141
function 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 {

0 commit comments

Comments
 (0)