forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDebugStringConvertible.cpp
More file actions
154 lines (123 loc) · 3.88 KB
/
DebugStringConvertible.cpp
File metadata and controls
154 lines (123 loc) · 3.88 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include "DebugStringConvertible.h"
#include <folly/Conv.h>
#include <folly/Format.h>
namespace facebook {
namespace react {
#if RN_DEBUG_STRING_CONVERTIBLE
std::string DebugStringConvertible::getDebugChildrenDescription(
DebugStringConvertibleOptions options) const {
if (options.depth >= options.maximumDepth) {
return "";
}
options.depth++;
auto trailing = options.format ? std::string{"\n"} : std::string{""};
auto childrenString = std::string{""};
for (auto child : getDebugChildren()) {
if (!child) {
continue;
}
childrenString += child->getDebugDescription(options) + trailing;
}
if (!childrenString.empty() && !trailing.empty()) {
// Removing trailing fragment.
childrenString.erase(childrenString.end() - 1);
}
return childrenString;
}
std::string DebugStringConvertible::getDebugPropsDescription(
DebugStringConvertibleOptions options) const {
if (options.depth >= options.maximumDepth) {
return "";
}
options.depth++;
auto propsString = std::string{""};
for (auto prop : getDebugProps()) {
if (!prop) {
continue;
}
auto name = prop->getDebugName();
auto value = prop->getDebugValue();
auto children = prop->getDebugPropsDescription(options);
auto valueAndChildren =
value + (children.empty() ? "" : "(" + children + ")");
propsString +=
" " + name + (valueAndChildren.empty() ? "" : "=" + valueAndChildren);
}
if (!propsString.empty()) {
// Removing leading space character.
propsString.erase(propsString.begin());
}
return propsString;
}
std::string DebugStringConvertible::getDebugDescription(
DebugStringConvertibleOptions options) const {
auto nameString = getDebugName();
auto valueString = getDebugValue();
// Convention:
// If `name` and `value` are empty, `description` is also empty.
if (nameString.empty() && valueString.empty()) {
return "";
}
// Convention:
// If `name` is empty and `value` isn't empty, `description` equals `value`.
if (nameString.empty()) {
return valueString;
}
auto childrenString = getDebugChildrenDescription(options);
auto propsString = getDebugPropsDescription(options);
auto leading =
options.format ? std::string(options.depth * 2, ' ') : std::string{""};
auto trailing = options.format ? std::string{"\n"} : std::string{""};
return leading + "<" + nameString +
(valueString.empty() ? "" : "=" + valueString) +
(propsString.empty() ? "" : " " + propsString) +
(childrenString.empty() ? "/>"
: ">" + trailing + childrenString + trailing +
leading + "</" + nameString + ">");
}
std::string DebugStringConvertible::getDebugName() const {
return "Node";
}
std::string DebugStringConvertible::getDebugValue() const {
return "";
}
SharedDebugStringConvertibleList DebugStringConvertible::getDebugChildren()
const {
return SharedDebugStringConvertibleList();
}
SharedDebugStringConvertibleList DebugStringConvertible::getDebugProps() const {
return SharedDebugStringConvertibleList();
}
/*
* `toString`-family implementation.
*/
std::string toString(std::string const &value) {
return value;
}
std::string toString(int const &value) {
return folly::to<std::string>(value);
}
std::string toString(bool const &value) {
return folly::to<std::string>(value);
}
std::string toString(float const &value) {
return folly::to<std::string>(value);
}
std::string toString(double const &value) {
return folly::to<std::string>(value);
}
std::string toString(void const *value) {
if (value == nullptr) {
return "null";
}
return folly::sformat("0x{0:016x}", reinterpret_cast<size_t>(value));
}
#endif
} // namespace react
} // namespace facebook