Skip to content

Commit

Permalink
Empty string fixes with XML serialization
Browse files Browse the repository at this point in the history
Fixes #182
  • Loading branch information
AzothAmmo committed Apr 4, 2015
1 parent aad1e37 commit b385b39
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
9 changes: 6 additions & 3 deletions include/cereal/archives/xml.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,12 @@ namespace cereal
itsOS << value << std::ends;

const auto strValue = itsOS.str();
// if there is the first or the last character in string is whitespace then add xml:space attribute
// the last character has index length-2 because there is \0 character at end added with std::ends
if( !strValue.empty() && ( xml_detail::isWhitespace( strValue[0] ) || xml_detail::isWhitespace( strValue[strValue.length() - 2] ) ) )

// If the first or last character is a whitespace, add xml:space attribute
// the string always contains a '\0' added by std::ends, so the last character is at len-2 and an 'empty'
// string has a length of 1 or lower
const auto len = strValue.length();
if ( len > 1 && ( xml_detail::isWhitespace( strValue[0] ) || xml_detail::isWhitespace( strValue[len - 2] ) ) )
{
itsNodes.top().node->append_attribute( itsXML.allocate_attribute( "xml:space", "preserve" ) );
}
Expand Down
6 changes: 5 additions & 1 deletion unittests/basic_string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,27 +38,32 @@ void test_string_basic()
{
std::basic_string<char> o_string = random_basic_string<char>(gen);
std::basic_string<char> o_string2 = "";
std::basic_string<char> o_string3;

std::ostringstream os;
{
OArchive oar(os);
oar(o_string);
oar(o_string2);
oar(o_string3);
}

std::basic_string<char> i_string;
std::basic_string<char> i_string2;
std::basic_string<char> i_string3;

std::istringstream is(os.str());
{
IArchive iar(is);

iar(i_string);
iar(i_string2);
iar(i_string3);
}

BOOST_CHECK_EQUAL(i_string, o_string);
BOOST_CHECK_EQUAL(i_string2, o_string2);
BOOST_CHECK_EQUAL(i_string3, o_string3);
}
}

Expand Down Expand Up @@ -148,7 +153,6 @@ void test_ws_in_out(Out const & o_value_with_ws)

namespace boost
{

void save( cereal::XMLOutputArchive & ar, boost::string_ref const & str )
{
ar.saveValue( str );
Expand Down

0 comments on commit b385b39

Please sign in to comment.