blob: 011df3eaba9607ef428634dd8751af49801beb2d [file] [log] [blame]
Sebastian Jansson55251c32019-08-08 11:14:51 +02001/*
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
Bjorn Terelius9f00f0e2019-08-30 09:39:31 +020012#include <algorithm>
13
Ali Tofigh7fa90572022-03-17 15:47:49 +010014#include "absl/strings/string_view.h"
Sebastian Jansson55251c32019-08-08 11:14:51 +020015#include "rtc_base/logging.h"
Sebastian Jansson55251c32019-08-08 11:14:51 +020016
17namespace webrtc {
Sebastian Jansson55251c32019-08-08 11:14:51 +020018namespace {
19size_t FindOrEnd(absl::string_view str, size_t start, char delimiter) {
20 size_t pos = str.find(delimiter, start);
Ali Tofigh7fa90572022-03-17 15:47:49 +010021 pos = (pos == absl::string_view::npos) ? str.length() : pos;
Sebastian Jansson55251c32019-08-08 11:14:51 +020022 return pos;
23}
24} // namespace
25
Sebastian Jansson0ee80082019-08-14 13:16:26 +020026namespace struct_parser_impl {
27namespace {
28inline void StringEncode(std::string* target, bool val) {
29 *target += rtc::ToString(val);
30}
31inline void StringEncode(std::string* target, double val) {
32 *target += rtc::ToString(val);
33}
34inline void StringEncode(std::string* target, int val) {
35 *target += rtc::ToString(val);
36}
Bjorn Terelius9f00f0e2019-08-30 09:39:31 +020037inline void StringEncode(std::string* target, unsigned val) {
38 *target += rtc::ToString(val);
39}
Sebastian Jansson0ee80082019-08-14 13:16:26 +020040inline void StringEncode(std::string* target, DataRate val) {
41 *target += webrtc::ToString(val);
42}
43inline void StringEncode(std::string* target, DataSize val) {
44 *target += webrtc::ToString(val);
45}
46inline void StringEncode(std::string* target, TimeDelta val) {
47 *target += webrtc::ToString(val);
48}
49
50template <typename T>
51inline void StringEncode(std::string* sb, absl::optional<T> val) {
52 if (val)
53 StringEncode(sb, *val);
54}
55} // namespace
56template <typename T>
57bool 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}
63template <typename T>
64void TypedParser<T>::Encode(const void* src, std::string* target) {
65 StringEncode(target, *reinterpret_cast<const T*>(src));
66}
67
68template class TypedParser<bool>;
69template class TypedParser<double>;
70template class TypedParser<int>;
Bjorn Terelius9f00f0e2019-08-30 09:39:31 +020071template class TypedParser<unsigned>;
Sebastian Jansson0ee80082019-08-14 13:16:26 +020072template class TypedParser<absl::optional<double>>;
73template class TypedParser<absl::optional<int>>;
Bjorn Terelius9f00f0e2019-08-30 09:39:31 +020074template class TypedParser<absl::optional<unsigned>>;
Sebastian Jansson0ee80082019-08-14 13:16:26 +020075
76template class TypedParser<DataRate>;
77template class TypedParser<DataSize>;
78template class TypedParser<TimeDelta>;
79template class TypedParser<absl::optional<DataRate>>;
80template class TypedParser<absl::optional<DataSize>>;
81template class TypedParser<absl::optional<TimeDelta>>;
82} // namespace struct_parser_impl
83
84StructParametersParser::StructParametersParser(
85 std::vector<struct_parser_impl::MemberParameter> members)
86 : members_(std::move(members)) {}
87
88void StructParametersParser::Parse(absl::string_view src) {
Sebastian Jansson55251c32019-08-08 11:14:51 +020089 size_t i = 0;
Sebastian Jansson0ee80082019-08-14 13:16:26 +020090 while (i < src.length()) {
91 size_t val_end = FindOrEnd(src, i, ',');
92 size_t colon_pos = FindOrEnd(src, i, ':');
Sebastian Jansson55251c32019-08-08 11:14:51 +020093 size_t key_end = std::min(val_end, colon_pos);
94 size_t val_begin = key_end + 1u;
Sebastian Jansson0ee80082019-08-14 13:16:26 +020095 absl::string_view key(src.substr(i, key_end - i));
Sebastian Jansson55251c32019-08-08 11:14:51 +020096 absl::string_view opt_value;
97 if (val_end >= val_begin)
Sebastian Jansson0ee80082019-08-14 13:16:26 +020098 opt_value = src.substr(val_begin, val_end - val_begin);
Sebastian Jansson55251c32019-08-08 11:14:51 +020099 i = val_end + 1u;
Sebastian Jansson0ee80082019-08-14 13:16:26 +0200100 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;
Sebastian Jansson55251c32019-08-08 11:14:51 +0200109 }
Sebastian Jansson0ee80082019-08-14 13:16:26 +0200110 }
Ying Wang22e37d82021-02-02 15:36:16 +0100111 // "_" 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] != '_')) {
Sebastian Jansson55251c32019-08-08 11:14:51 +0200115 RTC_LOG(LS_INFO) << "No field with key: '" << key
Sebastian Jansson0ee80082019-08-14 13:16:26 +0200116 << "' (found in trial: \"" << src << "\")";
Sebastian Jansson55251c32019-08-08 11:14:51 +0200117 }
118 }
119}
120
Sebastian Jansson0ee80082019-08-14 13:16:26 +0200121std::string StructParametersParser::Encode() const {
122 std::string res;
Sebastian Jansson55251c32019-08-08 11:14:51 +0200123 bool first = true;
Sebastian Jansson0ee80082019-08-14 13:16:26 +0200124 for (const auto& member : members_) {
Sebastian Jansson55251c32019-08-08 11:14:51 +0200125 if (!first)
Sebastian Jansson0ee80082019-08-14 13:16:26 +0200126 res += ",";
127 res += member.key;
128 res += ":";
129 member.parser.encode(member.member_ptr, &res);
Sebastian Jansson55251c32019-08-08 11:14:51 +0200130 first = false;
131 }
Sebastian Jansson0ee80082019-08-14 13:16:26 +0200132 return res;
Sebastian Jansson55251c32019-08-08 11:14:51 +0200133}
Sebastian Jansson55251c32019-08-08 11:14:51 +0200134
135} // namespace webrtc