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/string.cpp (1 of 6 files). Show all

Unified · Split

src/vm/string.cpp+8−3
@@ -191,10 +191,15 @@ vm_string_t::vm_string_t(std::size_t encoded_str_len, const uint8_t *encoded_str · 190 unmodified lines
191191 , _mem{}
192192 {
193193 assert((encoded_str_len > 0 && encoded_str != nullptr) || (encoded_str_len == 0 && encoded_str == nullptr));
194− auto character_count = utf8::count_codepoints(encoded_str, encoded_str_len);
195− _character_size = (encoded_str_len != character_count) ? sizeof(rune_t) : sizeof(char);
194+ const auto utf8_length = utf8::count_codepoints(encoded_str, encoded_str_len);
196195
197− _length = static_cast<word_t>(character_count);
196+ // Check if the supplied string is completely valid UTF-8 - only valid UTF-8 strings can be DisVM strings.
197+ if (encoded_str_len != utf8_length.byte_count)
198+ throw invalid_utf8{};
199+
200+ _character_size = (encoded_str_len != utf8_length.codepoint_count) ? sizeof(rune_t) : sizeof(char);
201+
202+ _length = static_cast<word_t>(utf8_length.codepoint_count);
198203 _length_max = sizeof(_mem.local) / _character_size;
199204
200205 // Allocate memory based on character count and size