@@ -295,68 +295,58 @@ class simplecpp::TokenList::Stream {
295295public:
296296 virtual ~Stream () = default ;
297297
298- virtual int get () = 0;
299- virtual int peek () = 0;
300- virtual void unget () = 0;
301- virtual bool good () = 0;
302-
298+ /* *
299+ * Read the next character. CR and CR LF are returned as LF.
300+ * At the end of the input, EOF converted to unsigned char is returned
301+ * and isGood () becomes false.
302+ */
303303 unsigned char readChar () {
304- auto ch = static_cast <unsigned char >(get ());
305-
306- // For UTF-16 encoded files the BOM is 0xfeff/0xfffe. If the
307- // character is non-ASCII character then replace it with 0xff
308- if (isUtf16) {
309- const auto ch2 = static_cast <unsigned char >(get ());
310- const int ch16 = makeUtf16Char (ch, ch2);
311- ch = static_cast <unsigned char >(((ch16 >= 0x80 ) ? 0xff : ch16));
304+ int ch = getUnit ();
305+ if (ch == EOF ) {
306+ atEnd = true ;
307+ lastChar = EOF ;
308+ return static_cast <unsigned char >(EOF );
312309 }
313310
314311 // Handling of newlines..
315312 if (ch == ' \r ' ) {
316313 ch = ' \n ' ;
317-
318- int ch2 = get ();
319- if (isUtf16) {
320- const int c2 = get ();
321- ch2 = makeUtf16Char (ch2, c2);
322- }
323-
324- if (ch2 != ' \n ' )
325- ungetChar ();
314+ const int ch2 = getUnit ();
315+ if (ch2 != ' \n ' && ch2 != EOF )
316+ buffer.push_back (static_cast <char >(ch2));
326317 }
327318
328- return ch;
319+ lastChar = ch;
320+ return static_cast <unsigned char >(ch);
329321 }
330322
323+ /* * The next character, without reading it. EOF at the end of the input. */
331324 int peekChar () {
332- int ch = peek ();
325+ const int ch = getUnit ();
333326 if (ch == EOF )
334- return ch;
335-
336- // For UTF-16 encoded files the BOM is 0xfeff/0xfffe. If the
337- // character is non-ASCII character then replace it with 0xff
338- if (isUtf16) {
339- (void )get ();
340- const auto ch2 = static_cast <unsigned char >(peek ());
341- unget ();
342- const int ch16 = makeUtf16Char (ch, ch2);
343- ch = (ch16 >= 0x80 ) ? 0xff : ch16;
344- }
345-
346- // Handling of newlines..
347- if (ch == ' \r ' )
348- ch = ' \n ' ;
349-
350- return ch;
327+ return EOF ;
328+ buffer.push_back (static_cast <char >(ch));
329+ return (ch == ' \r ' ) ? ' \n ' : ch;
351330 }
352331
332+ /* * Put back the character that was last read with readChar() */
353333 void ungetChar () {
354- unget ();
355- if (isUtf16)
356- unget ();
334+ if (lastChar != EOF )
335+ buffer.push_back (static_cast <char >(lastChar));
336+ lastChar = EOF ;
337+ }
338+
339+ /* * false when readChar() has reached the end of the input */
340+ bool isGood () const {
341+ return !atEnd;
357342 }
358343
359344protected:
345+ virtual int get () = 0;
346+ virtual int peek () = 0;
347+ virtual void unget () = 0;
348+ virtual bool good () = 0;
349+
360350 void init () {
361351 // initialize since we use peek() in getAndSkipBOM()
362352 isUtf16 = false ;
@@ -365,6 +355,35 @@ class simplecpp::TokenList::Stream {
365355 }
366356
367357private:
358+ /* *
359+ * Read the next character from the input, or take it from the buffer.
360+ * Newlines are not converted.
361+ */
362+ int getUnit () {
363+ if (!buffer.empty ()) {
364+ const auto ch = static_cast <unsigned char >(buffer.back ());
365+ buffer.pop_back ();
366+ return ch;
367+ }
368+ if (inputEnd)
369+ return EOF ;
370+ const int ch = get ();
371+ if (ch == EOF ) {
372+ inputEnd = true ;
373+ return EOF ;
374+ }
375+ if (!isUtf16)
376+ return ch;
377+
378+ // For UTF-16 encoded files the BOM is 0xfeff/0xfffe. If the
379+ // character is non-ASCII character then replace it with 0xff
380+ const int ch2 = get ();
381+ if (ch2 == EOF )
382+ inputEnd = true ;
383+ const int ch16 = makeUtf16Char (static_cast <unsigned char >(ch), static_cast <unsigned char >(ch2));
384+ return (ch16 >= 0x80 ) ? 0xff : ch16;
385+ }
386+
368387 inline int makeUtf16Char (const unsigned char ch, const unsigned char ch2) const {
369388 return (bom == 0xfeff ) ? (ch<<8 | ch2) : (ch2<<8 | ch);
370389 }
@@ -400,6 +419,15 @@ class simplecpp::TokenList::Stream {
400419 }
401420
402421 unsigned short bom;
422+
423+ /* * characters that are read but not used yet. The next character is at the back. */
424+ std::string buffer;
425+ /* * the last character returned by readChar(), EOF if ungetChar() has nothing to put back */
426+ int lastChar{EOF };
427+ /* * readChar() has returned EOF */
428+ bool atEnd{false };
429+ /* * get() has returned EOF */
430+ bool inputEnd{false };
403431protected:
404432 bool isUtf16;
405433};
@@ -738,7 +766,7 @@ static simplecpp::TokenString readNameOrNumber(simplecpp::TokenList::Stream &str
738766{
739767 simplecpp::TokenString currentToken;
740768 const bool num = !!std::isdigit (ch);
741- while (stream.good () && isNameChar (ch)) {
769+ while (stream.isGood () && isNameChar (ch)) {
742770 currentToken += ch;
743771 ch = stream.readChar ();
744772 if (num && ch==' \' ' && isNameChar (stream.peekChar ()))
@@ -764,17 +792,17 @@ static simplecpp::TokenString readNameOrNumber(simplecpp::TokenList::Stream &str
764792static simplecpp::TokenString readLineComment (simplecpp::TokenList::Stream &stream, unsigned char ch, const simplecpp::Location &location, unsigned int &multiline, simplecpp::OutputList *outputList)
765793{
766794 simplecpp::TokenString currentToken;
767- while (stream.good () && ch != ' \n ' ) {
795+ while (stream.isGood () && ch != ' \n ' ) {
768796 currentToken += ch;
769797 ch = stream.readChar ();
770798 if (ch == ' \\ ' ) {
771799 simplecpp::TokenString tmp;
772800 char tmp_ch = ch;
773- while ((stream.good ()) && (tmp_ch == ' \\ ' || tmp_ch == ' ' || tmp_ch == ' \t ' )) {
801+ while ((stream.isGood ()) && (tmp_ch == ' \\ ' || tmp_ch == ' ' || tmp_ch == ' \t ' )) {
774802 tmp += tmp_ch;
775803 tmp_ch = stream.readChar ();
776804 }
777- if (!stream.good ()) {
805+ if (!stream.isGood ()) {
778806 break ;
779807 }
780808
@@ -803,7 +831,7 @@ simplecpp::TokenString simplecpp::TokenList::readBlockComment(Stream &stream, un
803831 TokenString currentToken = " /*" ;
804832 (void )stream.readChar ();
805833 unsigned char ch = stream.readChar ();
806- while (stream.good ()) {
834+ while (stream.isGood ()) {
807835 currentToken += ch;
808836 if (currentToken.size () >= 4U && endsWith (currentToken, COMMENT_END ))
809837 break ;
@@ -830,7 +858,7 @@ void simplecpp::TokenList::readErrorText(Stream &stream, unsigned char ch, Locat
830858{
831859 TokenString currentToken;
832860 char prev = ' ' ;
833- while (stream.good () && (prev == ' \\ ' || (ch != ' \r ' && ch != ' \n ' ))) {
861+ while (stream.isGood () && (prev == ' \\ ' || (ch != ' \r ' && ch != ' \n ' ))) {
834862 currentToken += ch;
835863 prev = ch;
836864 ch = stream.readChar ();
@@ -864,9 +892,9 @@ void simplecpp::TokenList::readfile(Stream &stream, const std::string &filename,
864892 const cppstd_t cppstd = getCppStd (dui.std );
865893
866894 Location location (fileIndex (filename), 1 , 1 );
867- while (stream.good ()) {
895+ while (stream.isGood ()) {
868896 unsigned char ch = stream.readChar ();
869- if (!stream.good ())
897+ if (!stream.isGood ())
870898 break ;
871899
872900 trailing_nl = false ;
@@ -1041,11 +1069,11 @@ void simplecpp::TokenList::readfile(Stream &stream, const std::string &filename,
10411069 currentToken = ch;
10421070 prefix.resize (prefix.size () - 1 );
10431071 ch = stream.readChar ();
1044- while (stream.good () && ch != ' (' && ch != ' \n ' ) {
1072+ while (stream.isGood () && ch != ' (' && ch != ' \n ' ) {
10451073 delim += ch;
10461074 ch = stream.readChar ();
10471075 }
1048- if (!stream.good () || ch == ' \n ' ) {
1076+ if (!stream.isGood () || ch == ' \n ' ) {
10491077 if (outputList) {
10501078 Output err{
10511079 Output::SYNTAX_ERROR ,
@@ -1057,7 +1085,7 @@ void simplecpp::TokenList::readfile(Stream &stream, const std::string &filename,
10571085 return ;
10581086 }
10591087 const std::string endOfRawString (' )' + delim + currentToken);
1060- while (stream.good () && (!endsWith (currentToken, endOfRawString) || currentToken.size () <= 1 ))
1088+ while (stream.isGood () && (!endsWith (currentToken, endOfRawString) || currentToken.size () <= 1 ))
10611089 currentToken += stream.readChar ();
10621090 if (!endsWith (currentToken, endOfRawString)) {
10631091 if (outputList) {
@@ -1700,7 +1728,7 @@ std::string simplecpp::TokenList::readUntil(Stream &stream, const Location &loca
17001728
17011729 bool backslash = false ;
17021730 char ch = 0 ;
1703- while (ch != end && ch != ' \r ' && ch != ' \n ' && stream.good ()) {
1731+ while (ch != end && ch != ' \r ' && ch != ' \n ' && stream.isGood ()) {
17041732 ch = stream.readChar ();
17051733 if (backslash && ch == ' \n ' ) {
17061734 ch = 0 ;
@@ -1728,7 +1756,7 @@ std::string simplecpp::TokenList::readUntil(Stream &stream, const Location &loca
17281756 }
17291757 }
17301758
1731- if (!stream.good () || ch != end) {
1759+ if (!stream.isGood () || ch != end) {
17321760 clear ();
17331761 if (outputList) {
17341762 Output err{
0 commit comments