Merge pull request #380 from kostasdizas/issue378-unicode_title

Added support for unicode sheet titles
This commit is contained in:
Thomas Fussell 2019-06-22 10:54:30 -04:00 committed by GitHub
commit 73b5a783a7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 29 additions and 2 deletions

View File

@ -29,6 +29,7 @@
#pragma clang diagnostic pop #pragma clang diagnostic pop
#include <detail/unicode.hpp> #include <detail/unicode.hpp>
#include <xlnt/utils/exceptions.hpp>
namespace xlnt { namespace xlnt {
namespace detail { namespace detail {
@ -69,5 +70,16 @@ std::string latin1_to_utf8(const std::string &latin1)
return utf8; return utf8;
} }
size_t string_length(const std::string &utf8_string)
{
auto end_it = utf8::find_invalid(utf8_string.begin(), utf8_string.end());
if (end_it != utf8_string.end())
{
throw xlnt::exception("Invalid UTF-8 encoding detected");
}
return utf8::distance(utf8_string.begin(), end_it);
}
} // namespace detail } // namespace detail
} // namespace xlnt } // namespace xlnt

View File

@ -29,6 +29,7 @@ namespace detail {
std::u16string utf8_to_utf16(const std::string &utf8_string); std::u16string utf8_to_utf16(const std::string &utf8_string);
std::string utf16_to_utf8(const std::u16string &utf16_string); std::string utf16_to_utf8(const std::u16string &utf16_string);
std::string latin1_to_utf8(const std::string &latin1); std::string latin1_to_utf8(const std::string &latin1);
size_t string_length(const std::string &utf8_string);
} // namespace detail } // namespace detail
} // namespace xlnt } // namespace xlnt

View File

@ -49,6 +49,7 @@
#include <detail/implementations/cell_impl.hpp> #include <detail/implementations/cell_impl.hpp>
#include <detail/implementations/workbook_impl.hpp> #include <detail/implementations/workbook_impl.hpp>
#include <detail/implementations/worksheet_impl.hpp> #include <detail/implementations/worksheet_impl.hpp>
#include <detail/unicode.hpp>
namespace { namespace {
@ -237,7 +238,7 @@ void worksheet::title(const std::string &title)
return; return;
} }
// excel limits worksheet titles to 31 characters // excel limits worksheet titles to 31 characters
if (title.empty() || title.length() > 31) if (title.empty() || detail::string_length(title) > 31)
{ {
throw invalid_sheet_title(title); throw invalid_sheet_title(title);
} }

View File

@ -104,6 +104,7 @@ public:
register_test(test_clear_cell); register_test(test_clear_cell);
register_test(test_clear_row); register_test(test_clear_row);
register_test(test_set_title); register_test(test_set_title);
register_test(test_set_title_unicode);
register_test(test_phonetics); register_test(test_phonetics);
register_test(test_insert_rows); register_test(test_insert_rows);
register_test(test_insert_columns); register_test(test_insert_columns);
@ -1267,6 +1268,18 @@ public:
xlnt_assert(ws2_title == ws2.title()); xlnt_assert(ws2_title == ws2.title());
} }
void test_set_title_unicode()
{
xlnt::workbook wb;
auto ws = wb.active_sheet();
// the 31 char limit also applies to 4-byte characters
const std::string test_long_utf8_title("巧みな外交は戦争を避ける助けとなる。");
xlnt_assert_throws_nothing(ws.title(test_long_utf8_title));
const std::string invalid_unicode("\xe6\x97\xa5\xd1\x88\xfa");
xlnt_assert_throws(ws.title(invalid_unicode),
xlnt::exception);
}
void test_phonetics() void test_phonetics()
{ {
xlnt::workbook wb; xlnt::workbook wb;