Cleaner YAML streaming for strings

This commit is contained in:
Pavel Kirienko
2014-03-07 00:58:14 +04:00
parent 4753d4ac79
commit 35e4fd00fc
2 changed files with 17 additions and 5 deletions
@@ -420,6 +420,17 @@ class YamlStreamer<Array<T, ArrayMode, MaxSize> >
{
typedef Array<T, ArrayMode, MaxSize> ArrayType;
static bool isNiceCharacter(int c)
{
if (c >= 32 && c <= 126)
return true;
static const char Good[] = {'\n', '\r', '\t'};
for (unsigned int i = 0; i < sizeof(Good) / sizeof(Good[0]); i++)
if (Good[i] == c)
return true;
return false;
}
template <typename Stream>
static void streamPrimitives(Stream& s, const ArrayType& array)
{
@@ -439,8 +450,8 @@ class YamlStreamer<Array<T, ArrayMode, MaxSize> >
s << '"';
for (std::size_t i = 0; i < array.size(); i++)
{
const unsigned int c = array.at(i);
if (c < 32 || c > 126) // Non-printable characters
const int c = array.at(i);
if (c < 32 || c > 126)
{
char nibbles[2] = {(c >> 4) & 0xF, c & 0xF};
for (int i = 0; i < 2; i++)
@@ -471,7 +482,7 @@ class YamlStreamer<Array<T, ArrayMode, MaxSize> >
bool printable_only = true;
for (int i = 0; i < array.size(); i++)
{
if (array[i] < 32 || array[i] > 126)
if (!isNiceCharacter(array[i]))
{
printable_only = false;
break;
@@ -43,7 +43,7 @@ TEST(Dsdl, Streaming)
mavlink.compid = 12;
mavlink.seq = 42;
mavlink.payload = "Here goes payload";
mavlink.payload = "Here\tgoes\npayload";
os << mavlink << std::endl << "==========" << std::endl;
uavcan::protocol::GetNodeInfo::Response get_node_info_rsp;
@@ -63,7 +63,7 @@ TEST(Dsdl, Streaming)
"sysid: 0\n"
"compid: 12\n"
"msgid: 0\n"
"payload: \"Here goes payload\"\n"
"payload: \"Here\\x09goes\\x0Apayload\"\n"
"==========\n"
"software_version: \n"
" major: 0\n"
@@ -117,5 +117,6 @@ TEST(Dsdl, Streaming)
" vector: [0, 0, 0, 0]\n"
" bools: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]\n"
"==========\n";
std::cout << os.str();
ASSERT_EQ(Reference, os.str());
}