Showing the changes to src/vm/utf8.cpp (1 of 6 files). Show all
Unified · Split
| @@ -54,28 +54,31 @@ utf8::decode_state_t disvm::runtime::utf8::decode_step(utf8::decode_state_t &sta · 53 unmodified lines | |||
| 54 | 54 | ||
| 55 | // Adopted from 'countCodePoints(uint8_t* s, size_t* count)' | 55 | // Adopted from 'countCodePoints(uint8_t* s, size_t* count)' |
| 56 | // See http://bjoern.hoehrmann.de/utf-8/decoder/dfa/ for details. | 56 | // See http://bjoern.hoehrmann.de/utf-8/decoder/dfa/ for details. |
| 57 | std::size_t disvm::runtime::utf8::count_codepoints(const uint8_t *str, const std::size_t max_len) | 57 | utf8::utf8_length_t disvm::runtime::utf8::count_codepoints(const uint8_t *str, const std::size_t max_len) |
| 58 | { | 58 | { |
| 59 | if (str == nullptr) | 59 | if (str == nullptr) |
| 60 | { | 60 | { |
| 61 | assert(max_len == 0); | 61 | assert(max_len == 0); |
| 62 | return std::size_t{ 0 }; | 62 | return{ 0, 0 }; |
| 63 | } | 63 | } |
| 64 | 64 | ||
| 65 | auto single_character = rune_t{}; | 65 | auto single_character = rune_t{}; |
| 66 | auto points = std::size_t{ 0 }; | 66 | auto points = std::size_t{ 0 }; |
| 67 | auto state = utf8::decode_state_t::accept; | 67 | auto state = utf8::decode_state_t::accept; |
| 68 | 68 | ||
| 69 | for (auto i = std::size_t{ 0 }; i < max_len; ++i) | 69 | auto last = str; |
| 70 | for (auto curr = last; curr < (str + max_len); ++curr) | ||
| 70 | { | 71 | { |
| 71 | if (utf8::decode_state_t::accept == utf8::decode_step(state, single_character, str[i])) | 72 | if (utf8::decode_state_t::accept == utf8::decode_step(state, single_character, *curr)) |
| 73 | { | ||
| 74 | last = curr; | ||
| 72 | points++; | 75 | points++; |
| 76 | } | ||
| 73 | } | 77 | } |
| 74 | 78 | ||
| 75 | if (state != utf8::decode_state_t::accept) | 79 | assert(str <= last); |
| 76 | throw invalid_utf8{}; | 80 | const auto byte_count = static_cast<std::size_t>(last - str + 1); |
| 77 | 81 | return{ points, byte_count }; | |
| 78 | return points; | ||
| 79 | } | 82 | } |
| 80 | 83 | ||
| 81 | std::size_t disvm::runtime::utf8::decode(const uint8_t *str, rune_t &codepoint) | 84 | std::size_t disvm::runtime::utf8::decode(const uint8_t *str, rune_t &codepoint) |