Reads a UTF-16LE string and returns it at UTF-8.
| 172 | |
| 173 | // Reads a UTF-16LE string and returns it at UTF-8. |
| 174 | static aiString ReadString(StreamReaderLE *stream, uint32_t numWChars) { |
| 175 | if (nullptr == stream || 0 == numWChars) { |
| 176 | return aiString(); |
| 177 | } |
| 178 | |
| 179 | // Allocate buffers (max expansion is 1 byte -> 4 bytes for UTF-8) |
| 180 | std::vector<unsigned char> str; |
| 181 | str.reserve(numWChars * 4 + 1); |
| 182 | uint16_t *temp = new uint16_t[numWChars]; |
| 183 | for (uint32_t n = 0; n < numWChars; ++n) { |
| 184 | temp[n] = stream->GetU2(); |
| 185 | } |
| 186 | |
| 187 | // Convert it and NUL-terminate. |
| 188 | const uint16_t *start(temp), *end(temp + numWChars); |
| 189 | utf8::utf16to8(start, end, back_inserter(str)); |
| 190 | str[str.size() - 1] = '\0'; |
| 191 | |
| 192 | // Return the final string. |
| 193 | aiString result = aiString((const char *)&str[0]); |
| 194 | delete[] temp; |
| 195 | |
| 196 | return result; |
| 197 | } |
| 198 | |
| 199 | // ------------------------------------------------------------------------------------------------ |
| 200 | // Returns whether the class can handle the format of the given file. |
no test coverage detected