* @brief Returns the constant's value as an escaped C string. */
| 1323 | * @brief Returns the constant's value as an escaped C string. |
| 1324 | */ |
| 1325 | std::string asEscapedCString(const WideStringType& value, std::size_t charSize) { |
| 1326 | // Keep track whether the last character was written as a hexadecimal |
| 1327 | // escape. |
| 1328 | bool lastWasHex = false; |
| 1329 | |
| 1330 | // When the string has any unprintable characters, convert all its |
| 1331 | // characters into hexadecimal escape sequences. |
| 1332 | bool forceHex = hasUnprintableChars(value); |
| 1333 | |
| 1334 | // However, if the only unprintable characters are zero bytes, do not force |
| 1335 | // the hexadecimal escape because |
| 1336 | // |
| 1337 | // "hello_world.f\x00" |
| 1338 | // |
| 1339 | // looks better than |
| 1340 | // |
| 1341 | // "\x68\x65\x6C\x6C\x6F\x5F\x77\x6F\x72\x6C\x64\x2E\x66\x00" |
| 1342 | // |
| 1343 | if (onlyUnprintableCharsAreZeroBytes(value)) { |
| 1344 | forceHex = false; |
| 1345 | } |
| 1346 | |
| 1347 | // Perform the conversion. |
| 1348 | std::string escapedCString; |
| 1349 | for (auto c : value) { |
| 1350 | escapedCString += charToEscapedCStringRepr(c, charSize, lastWasHex, forceHex); |
| 1351 | } |
| 1352 | return escapedCString; |
| 1353 | } |
| 1354 | |
| 1355 | /** |
| 1356 | * Remove comments from string. Comment must start with a single @c commentChar |