Skip to content

Commit c7436d8

Browse files
committed
Revise ArrayPrinter to escape character data, remove separator parameter
1 parent 1057813 commit c7436d8

File tree

4 files changed

+68
-11
lines changed

4 files changed

+68
-11
lines changed

src/include/FlashString/Array.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,9 @@ template <typename ElementType> class Array : public Object<Array<ElementType>,
119119
* @brief Returns a printer object for this array
120120
* @note ElementType must be supported by Print
121121
*/
122-
ArrayPrinter<Array> printer(const WString& separator = ", ") const
122+
ArrayPrinter<Array> printer() const
123123
{
124-
return ArrayPrinter<Array>(*this, separator);
124+
return ArrayPrinter<Array>(*this);
125125
}
126126

127127
size_t printTo(Print& p) const

src/include/FlashString/ArrayPrinter.hpp

+63-6
Original file line numberDiff line numberDiff line change
@@ -20,39 +20,96 @@
2020
#pragma once
2121

2222
#include "Print.hpp"
23+
#include <stringutil.h>
2324

2425
namespace FSTR
2526
{
27+
template <typename T> typename std::enable_if<!std::is_same<T, char>::value, size_t>::type printElement(Print& p, T e)
28+
{
29+
return print(p, e);
30+
}
31+
32+
template <typename T> typename std::enable_if<std::is_same<T, char>::value, size_t>::type printElement(Print& p, T c)
33+
{
34+
auto escape = [](char c) -> char {
35+
switch(c) {
36+
case '\0':
37+
return '0';
38+
case '\'':
39+
return '\'';
40+
case '\"':
41+
return '"';
42+
case '\?':
43+
return '?';
44+
case '\\':
45+
return '\\';
46+
case '\a':
47+
return 'a';
48+
case '\b':
49+
return 'b';
50+
case '\f':
51+
return 'f';
52+
case '\n':
53+
return 'n';
54+
case '\r':
55+
return 'r';
56+
case '\t':
57+
return 't';
58+
case '\v':
59+
return 'v';
60+
default:
61+
return '\0';
62+
}
63+
};
64+
65+
char buf[8];
66+
char* o = buf;
67+
*o++ = '\'';
68+
char esc = escape(c);
69+
if(esc) {
70+
*o++ = '\\';
71+
*o++ = esc;
72+
} else if(isprint(c)) {
73+
*o++ = c;
74+
} else {
75+
*o++ = '\\';
76+
*o++ = 'x';
77+
*o++ = hexchar(uint8_t(c) >> 4);
78+
*o++ = hexchar(uint8_t(c) & 0x0f);
79+
}
80+
*o++ = '\'';
81+
return p.write(buf, o - buf);
82+
}
83+
2684
/**
2785
* @brief Class template to provide a simple way to print the contents of an array
2886
* @note Used by Array::printTo() method
2987
*/
3088
template <class ArrayType> class ArrayPrinter : public Printable
3189
{
3290
public:
33-
ArrayPrinter(const ArrayType& array, const WString& separator) : array(array), separator(separator)
91+
ArrayPrinter(const ArrayType& array) : array(array)
3492
{
3593
}
3694

3795
size_t printTo(Print& p) const override
3896
{
3997
size_t count = 0;
4098

41-
count += p.print("[");
99+
count += p.print('{');
42100
for(unsigned i = 0; i < array.length(); ++i) {
43101
if(i > 0) {
44-
count += p.print(separator);
102+
count += p.print(", ");
45103
}
46-
count += print(p, array[i]);
104+
count += printElement(p, array[i]);
47105
}
48-
count += p.print("]");
106+
count += p.print('}');
49107

50108
return count;
51109
}
52110

53111
private:
54112
const ArrayType& array;
55-
WString separator;
56113
};
57114

58115
} // namespace FSTR

src/include/FlashString/Table.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ template <typename ElementType, size_t Columns> class TableRow
6666
*/
6767
size_t printTo(Print& p) const
6868
{
69-
return FSTR::ArrayPrinter<TableRow>(*this, ", ").printTo(p);
69+
return FSTR::ArrayPrinter<TableRow>(*this).printTo(p);
7070
}
7171

7272
/**

src/include/FlashString/Vector.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,9 @@ template <class ObjectType> class Vector : public Object<Vector<ObjectType>, Obj
146146

147147
/* Arduino Print support */
148148

149-
ArrayPrinter<Vector> printer(const WString& separator = ", ") const
149+
ArrayPrinter<Vector> printer() const
150150
{
151-
return ArrayPrinter<Vector>(*this, separator);
151+
return ArrayPrinter<Vector>(*this);
152152
}
153153

154154
size_t printTo(Print& p) const

0 commit comments

Comments
 (0)