From 35e4fd00fcaf8c2574a74b6d87b2a1b448d322e0 Mon Sep 17 00:00:00 2001 From: Pavel Kirienko Date: Fri, 7 Mar 2014 00:58:14 +0400 Subject: [PATCH] Cleaner YAML streaming for strings --- .../include/uavcan/internal/marshal/array.hpp | 17 ++++++++++++++--- .../dsdl_test/dsdl_uavcan_compilability.cpp | 5 +++-- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/libuavcan/include/uavcan/internal/marshal/array.hpp b/libuavcan/include/uavcan/internal/marshal/array.hpp index 0697618fe8..f441c14419 100644 --- a/libuavcan/include/uavcan/internal/marshal/array.hpp +++ b/libuavcan/include/uavcan/internal/marshal/array.hpp @@ -420,6 +420,17 @@ class YamlStreamer > { typedef Array 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 static void streamPrimitives(Stream& s, const ArrayType& array) { @@ -439,8 +450,8 @@ class YamlStreamer > 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 > 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; diff --git a/libuavcan/test/dsdl_test/dsdl_uavcan_compilability.cpp b/libuavcan/test/dsdl_test/dsdl_uavcan_compilability.cpp index 57bed85f3e..9ceba45950 100644 --- a/libuavcan/test/dsdl_test/dsdl_uavcan_compilability.cpp +++ b/libuavcan/test/dsdl_test/dsdl_uavcan_compilability.cpp @@ -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()); }