Visitar URL original
Handle multiline strings better · dummyunit/simplecpp@a4ee7a4 · GitHub
Skip to content

Commit a4ee7a4

Browse files
committed
Handle multiline strings better
1 parent f217a06 commit a4ee7a4

2 files changed

Lines changed: 19 additions & 2 deletions

File tree

‎simplecpp.cpp‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,18 @@ void simplecpp::TokenList::readfile(std::istream &istr, const std::string &filen
528528
if (currentToken.size() < 2U)
529529
// TODO report
530530
return;
531+
532+
std::string s = currentToken;
533+
std::string::size_type pos;
534+
while ((pos = s.find_first_of("\r\n")) != std::string::npos) {
535+
s.erase(pos,1);
536+
}
537+
538+
push_back(new Token(s, location)); // push string without newlines
539+
540+
location.adjust(currentToken);
541+
542+
continue;
531543
}
532544

533545
else {
@@ -899,8 +911,12 @@ std::string simplecpp::TokenList::readUntil(std::istream &istr, const Location &
899911
while (ch != end && ch != '\r' && ch != '\n' && istr.good()) {
900912
ch = (unsigned char)istr.get();
901913
ret += ch;
902-
if (ch == '\\')
903-
ret += (unsigned char)istr.get();
914+
if (ch == '\\') {
915+
const char next = (unsigned char)istr.get();
916+
if (next == '\r' || next == '\n')
917+
ret.erase(ret.size()-1U);
918+
ret += next;
919+
}
904920
}
905921

906922
if (!istr.good() || ch != end) {

‎test.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,6 +1031,7 @@ void readfile_string()
10311031
const char code[] = "A = \"abc\'def\"";
10321032
ASSERT_EQUALS("A = \"abc\'def\"", readfile(code));
10331033
ASSERT_EQUALS("( \"\\\\\\\\\" )", readfile("(\"\\\\\\\\\")"));
1034+
ASSERT_EQUALS("x = \"a b\"\n;", readfile("x=\"a\\\n b\";"));
10341035
}
10351036

10361037
void readfile_rawstring()

0 commit comments

Comments
 (0)