mirror of
https://github.com/tfussell/xlnt.git
synced 2024-03-22 13:11:17 +08:00
parent
10c5781e6d
commit
d4b36b6582
|
@ -29,6 +29,7 @@
|
|||
#pragma clang diagnostic pop
|
||||
|
||||
#include <detail/unicode.hpp>
|
||||
#include <xlnt/utils/exceptions.hpp>
|
||||
|
||||
namespace xlnt {
|
||||
namespace detail {
|
||||
|
@ -69,5 +70,16 @@ std::string latin1_to_utf8(const std::string &latin1)
|
|||
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 xlnt
|
||||
|
|
|
@ -29,6 +29,7 @@ namespace detail {
|
|||
std::u16string utf8_to_utf16(const std::string &utf8_string);
|
||||
std::string utf16_to_utf8(const std::u16string &utf16_string);
|
||||
std::string latin1_to_utf8(const std::string &latin1);
|
||||
size_t string_length(const std::string &utf8_string);
|
||||
|
||||
} // namespace detail
|
||||
} // namespace xlnt
|
||||
|
|
|
@ -46,6 +46,7 @@
|
|||
#include <detail/implementations/cell_impl.hpp>
|
||||
#include <detail/implementations/workbook_impl.hpp>
|
||||
#include <detail/implementations/worksheet_impl.hpp>
|
||||
#include <detail/unicode.hpp>
|
||||
|
||||
namespace {
|
||||
|
||||
|
@ -234,7 +235,7 @@ void worksheet::title(const std::string &title)
|
|||
return;
|
||||
}
|
||||
// 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);
|
||||
}
|
||||
|
|
|
@ -104,6 +104,7 @@ public:
|
|||
register_test(test_clear_cell);
|
||||
register_test(test_clear_row);
|
||||
register_test(test_set_title);
|
||||
register_test(test_set_title_unicode);
|
||||
}
|
||||
|
||||
void test_new_worksheet()
|
||||
|
@ -1259,5 +1260,17 @@ public:
|
|||
xlnt_assert(ws1_title == ws1.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);
|
||||
}
|
||||
};
|
||||
static worksheet_test_suite x;
|
||||
static worksheet_test_suite x;
|
||||
|
|
Loading…
Reference in New Issue
Block a user