mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-10-12 14:33:34 +08:00
Cleaner YAML streaming for strings
This commit is contained in:
@@ -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());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user