#include "CharStrmReader.h" #include "../db/assert.h" namespace nw4hbm { namespace ut { inline bool IsSJISLeadByte(u8 c) { return (0x81 <= c && c < 0xA0) || 0xE0 <= c; } u16 CharStrmReader::ReadNextCharUTF8() { NW4HBM_ASSERT_VALID_PTR(76, this); NW4HBM_ASSERT_VALID_PTR(77, mCharStrm); NW4HBM_ASSERT(79, (GetChar() & 0xC0) != 0x80); u16 code; if ((GetChar(0) & 0x80) == 0x00) { // 1-byte UTF-8 sequence code = GetChar(0); StepStrm(1); } else if ((GetChar(0) & 0xE0) == 0xC0) { // 2-byte UTF-8 sequence code = (GetChar(0) & 0x1f) << 6 | (GetChar(1) & 0x3f); StepStrm(2); } else { // 3-byte UTF-8 sequence NW4HBM_ASSERT(100, (GetChar() & 0xF0) == 0xE0); /* technical ERRATUM: the mask of GetChar(0) should be 0x0f */ code = (GetChar(0) & 0x1f) << 12 | (GetChar(1) & 0x3f) << 6 | (GetChar(2) & 0x3f); StepStrm(3); } /* NOTE: 4-byte to 7-byte UTF-8 sequences usually encode code points outside * of the BMP; I think sticking to the BMP is fine here. */ return code; } u16 CharStrmReader::ReadNextCharUTF16() { NW4HBM_ASSERT_VALID_PTR(129, this); NW4HBM_ASSERT_VALID_PTR(130, mCharStrm); NW4HBM_ASSERT_ALIGN2(131, mCharStrm); u16 code = GetChar(0); StepStrm(1); return code; } u16 CharStrmReader::ReadNextCharCP1252() { NW4HBM_ASSERT_VALID_PTR(155, this); NW4HBM_ASSERT_VALID_PTR(156, mCharStrm); u16 code = GetChar(0); StepStrm(1); return code; } u16 CharStrmReader::ReadNextCharSJIS() { NW4HBM_ASSERT_VALID_PTR(180, this); NW4HBM_ASSERT_VALID_PTR(181, mCharStrm); u16 code; if (IsSJISLeadByte(GetChar(0))) { code = GetChar(0) << 8 | GetChar(1); StepStrm(2); } else { code = GetChar(0); StepStrm(1); } return code; } } // namespace ut } // namespace nw4hbm