From b20b4fdd9bfc4c428403be7025edeede518be03d Mon Sep 17 00:00:00 2001 From: samogot Date: Mon, 24 Jul 2017 19:53:16 +0300 Subject: [PATCH] Escape string values for double quote character (") in index-tool dump-json Double quote character may appear in custom string literal suffixes definition (operator"") --- index-tool/main.cc | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/index-tool/main.cc b/index-tool/main.cc index 37b0da3..62e02cf 100644 --- a/index-tool/main.cc +++ b/index-tool/main.cc @@ -1,4 +1,5 @@ #include +#include #include #include "../libindexdb/FileIo.h" @@ -31,6 +32,21 @@ static void dump(const indexdb::Index &index) } } +static std::string escapeQuotes(const std::string& str) { + std::stringstream stream; + for (auto c : str) { + switch(c) { + case '"': + stream << "\\\""; + break; + default: + stream << c; + break; + } + } + return stream.str(); +} + static void dumpJson(const indexdb::Index &index) { std::cout << "{" << std::endl; @@ -43,7 +59,7 @@ static void dumpJson(const indexdb::Index &index) const indexdb::StringTable *table = index.stringTable(name); for (size_t stringIndex = 0, stringCount = table->size(); stringIndex < stringCount; ++stringIndex) { - std::cout << " \"" << table->item(stringIndex) << "\""; + std::cout << " \"" << escapeQuotes(table->item(stringIndex)) << "\""; if (stringIndex + 1 < stringCount) std::cout << ','; std::cout << std::endl; @@ -92,7 +108,7 @@ static void dumpJson(const indexdb::Index &index) columnStringTables[columnIndex]; if (stringTable != NULL) { std::cout << '"' - << stringTable->item(tableRow[columnIndex]) + << escapeQuotes(stringTable->item(tableRow[columnIndex])) << '"'; } else { std::cout << tableRow[columnIndex];