Fetches the next word from the given text stream starting from the given position. On success, writes the decoded word into *word and updates position to the location past the returned word. A word ends at the next comment or whitespace. However, double-quoted strings remain intact, and a backslash always escapes the next character.
| 95 | // A word ends at the next comment or whitespace. However, double-quoted |
| 96 | // strings remain intact, and a backslash always escapes the next character. |
| 97 | spv_result_t getWord(spv_text text, spv_position position, std::string* word) { |
| 98 | if (!text->str || !text->length) return SPV_ERROR_INVALID_TEXT; |
| 99 | if (!position) return SPV_ERROR_INVALID_POINTER; |
| 100 | |
| 101 | const size_t start_index = position->index; |
| 102 | |
| 103 | bool quoting = false; |
| 104 | bool escaping = false; |
| 105 | |
| 106 | // NOTE: Assumes first character is not white space! |
| 107 | while (true) { |
| 108 | if (position->index >= text->length) { |
| 109 | word->assign(text->str + start_index, text->str + position->index); |
| 110 | return SPV_SUCCESS; |
| 111 | } |
| 112 | const char ch = text->str[position->index]; |
| 113 | if (ch == '\\') { |
| 114 | escaping = !escaping; |
| 115 | } else { |
| 116 | switch (ch) { |
| 117 | case '"': |
| 118 | if (!escaping) quoting = !quoting; |
| 119 | break; |
| 120 | case ' ': |
| 121 | case ';': |
| 122 | case ',': |
| 123 | case '(': |
| 124 | case ')': |
| 125 | case '\t': |
| 126 | case '\n': |
| 127 | case '\r': |
| 128 | if (escaping || quoting) break; |
| 129 | word->assign(text->str + start_index, text->str + position->index); |
| 130 | return SPV_SUCCESS; |
| 131 | case '\0': { // NOTE: End of word found! |
| 132 | word->assign(text->str + start_index, text->str + position->index); |
| 133 | return SPV_SUCCESS; |
| 134 | } |
| 135 | default: |
| 136 | break; |
| 137 | } |
| 138 | escaping = false; |
| 139 | } |
| 140 | |
| 141 | position->column++; |
| 142 | position->index++; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | // Returns true if the characters in the text as position represent |
| 147 | // the start of an Opcode. |
no outgoing calls
no test coverage detected