diff --git a/include/xlnt/utils/utf8string.hpp b/include/xlnt/utils/utf8string.hpp index f4f8ee59..f83e01ba 100644 --- a/include/xlnt/utils/utf8string.hpp +++ b/include/xlnt/utils/utf8string.hpp @@ -36,8 +36,8 @@ class XLNT_CLASS utf8string public: static utf8string from_utf8(const std::string &s); static utf8string from_latin1(const std::string &s); - static utf8string from_utf16(const std::string &s); - static utf8string from_utf32(const std::string &s); + static utf8string from_utf16(const std::vector &s); + static utf8string from_utf32(const std::vector &s); static bool is_valid(const std::string &s); diff --git a/source/cell/cell.cpp b/source/cell/cell.cpp index eb204550..e3a759d1 100644 --- a/source/cell/cell.cpp +++ b/source/cell/cell.cpp @@ -141,53 +141,7 @@ std::string cell::check_string(const std::string &to_check) { return s; } - - auto wb_encoding = get_workbook().get_encoding(); - - //XXX: use utfcpp for this! - switch(wb_encoding) - { - case encoding::latin1: break; // all bytes are valid in latin1 - case encoding::ascii: - for (char c : s) - { - if (c < 0) - { - throw xlnt::unicode_decode_error(c); - } - } - break; - case encoding::utf8: - { - if(!utf8string::is_valid(s)) - { - throw xlnt::unicode_decode_error('0'); - } - break; - } - case encoding::utf16: - { - if(!utf8string::from_utf16(s).is_valid()) - { - throw xlnt::unicode_decode_error('0'); - } - break; - } - case encoding::utf32: - { - if(!utf8string::from_utf32(s).is_valid()) - { - throw xlnt::unicode_decode_error('0'); - } - break; - } - default: - // other encodings not supported yet - break; - } // switch(wb_encoding) - - // check encoding? - if (s.size() > 32767) + else if (s.size() > 32767) { s = s.substr(0, 32767); // max string length in Excel } diff --git a/source/cell/tests/test_cell.hpp b/source/cell/tests/test_cell.hpp index 4cd65ce7..9d83cf1e 100644 --- a/source/cell/tests/test_cell.hpp +++ b/source/cell/tests/test_cell.hpp @@ -53,6 +53,9 @@ public: cell.set_value("03:40:16"); TS_ASSERT(cell.get_value() == xlnt::time(3, 40, 16)); + cell.set_value("03:"); + TS_ASSERT_EQUALS(cell.get_value(), "03:"); + cell.set_value("03:40"); TS_ASSERT(cell.get_value() == xlnt::time(3, 40)); @@ -322,15 +325,7 @@ public: auto test_string = "Compound Value (" + std::string(1, pound) + ")"; return test_string; } - - void test_bad_encoding() - { - auto ws = wb.create_sheet(); - auto cell = ws[xlnt::cell_reference("A1")]; - TS_ASSERT_THROWS(cell.check_string(make_latin1_string()), xlnt::unicode_decode_error); - TS_ASSERT_THROWS(cell.set_value(make_latin1_string()), xlnt::unicode_decode_error); - } - + void test_good_encoding() { xlnt::workbook latin1_wb(xlnt::encoding::latin1); diff --git a/source/utils/tests/test_utf8string.hpp b/source/utils/tests/test_utf8string.hpp index 0e847bad..e1d82b47 100644 --- a/source/utils/tests/test_utf8string.hpp +++ b/source/utils/tests/test_utf8string.hpp @@ -24,13 +24,13 @@ public: void test_utf16() { - auto utf16_valid = xlnt::utf8string::from_utf16("abc"); + auto utf16_valid = xlnt::utf8string::from_utf16({ 'a', 'b', 'c' }); TS_ASSERT(utf16_valid.is_valid()); } void test_utf32() { - auto utf32_valid = xlnt::utf8string::from_utf32("abc"); + auto utf32_valid = xlnt::utf8string::from_utf32({ 'a', 'b', 'c' }); TS_ASSERT(utf32_valid.is_valid()); } }; diff --git a/source/utils/utf8string.cpp b/source/utils/utf8string.cpp index 8e4bd61b..1b089880 100644 --- a/source/utils/utf8string.cpp +++ b/source/utils/utf8string.cpp @@ -21,6 +21,7 @@ // @license: http://www.opensource.org/licenses/mit-license.php // @author: see AUTHORS file #include +#include namespace xlnt { @@ -40,7 +41,7 @@ utf8string utf8string::from_latin1(const std::string &s) return result; } -utf8string utf8string::from_utf16(const std::string &s) +utf8string utf8string::from_utf16(const std::vector &s) { utf8string result; utf8::utf16to8(s.begin(), s.end(), std::back_inserter(result.bytes_)); @@ -48,7 +49,7 @@ utf8string utf8string::from_utf16(const std::string &s) return result; } -utf8string utf8string::from_utf32(const std::string &s) +utf8string utf8string::from_utf32(const std::vector &s) { utf8string result; utf8::utf32to8(s.begin(), s.end(), std::back_inserter(result.bytes_));