Skip to content

Commit 8b5522e

Browse files
committed
keep lastCh intact when using FileStream::peek()
1 parent 5611856 commit 8b5522e

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

simplecpp.cpp

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -383,31 +383,36 @@ class FileStream : public simplecpp::TokenList::Stream {
383383
}
384384

385385
virtual int get() {
386-
lastCh = fgetc(file);
386+
lastStatus = lastCh = fgetc(file);
387387
return lastCh;
388388
}
389389
virtual int peek() {
390-
const int ch = get();
391-
unget();
390+
// keep lastCh intact
391+
const int ch = fgetc(file);
392+
unget_internal(ch);
392393
return ch;
393394
}
394395
virtual void unget() {
396+
unget_internal(lastCh);
397+
}
398+
virtual bool good() {
399+
return lastStatus != EOF;
400+
}
401+
402+
private:
403+
void unget_internal(int ch) {
395404
if (isUtf16) {
396405
// TODO: use ungetc() as well
397406
// UTF-16 has subsequent unget() calls
398407
fseek(file, -1, SEEK_CUR);
399408
}
400409
else
401-
ungetc(lastCh, file);
402-
403-
}
404-
virtual bool good() {
405-
return lastCh != EOF;
410+
ungetc(ch, file);
406411
}
407412

408-
private:
409413
FILE *file;
410414
int lastCh;
415+
int lastStatus;
411416
};
412417

413418
simplecpp::TokenList::TokenList(std::vector<std::string> &filenames) : frontToken(nullptr), backToken(nullptr), files(filenames) {}

0 commit comments

Comments
 (0)