Extract a string literal from a given range. Copies all the characters from `begin` to the first '\0' it encounters, while removing escape patterns. Not finding a '\0' before reaching `end` fails the extraction. Returns `true` if the extraction succeeded. `output` value is undefined if false is returned.
| 36 | // Returns `true` if the extraction succeeded. |
| 37 | // `output` value is undefined if false is returned. |
| 38 | spv_result_t ExtractStringLiteral(const spv_position_t& loc, const char* begin, |
| 39 | const char* end, std::string* output) { |
| 40 | size_t sourceLength = std::distance(begin, end); |
| 41 | std::string escapedString; |
| 42 | escapedString.resize(sourceLength); |
| 43 | |
| 44 | size_t writeIndex = 0; |
| 45 | size_t readIndex = 0; |
| 46 | for (; readIndex < sourceLength; writeIndex++, readIndex++) { |
| 47 | const char read = begin[readIndex]; |
| 48 | if (read == '\0') { |
| 49 | escapedString.resize(writeIndex); |
| 50 | output->append(escapedString); |
| 51 | return SPV_SUCCESS; |
| 52 | } |
| 53 | |
| 54 | if (read == '\\') { |
| 55 | ++readIndex; |
| 56 | } |
| 57 | escapedString[writeIndex] = begin[readIndex]; |
| 58 | } |
| 59 | |
| 60 | spvtools::Error(spvtools::utils::CLIMessageConsumer, "", loc, |
| 61 | "Missing NULL terminator for literal string."); |
| 62 | return SPV_ERROR_INVALID_BINARY; |
| 63 | } |
| 64 | |
| 65 | spv_result_t extractOpString(const spv_position_t& loc, |
| 66 | const spv_parsed_instruction_t& instruction, |
no test coverage detected