146 AutoUTFInputStream(InputByteStream& is, UTFType type = kUTF8) : is_(&is), type_(type), hasBOM_(false) {
149 static const TakeFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(Take) };
150 takeFunc_ = f[type_];
151 current_ = takeFunc_(*is_);
154 UTFType GetType()
const {
return type_; }
155 bool HasBOM()
const {
return hasBOM_; }
157 Ch Peek()
const {
return current_; }
158 Ch Take() { Ch c = current_; current_ = takeFunc_(*is_);
return c; }
159 size_t Tell()
const {
return is_->Tell(); }
180 const unsigned char* c =
reinterpret_cast<const unsigned char *
>(is_->Peek4());
184 unsigned bom =
static_cast<unsigned>(c[0] | (c[1] << 8) | (c[2] << 16) | (c[3] << 24));
186 if (bom == 0xFFFE0000) { type_ = kUTF32BE; hasBOM_ =
true; is_->Take(); is_->Take(); is_->Take(); is_->Take(); }
187 else if (bom == 0x0000FEFF) { type_ = kUTF32LE; hasBOM_ =
true; is_->Take(); is_->Take(); is_->Take(); is_->Take(); }
188 else if ((bom & 0xFFFF) == 0xFFFE) { type_ = kUTF16BE; hasBOM_ =
true; is_->Take(); is_->Take(); }
189 else if ((bom & 0xFFFF) == 0xFEFF) { type_ = kUTF16LE; hasBOM_ =
true; is_->Take(); is_->Take(); }
190 else if ((bom & 0xFFFFFF) == 0xBFBBEF) { type_ = kUTF8; hasBOM_ =
true; is_->Take(); is_->Take(); is_->Take(); }
204 int pattern = (c[0] ? 1 : 0) | (c[1] ? 2 : 0) | (c[2] ? 4 : 0) | (c[3] ? 8 : 0);
206 case 0x08: type_ = kUTF32BE;
break;
207 case 0x0A: type_ = kUTF16BE;
break;
208 case 0x01: type_ = kUTF32LE;
break;
209 case 0x05: type_ = kUTF16LE;
break;
210 case 0x0F: type_ = kUTF8;
break;
216 if (type_ == kUTF16LE || type_ == kUTF16BE)
RAPIDJSON_ASSERT(
sizeof(Ch) >= 2);
217 if (type_ == kUTF32LE || type_ == kUTF32BE)
RAPIDJSON_ASSERT(
sizeof(Ch) >= 4);
220 typedef Ch (*TakeFunc)(InputByteStream& is);
221 InputByteStream* is_;
249 if (type_ == kUTF16LE || type_ == kUTF16BE)
RAPIDJSON_ASSERT(
sizeof(Ch) >= 2);
250 if (type_ == kUTF32LE || type_ == kUTF32BE)
RAPIDJSON_ASSERT(
sizeof(Ch) >= 4);
252 static const PutFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(Put) };
259 UTFType GetType()
const {
return type_; }
261 void Put(Ch c) { putFunc_(*os_, c); }
262 void Flush() { os_->Flush(); }
276 typedef void (*PutBOMFunc)(OutputByteStream&);
277 static const PutBOMFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(PutBOM) };
281 typedef void (*PutFunc)(OutputByteStream&, Ch);
283 OutputByteStream* os_;