-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTemplatePrinter.cpp
More file actions
69 lines (63 loc) · 1.41 KB
/
TemplatePrinter.cpp
File metadata and controls
69 lines (63 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/**
* \file
*
* \copyright This is free and unencumbered software released into the public domain.
* for more information look at LICENSE file or <https://unlicense.org>
*
*/
#include "TemplatePrinter.h"
void TemplatePrinter::printTo(Print& pr, const char* i_template, const char* i_values[])
{
const char* ps=i_template;
while (const char* pn = strchr(ps, '%')) {
pr.write(ps, pn-ps);
pn++;
char next = *pn;
if (next == '\0') {
return;
}
if ( next == '%') { // escaped %
pr.write('%');
} else {
int idx = next - '0';
if (idx >= 0 && idx <= 9) {
pr.write(i_values[idx]);
} else {
pr.write('%');
pr.write(next);
}
}
ps = pn + 1;
}
// write rest of str
pr.write(ps);
}
void TemplatePrinter::printTo(Print& pr, const __FlashStringHelper* i_template, const char* i_values[])
{
PGM_P ps = reinterpret_cast<PGM_P>(i_template);
while (1) {
uint8_t c = pgm_read_byte(ps++);
if (!c) {
break;
}
if (c== '%') { // escaped %
uint8_t c = pgm_read_byte(ps++);
if (!c) {
break;
}
if (c == '%') { // escaped %
pr.write('%');
} else {
int idx = c - '0';
if (idx >= 0 && idx <= 9) {
pr.write(i_values[idx]);
} else {
pr.write('%');
pr.write(c);
}
}
} else {
pr.write(c);
}
}
}