Skip to content

Commit

Permalink
escape % sign in subsitutionformatter
Browse files Browse the repository at this point in the history
Signed-off-by: Aidan Hahn <aidanhahn@datawire.io>
  • Loading branch information
aidanhahn committed Jan 7, 2022
1 parent c5c5d4c commit 6804b59
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
7 changes: 4 additions & 3 deletions source/common/formatter/substitution_formatter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <algorithm>
#include <climits>
#include <cstddef>
#include <cstdint>
#include <regex>
#include <string>
Expand Down Expand Up @@ -454,15 +455,15 @@ SubstitutionFormatParser::parse(const std::string& format,
std::vector<FormatterProviderPtr> formatters;
const std::regex command_w_args_regex(R"EOF(^%([A-Z]|[0-9]|_)+(\([^\)]*\))?(:[0-9]+)?(%))EOF");

for (size_t pos = 0; pos < format.length(); ++pos) {
for (size_t pos = 0; pos < format.size(); ++pos) {
if (format[pos] != '%') {
current_token += format[pos];
continue;
}

// escape '%%'
if (format.length() > pos+1) {
if (format[pos+1] == '%') {
if (format.size() > pos + 1) {
if (format[pos + 1] == '%') {
current_token += '%';
pos++;
continue;
Expand Down
4 changes: 2 additions & 2 deletions test/common/formatter/substitution_formatter_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3084,9 +3084,9 @@ TEST(SubstitutionFormatterTest, EscapingFormatParse) {

auto providers = SubstitutionFormatParser::parse("%%");

EXPECT_EQ(providers.size(), 1);
ASSERT_EQ(providers.size(), 1);
EXPECT_EQ("%", providers[0]->format(request_headers, response_headers, response_trailers,
stream_info, body));
stream_info, body));
}

TEST(SubstitutionFormatterTest, FormatterExtension) {
Expand Down

0 comments on commit 6804b59

Please sign in to comment.