Fixed length calculation and added a test.

This commit is contained in:
Andrey Semashev
2016-08-07 23:35:04 +03:00
parent 6abcc6cd09
commit 94f1e6b0c6
2 changed files with 12 additions and 2 deletions

View File

@ -204,7 +204,7 @@ BOOST_UTF8_DECL int utf8_codecvt_facet::do_length(
last_octet_count = (get_octet_count(*from_next));
++char_count;
}
return static_cast<int>(from_next-from_end);
return static_cast<int>(from_next-from);
}
BOOST_UTF8_DECL unsigned int utf8_codecvt_facet::get_octet_count(

View File

@ -29,7 +29,7 @@ namespace std{
using ::wcslen;
#if !defined(UNDER_CE) && !defined(__PGIC__)
using ::w_int;
#endif
#endif
} // namespace std
#endif
@ -251,6 +251,16 @@ test_main(int /* argc */, char * /* argv */[]) {
)
);
}
// Test length calculation
{
std::codecvt<wchar_t, char, std::mbstate_t> const& fac = std::use_facet< std::codecvt<wchar_t, char, std::mbstate_t> >(utf8_locale);
std::mbstate_t mbs = std::mbstate_t();
const int utf8_len = sizeof(td::utf8_encoding) / sizeof(*td::utf8_encoding);
int res = fac.length(mbs, reinterpret_cast< const char* >(td::utf8_encoding), reinterpret_cast< const char* >(td::utf8_encoding + utf8_len), ~static_cast< std::size_t >(0u));
BOOST_TEST_EQ(utf8_len, res);
}
return EXIT_SUCCESS;
}