remove some unneccessary encoding stuff

This commit is contained in:
Thomas Fussell 2016-07-17 13:49:59 -04:00
parent 93a5bccac2
commit 101ba5e49f
5 changed files with 12 additions and 62 deletions

View File

@ -36,8 +36,8 @@ class XLNT_CLASS utf8string
public: public:
static utf8string from_utf8(const std::string &s); static utf8string from_utf8(const std::string &s);
static utf8string from_latin1(const std::string &s); static utf8string from_latin1(const std::string &s);
static utf8string from_utf16(const std::string &s); static utf8string from_utf16(const std::vector<std::uint16_t> &s);
static utf8string from_utf32(const std::string &s); static utf8string from_utf32(const std::vector<std::uint32_t> &s);
static bool is_valid(const std::string &s); static bool is_valid(const std::string &s);

View File

@ -141,53 +141,7 @@ std::string cell::check_string(const std::string &to_check)
{ {
return s; return s;
} }
else if (s.size() > 32767)
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)
{ {
s = s.substr(0, 32767); // max string length in Excel s = s.substr(0, 32767); // max string length in Excel
} }

View File

@ -53,6 +53,9 @@ public:
cell.set_value("03:40:16"); cell.set_value("03:40:16");
TS_ASSERT(cell.get_value<xlnt::time>() == xlnt::time(3, 40, 16)); TS_ASSERT(cell.get_value<xlnt::time>() == xlnt::time(3, 40, 16));
cell.set_value("03:");
TS_ASSERT_EQUALS(cell.get_value<std::string>(), "03:");
cell.set_value("03:40"); cell.set_value("03:40");
TS_ASSERT(cell.get_value<xlnt::time>() == xlnt::time(3, 40)); TS_ASSERT(cell.get_value<xlnt::time>() == xlnt::time(3, 40));
@ -322,15 +325,7 @@ public:
auto test_string = "Compound Value (" + std::string(1, pound) + ")"; auto test_string = "Compound Value (" + std::string(1, pound) + ")";
return test_string; 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() void test_good_encoding()
{ {
xlnt::workbook latin1_wb(xlnt::encoding::latin1); xlnt::workbook latin1_wb(xlnt::encoding::latin1);

View File

@ -24,13 +24,13 @@ public:
void test_utf16() 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()); TS_ASSERT(utf16_valid.is_valid());
} }
void test_utf32() 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()); TS_ASSERT(utf32_valid.is_valid());
} }
}; };

View File

@ -21,6 +21,7 @@
// @license: http://www.opensource.org/licenses/mit-license.php // @license: http://www.opensource.org/licenses/mit-license.php
// @author: see AUTHORS file // @author: see AUTHORS file
#include <xlnt/utils/utf8string.hpp> #include <xlnt/utils/utf8string.hpp>
#include <xlnt/utils/unicode_decode_error.hpp>
namespace xlnt { namespace xlnt {
@ -40,7 +41,7 @@ utf8string utf8string::from_latin1(const std::string &s)
return result; return result;
} }
utf8string utf8string::from_utf16(const std::string &s) utf8string utf8string::from_utf16(const std::vector<std::uint16_t> &s)
{ {
utf8string result; utf8string result;
utf8::utf16to8(s.begin(), s.end(), std::back_inserter(result.bytes_)); 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; return result;
} }
utf8string utf8string::from_utf32(const std::string &s) utf8string utf8string::from_utf32(const std::vector<std::uint32_t> &s)
{ {
utf8string result; utf8string result;
utf8::utf32to8(s.begin(), s.end(), std::back_inserter(result.bytes_)); utf8::utf32to8(s.begin(), s.end(), std::back_inserter(result.bytes_));