internal static Decoder AutoDetectEncoding(byte[] buffer, ref int index, int length) {
if (4 <= (length - index)) {
uint w = (uint)buffer[index + 0] << 24 | (uint)buffer[index + 1] << 16 | (uint)buffer[index + 2] << 8 | (uint)buffer[index + 3];
// see if it's a 4-byte encoding
switch (w) {
case 0xfefffeff:
index += 4;
return new Ucs4DecoderBigEngian();
case 0xfffefffe:
index += 4;
return new Ucs4DecoderLittleEndian();
case 0x3c000000:
goto case 0xfefffeff;
case 0x0000003c:
goto case 0xfffefffe;
}
w >>= 8;
if (w == 0xefbbbf) {
index += 3;
return Encoding.UTF8.GetDecoder();
}
w >>= 8;
switch (w) {
case 0xfeff:
index += 2;
return UnicodeEncoding.BigEndianUnicode.GetDecoder();
case 0xfffe:
index += 2;
return new UnicodeEncoding(false, false).GetDecoder();
case 0x3c00:
goto case 0xfeff;
case 0x003c:
goto case 0xfffe;
}
}
return null;
}
private int ReadChar() {