Skip to content

Commit fa5e7e3

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

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
@@ -300,6 +300,8 @@ class simplecpp::TokenList::Stream {
300300

301301
protected:
302302
void init() {
303+
// initialize since we use peek() in getAndSkipBOM()
304+
isUtf16 = false;
303305
bom = getAndSkipBOM();
304306
isUtf16 = (bom == 0xfeff || bom == 0xfffe);
305307
}
@@ -338,6 +340,7 @@ class simplecpp::TokenList::Stream {
338340
}
339341

340342
unsigned short bom;
343+
protected:
341344
bool isUtf16;
342345
};
343346

@@ -389,7 +392,14 @@ class FileStream : public simplecpp::TokenList::Stream {
389392
return ch;
390393
}
391394
virtual void unget() {
392-
ungetc(lastCh, file);
395+
if (isUtf16) {
396+
// TODO: use ungetc() as well
397+
// UTF-16 has subsequent unget() calls
398+
fseek(file, -1, SEEK_CUR);
399+
}
400+
else
401+
ungetc(lastCh, file);
402+
393403
}
394404
virtual bool good() {
395405
return lastCh != EOF;

0 commit comments

Comments
 (0)