moved BOM detection to separate methods.

This commit is contained in:
morkt 2016-12-03 19:18:59 +04:00
parent 0b12c7bcef
commit 7983cd0674

View File

@ -114,29 +114,45 @@ namespace JustView
var enc = Encoding.Default; var enc = Encoding.Default;
if (3 == file.Read (m_test_buf, 0, 3)) 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; enc = Encoding.UTF8;
else if (0xfe == m_test_buf[0] && 0xff == m_test_buf[1]) else if (IsUTF16BE())
enc = Encoding.BigEndianUnicode; enc = Encoding.BigEndianUnicode;
else if (0xff == m_test_buf[0] && 0xfe == m_test_buf[1]) else if (IsUTF16LE())
enc = Encoding.Unicode; enc = Encoding.Unicode;
} }
file.Position = 0; file.Position = 0;
return enc; 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) public bool IsTextFile (Stream file)
{ {
int read = file.Read (m_test_buf, 0, m_test_buf.Length); int read = file.Read (m_test_buf, 0, m_test_buf.Length);
file.Position = 0; file.Position = 0;
if (read > 3 && (IsUTF8() || IsUTF16LE() || IsUTF16BE()))
return true;
bool found_eol = false; bool found_eol = false;
for (int i = 0; i < read; ++i) for (int i = 0; i < read; ++i)
{ {
byte c = m_test_buf[i]; byte c = m_test_buf[i];
if (c < 9 || (c > 0x0d && c < 0x1a) || (c > 0x1b && c < 0x20)) if (c < 9 || (c > 0x0d && c < 0x1a) || (c > 0x1b && c < 0x20))
return false; return false;
if (!found_eol && 0x0a == c) found_eol = found_eol || 0x0A == c;
found_eol = true;
} }
return found_eol || read < 80; return found_eol || read < 80;
} }