Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ef342e4

Browse files
Escape special characters for GraphQL
1 parent 0c236d8 commit ef342e4

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

AmendTitleAndApplyLabel/issuegatherer.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,33 @@ bool IssueGatherer::matchAndAmendTitle(const std::regex &regex, std::string &tit
114114

115115
title = std::regex_replace(title, regex, "");
116116

117+
// We need to escape special characters(eg double quotes) for GraphQL
118+
// Strings literals need 2 levels of escape
119+
// Example title: Test "x"
120+
// Correctly escaped it should look like this
121+
// when the GraphQL query string is printed: Test \\\"x\\\"
122+
// But in C++ those backslashes must be escaped too!!!
123+
std::string buffer;
124+
buffer.reserve(title.size() + 6); // reserve 6 extra characters. Usually there shouldn't be more than one pair of doublequotes
125+
for (auto ch : title) {
126+
switch (ch) {
127+
case '\"':
128+
buffer.append("\\\\\\\"");
129+
break;
130+
case '\\':
131+
buffer.append("\\\\\\\\");
132+
break;
133+
case '/':
134+
buffer.append("\\\\\\/");
135+
break;
136+
default:
137+
buffer.append(1, ch);
138+
break;
139+
}
140+
}
141+
142+
title.swap(buffer);
143+
117144
return true;
118145
}
119146

0 commit comments

Comments
 (0)