Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 4 additions & 1 deletion src/CharsetDetector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,10 @@ private bool IsStartsWithBom(byte[] buf, int offset, int len)
var bomSet = FindCharSetByBom(buf, offset, len);
if (bomSet != null)
{
_detectionDetail = new DetectionDetail(bomSet, 1.0f);
_detectionDetail = new DetectionDetail(bomSet, 1.0f)
{
HasBOM = true
};
return true;
}
return false;
Expand Down
7 changes: 6 additions & 1 deletion src/DetectionDetail.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ public DetectionDetail(CharsetProber prober, TimeSpan? time = null)
/// </summary>
public CharsetProber Prober { get; set; }

/// <summary>
/// A Byte Order Mark was detected
/// </summary>
public bool HasBOM { get; set; }

/// <summary>
/// The time spend
/// </summary>
Expand All @@ -80,7 +85,7 @@ public DetectionDetail(CharsetProber prober, TimeSpan? time = null)

public override string ToString()
{
return $"Detected {EncodingName} with confidence of {Confidence}";
return $"Detected {EncodingName} with confidence of {Confidence}. (BOM: {HasBOM})";
}

internal static Encoding GetEncoding(string encodingShortName)
Expand Down
26 changes: 21 additions & 5 deletions tests/CharsetDetectorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public void TestAscii()
var result = CharsetDetector.DetectFromStream(stream);
Assert.AreEqual(CodepageName.ASCII, result.Detected.EncodingName);
Assert.AreEqual(1.0f, result.Detected.Confidence);
Assert.IsFalse(result.Detected.HasBOM);
}
}

Expand Down Expand Up @@ -77,6 +78,7 @@ public void DetectFromByteArray(int offset, int len, string detectedCodepage)
// Assert
Assert.AreEqual(detectedCodepage, result.Detected.EncodingName);
Assert.AreEqual(1.0f, result.Detected.Confidence);
Assert.IsFalse(result.Detected.HasBOM);
}

[Test]
Expand All @@ -91,8 +93,9 @@ public void TestCaseBomUtf7(byte[] bufferBytes)
.Detected;
Assert.AreEqual(CodepageName.UTF7, result.EncodingName);
Assert.AreEqual(1.0f, result.Confidence);
Assert.IsTrue(result.HasBOM);
}

[Test]
public void TestBomGb18030()
{
Expand All @@ -101,6 +104,7 @@ public void TestBomGb18030()
.Detected;
Assert.AreEqual(CodepageName.GB18030, result.EncodingName);
Assert.AreEqual(1.0f, result.Confidence);
Assert.IsTrue(result.HasBOM);
}

[Test]
Expand All @@ -114,6 +118,7 @@ public void TestUTF8_1()
var result = CharsetDetector.DetectFromBytes(buf);
Assert.AreEqual(CodepageName.UTF8, result.Detected.EncodingName);
Assert.AreEqual(1.0f, result.Detected.Confidence);
Assert.IsFalse(result.Detected.HasBOM);
}

[Test]
Expand All @@ -123,8 +128,9 @@ public void TestBomUtf8()
var result = CharsetDetector.DetectFromBytes(buf);
Assert.AreEqual(CodepageName.UTF8, result.Detected.EncodingName);
Assert.AreEqual(1.0f, result.Detected.Confidence);
Assert.IsTrue(result.Detected.HasBOM);
}

[Test]
public void Test2byteArrayBomUTF16_BE()
{
Expand All @@ -133,6 +139,7 @@ public void Test2byteArrayBomUTF16_BE()
var result = CharsetDetector.DetectFromBytes(buf);
Assert.AreEqual(CodepageName.UTF16_BE, result.Detected.EncodingName);
Assert.AreEqual(1.0f, result.Detected.Confidence);
Assert.IsTrue(result.Detected.HasBOM);
}

[Test]
Expand All @@ -143,6 +150,7 @@ public void TestBomUTF16_BE()
var result = CharsetDetector.DetectFromBytes(buf);
Assert.AreEqual(CodepageName.UTF16_BE, result.Detected.EncodingName);
Assert.AreEqual(1.0f, result.Detected.Confidence);
Assert.IsTrue(result.Detected.HasBOM);
}

[Test]
Expand All @@ -154,6 +162,7 @@ public void TestBomX_ISO_10646_UCS_4_3412()
var result = CharsetDetector.DetectFromBytes(buf);
Assert.AreEqual(CodepageName.X_ISO_10646_UCS_4_3412, result.Detected.EncodingName);
Assert.AreEqual(1.0f, result.Detected.Confidence);
Assert.IsTrue(result.Detected.HasBOM);
}

[Test]
Expand All @@ -165,6 +174,7 @@ public void TestBomX_ISO_10646_UCS_4_2143()
var result = CharsetDetector.DetectFromBytes(buf);
Assert.AreEqual(CodepageName.X_ISO_10646_UCS_4_2143, result.Detected.EncodingName);
Assert.AreEqual(1.0f, result.Detected.Confidence);
Assert.IsTrue(result.Detected.HasBOM);
}

[Test]
Expand All @@ -174,15 +184,17 @@ public void Test2byteArrayBomUTF16_LE()
var result = CharsetDetector.DetectFromBytes(buf);
Assert.AreEqual(CodepageName.UTF16_LE, result.Detected.EncodingName);
Assert.AreEqual(1.0f, result.Detected.Confidence);
Assert.IsTrue(result.Detected.HasBOM);
}

[Test]
public void TestBomUTF16_LE()
{
byte[] buf = { 0xFF, 0xFE, 0x68, 0x00, 0x65, 0x00 };
var result = CharsetDetector.DetectFromBytes(buf);
Assert.AreEqual(CodepageName.UTF16_LE, result.Detected.EncodingName);
Assert.AreEqual(1.0f, result.Detected.Confidence);
Assert.IsTrue(result.Detected.HasBOM);
}

[Test]
Expand All @@ -192,6 +204,7 @@ public void TestBomUTF32_BE()
var result = CharsetDetector.DetectFromBytes(buf);
Assert.AreEqual(CodepageName.UTF32_BE, result.Detected.EncodingName);
Assert.AreEqual(1.0f, result.Detected.Confidence);
Assert.IsTrue(result.Detected.HasBOM);
}

[Test]
Expand All @@ -201,6 +214,7 @@ public void TestBomUTF32_LE()
var result = CharsetDetector.DetectFromBytes(buf);
Assert.AreEqual(CodepageName.UTF32_LE, result.Detected.EncodingName);
Assert.AreEqual(1.0f, result.Detected.Confidence);
Assert.IsTrue(result.Detected.HasBOM);
}

[Test]
Expand All @@ -210,26 +224,27 @@ public void TestIssue3()
var result = CharsetDetector.DetectFromBytes(buf);
Assert.AreEqual(CodepageName.ASCII, result.Detected.EncodingName);
Assert.AreEqual(1.0f, result.Detected.Confidence);
Assert.IsFalse(result.Detected.HasBOM);
}

[Test]
public void TestOutOfRange()
{

byte[] buf = Encoding.UTF8.GetBytes("3");
var result = CharsetDetector.DetectFromBytes(buf);
Assert.AreEqual(CodepageName.ASCII, result.Detected.EncodingName);
Assert.AreEqual(1.0f, result.Detected.Confidence);
Assert.IsFalse(result.Detected.HasBOM);
}

[Test]
public void TestOutOfRange2()
{

byte[] buf = Encoding.UTF8.GetBytes("1234567890");
var result = CharsetDetector.DetectFromBytes(buf);
Assert.AreEqual(CodepageName.ASCII, result.Detected.EncodingName);
Assert.AreEqual(1.0f, result.Detected.Confidence);
Assert.IsFalse(result.Detected.HasBOM);
}

[Test]
Expand All @@ -239,6 +254,7 @@ public void TestSingleChar()
var result = CharsetDetector.DetectFromBytes(buf);
Assert.AreEqual(CodepageName.ASCII, result.Detected.EncodingName);
Assert.AreEqual(1, result.Detected.Confidence);
Assert.IsFalse(result.Detected.HasBOM);
}
}
}