Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Simplify encoding detection using SourceText auto-detection
- Remove custom DetectEncoding() method and use SourceText.From(stream, encoding: null)
- SourceText automatically detects encoding including BOM when encoding is null
- Move <see href/> tags inside <summary> elements for better documentation
- Simplifies implementation while maintaining full functionality

Co-authored-by: jjonescz <[email protected]>
  • Loading branch information
Copilot and jjonescz committed Dec 7, 2025
commit c061b9e5907f6987d4b5875c8b7d22f6325faa0c
Original file line number Diff line number Diff line change
Expand Up @@ -258,30 +258,8 @@ internal readonly record struct SourceFile(string Path, SourceText Text)
public static SourceFile Load(string filePath)
{
using var stream = File.OpenRead(filePath);
// Detect BOM to determine the appropriate encoding
Encoding encoding = DetectEncoding(stream);
stream.Position = 0; // Reset stream position after BOM detection
return new SourceFile(filePath, SourceText.From(stream, encoding));
}

private static Encoding DetectEncoding(Stream stream)
{
// UTF-8 BOM is 0xEF 0xBB 0xBF
if (stream.Length < 3)
{
return new UTF8Encoding(encoderShouldEmitUTF8Identifier: false);
}

#if NETCOREAPP
Span<byte> buffer = stackalloc byte[3];
int bytesRead = stream.Read(buffer);
#else
byte[] buffer = new byte[3];
int bytesRead = stream.Read(buffer, 0, 3);
#endif
bool hasUtf8Bom = bytesRead == 3 && buffer[0] == 0xEF && buffer[1] == 0xBB && buffer[2] == 0xBF;

return new UTF8Encoding(encoderShouldEmitUTF8Identifier: hasUtf8Bom);
// Let SourceText.From auto-detect the encoding (including BOM detection)
return new SourceFile(filePath, SourceText.From(stream, encoding: null));
}

public SourceFile WithText(SourceText newText)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -520,8 +520,8 @@ public void RemoveMultiple()
/// <summary>
/// Verifies that files without UTF-8 BOM don't get one added when saved.
/// This is critical for shebang (#!) scripts on Unix-like systems.
/// </summary>
/// <see href="https://github.com/dotnet/sdk/issues/52054"/>
/// </summary>
[Fact]
public void PreservesNoBomEncoding()
{
Expand Down Expand Up @@ -550,8 +550,8 @@ public void PreservesNoBomEncoding()

/// <summary>
/// Verifies that files with UTF-8 BOM preserve it when saved.
/// </summary>
/// <see href="https://github.com/dotnet/sdk/issues/52054"/>
/// </summary>
[Fact]
public void PreservesBomEncoding()
{
Expand Down
Loading