MCPcopy Create free account
hub / github.com/KhronosGroup/SPIRV-Tools / ExtractStringLiteral

Function ExtractStringLiteral

tools/objdump/extract_source.cpp:38–63  ·  view source on GitHub ↗

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.

Source from the content-addressed store, hash-verified

36// Returns `true` if the extraction succeeded.
37// `output` value is undefined if false is returned.
38spv_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
65spv_result_t extractOpString(const spv_position_t& loc,
66 const spv_parsed_instruction_t& instruction,

Callers 3

extractOpStringFunction · 0.85
extractOpSourceContinuedFunction · 0.85
extractOpSourceFunction · 0.85

Calls 2

resizeMethod · 0.80
ErrorFunction · 0.50

Tested by

no test coverage detected