Skip to content

Commit 1e8ee57

Browse files
committed
keep lastCh intact when using FileStream::peek()
1 parent 1f8d856 commit 1e8ee57

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

simplecpp.cpp

+14-9
Original file line numberDiff line numberDiff line change
@@ -382,31 +382,36 @@ class FileStream : public simplecpp::TokenList::Stream {
382382
}
383383

384384
virtual int get() {
385-
lastCh = fgetc(file);
385+
lastStatus = lastCh = fgetc(file);
386386
return lastCh;
387387
}
388388
virtual int peek() {
389-
const int ch = get();
390-
unget();
389+
// keep lastCh intact
390+
const int ch = fgetc(file);
391+
unget_internal(ch);
391392
return ch;
392393
}
393394
virtual void unget() {
395+
unget_internal(lastCh);
396+
}
397+
virtual bool good() {
398+
return lastStatus != EOF;
399+
}
400+
401+
private:
402+
void unget_internal(int ch) {
394403
if (isUtf16) {
395404
// TODO: use ungetc() as well
396405
// UTF-16 has subsequent unget() calls
397406
fseek(file, -1, SEEK_CUR);
398407
}
399408
else
400-
ungetc(lastCh, file);
401-
402-
}
403-
virtual bool good() {
404-
return lastCh != EOF;
409+
ungetc(ch, file);
405410
}
406411

407-
private:
408412
FILE *file;
409413
int lastCh;
414+
int lastStatus;
410415
};
411416

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

0 commit comments

Comments
 (0)