Skip to content

Commit d06accf

Browse files
committed
fixed FileStream::unget() with subsequent calls (i.e. UTF-16 encoding)
1 parent 6103b6c commit d06accf

1 file changed

Lines changed: 11 additions & 1 deletion

File tree

simplecpp.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,8 @@ class simplecpp::TokenList::Stream {
298298

299299
protected:
300300
void init() {
301+
// initialize since we use peek() in getAndSkipBOM()
302+
isUtf16 = false;
301303
bom = getAndSkipBOM();
302304
isUtf16 = (bom == 0xfeff || bom == 0xfffe);
303305
}
@@ -336,6 +338,7 @@ class simplecpp::TokenList::Stream {
336338
}
337339

338340
unsigned short bom;
341+
protected:
339342
bool isUtf16;
340343
};
341344

@@ -387,7 +390,14 @@ class FileStream : public simplecpp::TokenList::Stream {
387390
return ch;
388391
}
389392
virtual void unget() {
390-
ungetc(lastCh, file);
393+
if (isUtf16) {
394+
// TODO: use ungetc() as well
395+
// UTF-16 has subsequent unget() calls
396+
fseek(file, -1, SEEK_CUR);
397+
}
398+
else
399+
ungetc(lastCh, file);
400+
391401
}
392402
virtual bool good() {
393403
return lastCh != EOF;

0 commit comments

Comments
 (0)