Skip to content

Commit

Permalink
Merge pull request #438 from mmd-osm/patch/cleanup21
Browse files Browse the repository at this point in the history
Minor improvements escaping routines
  • Loading branch information
mmd-osm authored Aug 5, 2024
2 parents 641c8c7 + 26b4f73 commit 1dcc832
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 20 deletions.
39 changes: 24 additions & 15 deletions include/cgimap/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,27 +44,36 @@ inline size_t unicode_strlen(const std::string & s)
}


// TODO: Proper escaping function
inline std::string escape(std::string_view input)
{
inline std::string escape(std::string_view input) {

std::string result = "\"";
int n = 0;

for (char i : input)
{
if (i == '\"')
result += "\\";
else if (i == '\\')
result += "\\";
// count chars to be escaped
for (char c : input) {
if (c == '"' || c == '\\')
++n;
}

result += i;
}
std::string result;
result.reserve(input.size() + n + 2); // input size + # of escaped chars + 2 enclosing quotes

result += "\"";
result += '"';

return result;
}
if (n == 0) {
result += input;
} else {
for (char c : input) {
if (c == '"' || c == '\\')
result += '\\';

result += c;
}
}

result += '"';

return result;
}

// array_agg returns some curly brackets in the response. remove them for output
// TODO: find a better way to do this.
Expand Down
10 changes: 5 additions & 5 deletions src/backend/apidb/common_pgsql_selection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -484,13 +484,13 @@ std::vector<std::string> psql_array_to_vector(std::string_view str) {
for (unsigned int i = 1; i < str_size; i++) {
if (str[i] == ',') {
if (quotedValue) {
value += ",";
value += ',';
} else {
write = true;
}
} else if (str[i] == '\"') {
} else if (str[i] == '"') {
if (escaped) {
value += "\"";
value += '"';
escaped = false;
} else if (quotedValue) {
quotedValue = false;
Expand All @@ -499,14 +499,14 @@ std::vector<std::string> psql_array_to_vector(std::string_view str) {
}
} else if (str[i] == '\\') {
if (escaped) {
value += "\\";
value += '\\';
escaped = false;
} else {
escaped = true;
}
} else if (str[i] == '}') {
if (quotedValue) {
value += "}";
value += '}';
} else {
write = true;
}
Expand Down

0 comments on commit 1dcc832

Please sign in to comment.