History

Implement seek() utfbytes() in Sys module. - Updated tests for utfbytes() - Updated utf8 API surface to support the general length case (codepoint _and_ byte) - Testing of seek() still outstanding Comment added regarding stack frame pointer in stack frames.

arr committed 9 years ago

Commit a6da1dc1 parent 1af55286 files · +94 −18Browse files

Showing the changes to src/vm/utf8.cpp (1 of 6 files). Show all

Unified · Split

src/vm/utf8.cpp+11−8
@@ -54,28 +54,31 @@ utf8::decode_state_t disvm::runtime::utf8::decode_step(utf8::decode_state_t &sta · 53 unmodified lines
5454
5555 // Adopted from 'countCodePoints(uint8_t* s, size_t* count)'
5656 // 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)
5858 {
5959 if (str == nullptr)
6060 {
6161 assert(max_len == 0);
62− return std::size_t{ 0 };
62+ return{ 0, 0 };
6363 }
6464
6565 auto single_character = rune_t{};
6666 auto points = std::size_t{ 0 };
6767 auto state = utf8::decode_state_t::accept;
6868
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)
7071 {
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;
7275 points++;
76+ }
7377 }
7478
75− if (state != utf8::decode_state_t::accept)
76− throw invalid_utf8{};
77−
78− return points;
79+ assert(str <= last);
80+ const auto byte_count = static_cast<std::size_t>(last - str + 1);
81+ return{ points, byte_count };
7982 }
8083
8184 std::size_t disvm::runtime::utf8::decode(const uint8_t *str, rune_t &codepoint)