Skip to content

Commit

Permalink
Merge pull request #3413 from jcking/cpp-string-utils
Browse files Browse the repository at this point in the history
[C++] Improve whitespace escaping
  • Loading branch information
parrt authored Dec 23, 2021
2 parents a6a1e11 + 0783936 commit afbd285
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 31 deletions.
2 changes: 0 additions & 2 deletions runtime/Cpp/runtime/src/ANTLRFileStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
* can be found in the LICENSE.txt file in the project root.
*/

#include "support/StringUtils.h"

#include "ANTLRFileStream.h"

using namespace antlr4;
Expand Down
6 changes: 2 additions & 4 deletions runtime/Cpp/runtime/src/CommonToken.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

#include "misc/Interval.h"

#include "support/StringUtils.h"
#include "support/CPPUtils.h"
#include "support/StringUtils.h"

#include "CommonToken.h"

Expand Down Expand Up @@ -165,9 +165,7 @@ std::string CommonToken::toString(Recognizer *r) const {
}
std::string txt = getText();
if (!txt.empty()) {
antlrcpp::replaceAll(txt, "\n", "\\n");
antlrcpp::replaceAll(txt, "\r", "\\r");
antlrcpp::replaceAll(txt, "\t", "\\t");
txt = antlrcpp::escapeWhitespace(txt);
} else {
txt = "<no text>";
}
Expand Down
14 changes: 8 additions & 6 deletions runtime/Cpp/runtime/src/DefaultErrorStrategy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
#include "atn/RuleTransition.h"
#include "atn/ATN.h"
#include "atn/ATNState.h"
#include "support/StringUtils.h"
#include "Parser.h"
#include "CommonToken.h"
#include "Vocabulary.h"
#include "support/StringUtils.h"

#include "DefaultErrorStrategy.h"

Expand Down Expand Up @@ -292,11 +292,13 @@ size_t DefaultErrorStrategy::getSymbolType(Token *symbol) {
}

std::string DefaultErrorStrategy::escapeWSAndQuote(const std::string &s) const {
std::string result = s;
antlrcpp::replaceAll(result, "\n", "\\n");
antlrcpp::replaceAll(result, "\r","\\r");
antlrcpp::replaceAll(result, "\t","\\t");
return "'" + result + "'";
std::string result;
result.reserve(s.size() + 2);
result.push_back('\'');
antlrcpp::escapeWhitespace(result, s);
result.push_back('\'');
result.shrink_to_fit();
return result;
}

misc::IntervalSet DefaultErrorStrategy::getErrorRecoverySet(Parser *recognizer) {
Expand Down
1 change: 0 additions & 1 deletion runtime/Cpp/runtime/src/Lexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include "ANTLRErrorListener.h"
#include "support/CPPUtils.h"
#include "CommonToken.h"
#include "support/StringUtils.h"

#include "Lexer.h"

Expand Down
1 change: 0 additions & 1 deletion runtime/Cpp/runtime/src/RecognitionException.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

#include "atn/ATN.h"
#include "Recognizer.h"
#include "support/StringUtils.h"
#include "ParserRuleContext.h"
#include "misc/IntervalSet.h"

Expand Down
14 changes: 8 additions & 6 deletions runtime/Cpp/runtime/src/Recognizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
#include "ConsoleErrorListener.h"
#include "RecognitionException.h"
#include "support/CPPUtils.h"
#include "support/StringUtils.h"
#include "Token.h"
#include "atn/ATN.h"
#include "atn/ATNSimulator.h"
#include "support/CPPUtils.h"
#include "support/StringUtils.h"

#include "Vocabulary.h"

Expand Down Expand Up @@ -113,11 +113,13 @@ std::string Recognizer::getTokenErrorDisplay(Token *t) {
}
}

antlrcpp::replaceAll(s, "\n", "\\n");
antlrcpp::replaceAll(s, "\r","\\r");
antlrcpp::replaceAll(s, "\t", "\\t");

return "'" + s + "'";
std::string result;
result.reserve(s.size() + 2);
result.push_back('\'');
antlrcpp::escapeWhitespace(result, s);
result.push_back('\'');
result.shrink_to_fit();
return result;
}

void Recognizer::addErrorListener(ANTLRErrorListener *listener) {
Expand Down
1 change: 0 additions & 1 deletion runtime/Cpp/runtime/src/antlr4-runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@
#include "support/BitSet.h"
#include "support/Casts.h"
#include "support/CPPUtils.h"
#include "support/StringUtils.h"
#include "support/Guid.h"
#include "tree/AbstractParseTreeVisitor.h"
#include "tree/ErrorNode.h"
Expand Down
1 change: 0 additions & 1 deletion runtime/Cpp/runtime/src/atn/ATNDeserializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
#include "misc/IntervalSet.h"
#include "Exceptions.h"
#include "support/CPPUtils.h"
#include "support/StringUtils.h"
#include "support/Casts.h"

#include "atn/LexerCustomAction.h"
Expand Down
31 changes: 24 additions & 7 deletions runtime/Cpp/runtime/src/support/StringUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,32 @@

namespace antlrcpp {

void replaceAll(std::string& str, std::string_view from, std::string_view to) {
if (from.empty())
return;
std::string escapeWhitespace(std::string_view in) {
std::string out;
escapeWhitespace(out, in);
out.shrink_to_fit();
return out;
}

size_t start_pos = 0;
while ((start_pos = str.find(from, start_pos)) != std::string::npos) {
str.replace(start_pos, from.length(), to);
start_pos += to.length(); // In case 'to' contains 'from', like replacing 'x' with 'yx'.
std::string& escapeWhitespace(std::string& out, std::string_view in) {
out.reserve(in.size()); // Best case, no escaping.
for (const auto &c : in) {
switch (c) {
case '\t':
out.append("\\t");
break;
case '\r':
out.append("\\r");
break;
case '\n':
out.append("\\n");
break;
default:
out.push_back(c);
break;
}
}
return out;
}

} // namespace antrlcpp
4 changes: 3 additions & 1 deletion runtime/Cpp/runtime/src/support/StringUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

namespace antlrcpp {

void replaceAll(std::string &str, std::string_view from, std::string_view to);
std::string escapeWhitespace(std::string_view in);

std::string& escapeWhitespace(std::string& out, std::string_view in);

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#include "ANTLRInputStream.h"
#include "support/Arrays.h"
#include "Exceptions.h"
#include "support/StringUtils.h"
#include "support/CPPUtils.h"

#include "tree/pattern/ParseTreePatternMatcher.h"
Expand Down

0 comments on commit afbd285

Please sign in to comment.