Skip to content

Commit a324f8a

Browse files
hoxyqfacebook-github-bot
authored andcommitted
Simple parser for serialized tracing categories (#54379)
Summary: # Changelog: [Internal] The CDP protocol concatenates categories in a single string, joined with comma. In this diff, we introduce a method that will parse such string and return a set of local categories. Reviewed By: sbuggay Differential Revision: D85996396
1 parent 2e35790 commit a324f8a

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

packages/react-native/ReactCommon/jsinspector-modern/tracing/TracingCategory.h

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <folly/container/small_vector.h>
1111

1212
#include <optional>
13+
#include <set>
1314
#include <string>
1415

1516
namespace facebook::react::jsinspector_modern::tracing {
@@ -77,4 +78,49 @@ inline std::string serializeTracingCategories(const Categories &categories)
7778
return serializedValue;
7879
}
7980

81+
// { Timeline, UserTiming } => "devtools.timeline,blink.user_timing"
82+
inline std::string serializeTracingCategories(const std::set<Category> &categories)
83+
{
84+
std::string serializedValue;
85+
86+
auto current = categories.begin();
87+
while (current != categories.end()) {
88+
serializedValue += tracingCategoryToString(*current);
89+
90+
++current;
91+
if (current != categories.end()) {
92+
serializedValue += ",";
93+
}
94+
}
95+
96+
return serializedValue;
97+
}
98+
99+
// "devtools.timeline,blink.user_timing" => { Timeline, UserTiming }
100+
inline std::set<Category> parseSerializedTracingCategories(const std::string &serializedCategories)
101+
{
102+
std::set<Category> categories;
103+
if (serializedCategories.empty()) {
104+
return categories;
105+
}
106+
107+
size_t start = 0;
108+
size_t end = serializedCategories.find(',');
109+
while (end != std::string::npos) {
110+
std::string token = serializedCategories.substr(start, end - start);
111+
if (auto category = getTracingCategoryFromString(token)) {
112+
categories.insert(*category);
113+
}
114+
start = end + 1;
115+
end = serializedCategories.find(',', start);
116+
}
117+
118+
std::string lastToken = serializedCategories.substr(start);
119+
if (auto category = getTracingCategoryFromString(lastToken)) {
120+
categories.insert(*category);
121+
}
122+
123+
return categories;
124+
}
125+
80126
} // namespace facebook::react::jsinspector_modern::tracing

0 commit comments

Comments
 (0)