mirror of
https://github.com/crskycode/GARbro.git
synced 2024-11-23 05:35:34 +08:00
moved BOM detection to separate methods.
This commit is contained in:
parent
0b12c7bcef
commit
7983cd0674
@ -114,29 +114,45 @@ namespace JustView
|
||||
var enc = Encoding.Default;
|
||||
if (3 == file.Read (m_test_buf, 0, 3))
|
||||
{
|
||||
if (0xef == m_test_buf[0] && 0xbb == m_test_buf[1] && 0xbf == m_test_buf[2])
|
||||
if (IsUTF8())
|
||||
enc = Encoding.UTF8;
|
||||
else if (0xfe == m_test_buf[0] && 0xff == m_test_buf[1])
|
||||
else if (IsUTF16BE())
|
||||
enc = Encoding.BigEndianUnicode;
|
||||
else if (0xff == m_test_buf[0] && 0xfe == m_test_buf[1])
|
||||
else if (IsUTF16LE())
|
||||
enc = Encoding.Unicode;
|
||||
}
|
||||
file.Position = 0;
|
||||
return enc;
|
||||
}
|
||||
|
||||
private bool IsUTF8 ()
|
||||
{
|
||||
return 0xEF == m_test_buf[0] && 0xBB == m_test_buf[1] && 0xBF == m_test_buf[2];
|
||||
}
|
||||
|
||||
private bool IsUTF16BE ()
|
||||
{
|
||||
return 0xFE == m_test_buf[0] && 0xFF == m_test_buf[1];
|
||||
}
|
||||
|
||||
private bool IsUTF16LE ()
|
||||
{
|
||||
return 0xFF == m_test_buf[0] && 0xFE == m_test_buf[1];
|
||||
}
|
||||
|
||||
public bool IsTextFile (Stream file)
|
||||
{
|
||||
int read = file.Read (m_test_buf, 0, m_test_buf.Length);
|
||||
file.Position = 0;
|
||||
if (read > 3 && (IsUTF8() || IsUTF16LE() || IsUTF16BE()))
|
||||
return true;
|
||||
bool found_eol = false;
|
||||
for (int i = 0; i < read; ++i)
|
||||
{
|
||||
byte c = m_test_buf[i];
|
||||
if (c < 9 || (c > 0x0d && c < 0x1a) || (c > 0x1b && c < 0x20))
|
||||
return false;
|
||||
if (!found_eol && 0x0a == c)
|
||||
found_eol = true;
|
||||
found_eol = found_eol || 0x0A == c;
|
||||
}
|
||||
return found_eol || read < 80;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user