-
Notifications
You must be signed in to change notification settings - Fork 0
/
Prettyfier.cpp
executable file
·86 lines (64 loc) · 1.69 KB
/
Prettyfier.cpp
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Copyright Benoit Blanchon 2014
// MIT License
//
// Arduino JSON library
// https://github.com/bblanchon/ArduinoJson
#include "Prettyfier.h"
using namespace ArduinoJson::Internals;
size_t Prettyfier::write(uint8_t c) {
size_t n = _inString ? handleStringChar(c) : handleMarkupChar(c);
_previousChar = c;
return n;
}
inline size_t Prettyfier::handleStringChar(uint8_t c) {
bool isQuote = c == '"' && _previousChar != '\\';
if (isQuote) _inString = false;
return _sink.write(c);
}
inline size_t Prettyfier::handleMarkupChar(uint8_t c) {
switch (c) {
case '{':
case '[':
return handleBlockOpen(c);
case '}':
case ']':
return handleBlockClose(c);
case ':':
return handleColumn();
case ',':
return handleComma();
case '"':
return handleQuoteOpen();
default:
return handleNormalChar(c);
}
}
inline size_t Prettyfier::handleBlockOpen(uint8_t c) {
return indentIfNeeded() + _sink.write(c);
}
inline size_t Prettyfier::handleBlockClose(uint8_t c) {
return unindentIfNeeded() + _sink.write(c);
}
inline size_t Prettyfier::handleColumn() {
return _sink.write(':') + _sink.write(' ');
}
inline size_t Prettyfier::handleComma() {
return _sink.write(',') + _sink.println();
}
inline size_t Prettyfier::handleQuoteOpen() {
_inString = true;
return indentIfNeeded() + _sink.write('"');
}
inline size_t Prettyfier::handleNormalChar(uint8_t c) {
return indentIfNeeded() + _sink.write(c);
}
size_t Prettyfier::indentIfNeeded() {
if (!inEmptyBlock()) return 0;
_sink.indent();
return _sink.println();
}
size_t Prettyfier::unindentIfNeeded() {
if (inEmptyBlock()) return 0;
_sink.unindent();
return _sink.println();
}