Skip to content

Commit 0616938

Browse files
committed
keep lastCh intact when using FileStream::peek()
1 parent 924a328 commit 0616938

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
@@ -385,31 +385,36 @@ class FileStream : public simplecpp::TokenList::Stream {
385385
}
386386

387387
virtual int get() {
388-
lastCh = fgetc(file);
388+
lastStatus = lastCh = fgetc(file);
389389
return lastCh;
390390
}
391391
virtual int peek() {
392-
const int ch = get();
393-
unget();
392+
// keep lastCh intact
393+
const int ch = fgetc(file);
394+
unget_internal(ch);
394395
return ch;
395396
}
396397
virtual void unget() {
398+
unget_internal(lastCh);
399+
}
400+
virtual bool good() {
401+
return lastStatus != EOF;
402+
}
403+
404+
private:
405+
void unget_internal(int ch) {
397406
if (isUtf16) {
398407
// TODO: use ungetc() as well
399408
// UTF-16 has subsequent unget() calls
400409
fseek(file, -1, SEEK_CUR);
401410
}
402411
else
403-
ungetc(lastCh, file);
404-
405-
}
406-
virtual bool good() {
407-
return lastCh != EOF;
412+
ungetc(ch, file);
408413
}
409414

410-
private:
411415
FILE *file;
412416
int lastCh;
417+
int lastStatus;
413418
};
414419

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

0 commit comments

Comments
 (0)