Skip to content

Commit e9e78b6

Browse files
refactor cpp_escape
1 parent 751dde4 commit e9e78b6

File tree

1 file changed

+28
-45
lines changed

1 file changed

+28
-45
lines changed

protoc-plugin/hpp_gen.cpp

Lines changed: 28 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -152,53 +152,36 @@ std::string cpp_escape(std::string_view src) {
152152
}
153153
std::string result;
154154
result.reserve(escaped_len);
155-
auto itr = std::back_inserter(result);
155+
static const char *const escapedChars[256] = {
156+
"\\\\000", "\\\\001", "\\\\002", "\\\\003", "\\\\004", "\\\\005", "\\\\006", "\\\a", "\\\b", "\\\t",
157+
"\\\n", "\\\v", "\\\f", "\\\r", "\\\\016", "\\\\017", "\\\\020", "\\\\021", "\\\\022", "\\\\023",
158+
"\\\\024", "\\\\025", "\\\\026", "\\\\027", "\\\\030", "\\\\031", "\\\\032", "\\\\033", "\\\\034", "\\\\035",
159+
"\\\\036", "\\\\037", " ", "!", "\\\"", "#", "$", "%", "&", "\\\'",
160+
"(", ")", "*", "+", ",", "-", ".", "/", "0", "1",
161+
"2", "3", "4", "5", "6", "7", "8", "9", ":", ";",
162+
"<", "=", ">", "\\\?", "@", "A", "B", "C", "D", "E",
163+
"F", "G", "H", "I", "J", "K", "L", "M", "N", "O",
164+
"P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y",
165+
"Z", "[", "\\\\", "]", "^", "_", "`", "a", "b", "c",
166+
"d", "e", "f", "g", "h", "i", "j", "k", "l", "m",
167+
"n", "o", "p", "q", "r", "s", "t", "u", "v", "w",
168+
"x", "y", "z", "{", "|", "}", "~", "\\\\177", "\\\\200", "\\\\201",
169+
"\\\\202", "\\\\203", "\\\\204", "\\\\205", "\\\\206", "\\\\207", "\\\\210", "\\\\211", "\\\\212", "\\\\213",
170+
"\\\\214", "\\\\215", "\\\\216", "\\\\217", "\\\\220", "\\\\221", "\\\\222", "\\\\223", "\\\\224", "\\\\225",
171+
"\\\\226", "\\\\227", "\\\\230", "\\\\231", "\\\\232", "\\\\233", "\\\\234", "\\\\235", "\\\\236", "\\\\237",
172+
"\\\\240", "\\\\241", "\\\\242", "\\\\243", "\\\\244", "\\\\245", "\\\\246", "\\\\247", "\\\\250", "\\\\251",
173+
"\\\\252", "\\\\253", "\\\\254", "\\\\255", "\\\\256", "\\\\257", "\\\\260", "\\\\261", "\\\\262", "\\\\263",
174+
"\\\\264", "\\\\265", "\\\\266", "\\\\267", "\\\\270", "\\\\271", "\\\\272", "\\\\273", "\\\\274", "\\\\275",
175+
"\\\\276", "\\\\277", "\\\\300", "\\\\301", "\\\\302", "\\\\303", "\\\\304", "\\\\305", "\\\\306", "\\\\307",
176+
"\\\\310", "\\\\311", "\\\\312", "\\\\313", "\\\\314", "\\\\315", "\\\\316", "\\\\317", "\\\\320", "\\\\321",
177+
"\\\\322", "\\\\323", "\\\\324", "\\\\325", "\\\\326", "\\\\327", "\\\\330", "\\\\331", "\\\\332", "\\\\333",
178+
"\\\\334", "\\\\335", "\\\\336", "\\\\337", "\\\\340", "\\\\341", "\\\\342", "\\\\343", "\\\\344", "\\\\345",
179+
"\\\\346", "\\\\347", "\\\\350", "\\\\351", "\\\\352", "\\\\353", "\\\\354", "\\\\355", "\\\\356", "\\\\357",
180+
"\\\\360", "\\\\361", "\\\\362", "\\\\363", "\\\\364", "\\\\365", "\\\\366", "\\\\367", "\\\\370", "\\\\371",
181+
"\\\\372", "\\\\373", "\\\\374", "\\\\375", "\\\\376", "\\\\377"};
156182

157183
for (const char c : src) {
158-
const std::size_t char_len = cpp_escaped_len(c);
159-
if (char_len == 1) {
160-
*itr++ = c;
161-
} else if (char_len == 2) {
162-
switch (c) {
163-
case '\n':
164-
*itr++ = '\\';
165-
*itr++ = 'n';
166-
break;
167-
case '\r':
168-
*itr++ = '\\';
169-
*itr++ = 'r';
170-
break;
171-
case '\t':
172-
*itr++ = '\\';
173-
*itr++ = 't';
174-
break;
175-
case '\"':
176-
*itr++ = '\\';
177-
*itr++ = '\"';
178-
break;
179-
case '\'':
180-
*itr++ = '\\';
181-
*itr++ = '\'';
182-
break;
183-
case '\\':
184-
*itr++ = '\\';
185-
*itr++ = '\\';
186-
break;
187-
case '?':
188-
*itr++ = '\\';
189-
*itr++ = '?';
190-
break;
191-
default:
192-
break;
193-
}
194-
} else {
195-
*itr++ = '\\';
196-
// NOLINTBEGIN(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers)
197-
*itr++ = static_cast<char>('0' + (static_cast<unsigned char>(c) / 64));
198-
*itr++ = static_cast<char>('0' + ((static_cast<unsigned char>(c) % 64) / 8));
199-
*itr++ = static_cast<char>('0' + (static_cast<unsigned char>(c) % 8));
200-
// NOLINTEND(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers)
201-
}
184+
result += escapedChars[static_cast<unsigned char>(c)];
202185
}
203186
return result;
204187
}

0 commit comments

Comments
 (0)