MCPcopy Create free account
hub / github.com/avast/retdec / charToEscapedCStringRepr

Function charToEscapedCStringRepr

src/utils/string.cpp:181–218  ·  view source on GitHub ↗

* @brief Converts the given character into a representation usable in escaped C * strings. * * @param[in] c Character to be converted. * @param[in] charSize How large is actually the character in @a c (in bits)? * @param[int,out] lastWasHex Was the last character a hexadecimal escape? * @param[in] forceHex Convert @a c into a hexadecimal escape sequence, even if * it is

Source from the content-addressed store, hash-verified

179* it is printable.
180*/
181std::string charToEscapedCStringRepr(WideCharType c,
182 std::size_t charSize, bool &lastWasHex, bool forceHex) {
183 if (forceHex) {
184 lastWasHex = true;
185 return charToHexaStringRepr(c, charSize);
186 }
187
188 if (canBeAppendedLiterally(c, lastWasHex)) {
189 lastWasHex = false;
190 std::string repr;
191 if (c == '"' || c == '\\') {
192 repr += '\\';
193 }
194 repr += c;
195 return repr;
196 }
197
198 // The following list of escape sequences is based on
199 // http://en.cppreference.com/w/c/language/escape
200 //
201 // Note: Although ? (question mark) and ' (single quote) can be written as
202 // escape sequences (see the link above), we generate them normally
203 // because this makes the string more readable.
204 lastWasHex = false;
205 switch (c) {
206 case '\a': return "\\a";
207 case '\b': return "\\b";
208 case '\f': return "\\f";
209 case '\n': return "\\n";
210 case '\r': return "\\r";
211 case '\t': return "\\t";
212 case '\v': return "\\v";
213 default: break;
214 }
215
216 lastWasHex = true;
217 return charToHexaStringRepr(c, charSize);
218}
219
220} // anonymous namespace
221

Callers 1

asEscapedCStringFunction · 0.85

Calls 2

charToHexaStringReprFunction · 0.85
canBeAppendedLiterallyFunction · 0.85

Tested by

no test coverage detected