xref: /aosp_15_r20/external/webrtc/rtc_base/experiments/struct_parameters_parser.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2019 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 #include "rtc_base/experiments/struct_parameters_parser.h"
11 
12 #include <algorithm>
13 
14 #include "absl/strings/string_view.h"
15 #include "rtc_base/logging.h"
16 
17 namespace webrtc {
18 namespace {
FindOrEnd(absl::string_view str,size_t start,char delimiter)19 size_t FindOrEnd(absl::string_view str, size_t start, char delimiter) {
20   size_t pos = str.find(delimiter, start);
21   pos = (pos == absl::string_view::npos) ? str.length() : pos;
22   return pos;
23 }
24 }  // namespace
25 
26 namespace struct_parser_impl {
27 namespace {
StringEncode(std::string * target,bool val)28 inline void StringEncode(std::string* target, bool val) {
29   *target += rtc::ToString(val);
30 }
StringEncode(std::string * target,double val)31 inline void StringEncode(std::string* target, double val) {
32   *target += rtc::ToString(val);
33 }
StringEncode(std::string * target,int val)34 inline void StringEncode(std::string* target, int val) {
35   *target += rtc::ToString(val);
36 }
StringEncode(std::string * target,unsigned val)37 inline void StringEncode(std::string* target, unsigned val) {
38   *target += rtc::ToString(val);
39 }
StringEncode(std::string * target,DataRate val)40 inline void StringEncode(std::string* target, DataRate val) {
41   *target += webrtc::ToString(val);
42 }
StringEncode(std::string * target,DataSize val)43 inline void StringEncode(std::string* target, DataSize val) {
44   *target += webrtc::ToString(val);
45 }
StringEncode(std::string * target,TimeDelta val)46 inline void StringEncode(std::string* target, TimeDelta val) {
47   *target += webrtc::ToString(val);
48 }
49 
50 template <typename T>
StringEncode(std::string * sb,absl::optional<T> val)51 inline void StringEncode(std::string* sb, absl::optional<T> val) {
52   if (val)
53     StringEncode(sb, *val);
54 }
55 }  // namespace
56 template <typename T>
Parse(absl::string_view src,void * target)57 bool TypedParser<T>::Parse(absl::string_view src, void* target) {
58   auto parsed = ParseTypedParameter<T>(std::string(src));
59   if (parsed.has_value())
60     *reinterpret_cast<T*>(target) = *parsed;
61   return parsed.has_value();
62 }
63 template <typename T>
Encode(const void * src,std::string * target)64 void TypedParser<T>::Encode(const void* src, std::string* target) {
65   StringEncode(target, *reinterpret_cast<const T*>(src));
66 }
67 
68 template class TypedParser<bool>;
69 template class TypedParser<double>;
70 template class TypedParser<int>;
71 template class TypedParser<unsigned>;
72 template class TypedParser<absl::optional<double>>;
73 template class TypedParser<absl::optional<int>>;
74 template class TypedParser<absl::optional<unsigned>>;
75 
76 template class TypedParser<DataRate>;
77 template class TypedParser<DataSize>;
78 template class TypedParser<TimeDelta>;
79 template class TypedParser<absl::optional<DataRate>>;
80 template class TypedParser<absl::optional<DataSize>>;
81 template class TypedParser<absl::optional<TimeDelta>>;
82 }  // namespace struct_parser_impl
83 
StructParametersParser(std::vector<struct_parser_impl::MemberParameter> members)84 StructParametersParser::StructParametersParser(
85     std::vector<struct_parser_impl::MemberParameter> members)
86     : members_(std::move(members)) {}
87 
Parse(absl::string_view src)88 void StructParametersParser::Parse(absl::string_view src) {
89   size_t i = 0;
90   while (i < src.length()) {
91     size_t val_end = FindOrEnd(src, i, ',');
92     size_t colon_pos = FindOrEnd(src, i, ':');
93     size_t key_end = std::min(val_end, colon_pos);
94     size_t val_begin = key_end + 1u;
95     absl::string_view key(src.substr(i, key_end - i));
96     absl::string_view opt_value;
97     if (val_end >= val_begin)
98       opt_value = src.substr(val_begin, val_end - val_begin);
99     i = val_end + 1u;
100     bool found = false;
101     for (auto& member : members_) {
102       if (key == member.key) {
103         found = true;
104         if (!member.parser.parse(opt_value, member.member_ptr)) {
105           RTC_LOG(LS_WARNING) << "Failed to read field with key: '" << key
106                               << "' in trial: \"" << src << "\"";
107         }
108         break;
109       }
110     }
111     // "_" is be used to prefix keys that are part of the string for
112     // debugging purposes but not neccessarily used.
113     // e.g. WebRTC-Experiment/param: value, _DebuggingString
114     if (!found && (key.empty() || key[0] != '_')) {
115       RTC_LOG(LS_INFO) << "No field with key: '" << key
116                        << "' (found in trial: \"" << src << "\")";
117     }
118   }
119 }
120 
Encode() const121 std::string StructParametersParser::Encode() const {
122   std::string res;
123   bool first = true;
124   for (const auto& member : members_) {
125     if (!first)
126       res += ",";
127     res += member.key;
128     res += ":";
129     member.parser.encode(member.member_ptr, &res);
130     first = false;
131   }
132   return res;
133 }
134 
135 }  // namespace webrtc
136