blob: d62eb6f1ea9bfe98af3b5e50939d868539b55a12 [file] [log] [blame]
Sebastian Jansson55251c32019-08-08 09:14:511/*
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 07:39:3112#include <algorithm>
13
Sebastian Jansson55251c32019-08-08 09:14:5114#include "rtc_base/logging.h"
Sebastian Jansson55251c32019-08-08 09:14:5115
16namespace webrtc {
Sebastian Jansson55251c32019-08-08 09:14:5117namespace {
18size_t FindOrEnd(absl::string_view str, size_t start, char delimiter) {
19 size_t pos = str.find(delimiter, start);
20 pos = (pos == std::string::npos) ? str.length() : pos;
21 return pos;
22}
23} // namespace
24
Sebastian Jansson0ee80082019-08-14 11:16:2625namespace struct_parser_impl {
26namespace {
27inline void StringEncode(std::string* target, bool val) {
28 *target += rtc::ToString(val);
29}
30inline void StringEncode(std::string* target, double val) {
31 *target += rtc::ToString(val);
32}
33inline void StringEncode(std::string* target, int val) {
34 *target += rtc::ToString(val);
35}
Bjorn Terelius9f00f0e2019-08-30 07:39:3136inline void StringEncode(std::string* target, unsigned val) {
37 *target += rtc::ToString(val);
38}
Sebastian Jansson0ee80082019-08-14 11:16:2639inline void StringEncode(std::string* target, DataRate val) {
40 *target += webrtc::ToString(val);
41}
42inline void StringEncode(std::string* target, DataSize val) {
43 *target += webrtc::ToString(val);
44}
45inline void StringEncode(std::string* target, TimeDelta val) {
46 *target += webrtc::ToString(val);
47}
48
49template <typename T>
50inline void StringEncode(std::string* sb, absl::optional<T> val) {
51 if (val)
52 StringEncode(sb, *val);
53}
54} // namespace
55template <typename T>
56bool TypedParser<T>::Parse(absl::string_view src, void* target) {
57 auto parsed = ParseTypedParameter<T>(std::string(src));
58 if (parsed.has_value())
59 *reinterpret_cast<T*>(target) = *parsed;
60 return parsed.has_value();
61}
62template <typename T>
63void TypedParser<T>::Encode(const void* src, std::string* target) {
64 StringEncode(target, *reinterpret_cast<const T*>(src));
65}
66
67template class TypedParser<bool>;
68template class TypedParser<double>;
69template class TypedParser<int>;
Bjorn Terelius9f00f0e2019-08-30 07:39:3170template class TypedParser<unsigned>;
Sebastian Jansson0ee80082019-08-14 11:16:2671template class TypedParser<absl::optional<double>>;
72template class TypedParser<absl::optional<int>>;
Bjorn Terelius9f00f0e2019-08-30 07:39:3173template class TypedParser<absl::optional<unsigned>>;
Sebastian Jansson0ee80082019-08-14 11:16:2674
75template class TypedParser<DataRate>;
76template class TypedParser<DataSize>;
77template class TypedParser<TimeDelta>;
78template class TypedParser<absl::optional<DataRate>>;
79template class TypedParser<absl::optional<DataSize>>;
80template class TypedParser<absl::optional<TimeDelta>>;
81} // namespace struct_parser_impl
82
83StructParametersParser::StructParametersParser(
84 std::vector<struct_parser_impl::MemberParameter> members)
85 : members_(std::move(members)) {}
86
87void StructParametersParser::Parse(absl::string_view src) {
Sebastian Jansson55251c32019-08-08 09:14:5188 size_t i = 0;
Sebastian Jansson0ee80082019-08-14 11:16:2689 while (i < src.length()) {
90 size_t val_end = FindOrEnd(src, i, ',');
91 size_t colon_pos = FindOrEnd(src, i, ':');
Sebastian Jansson55251c32019-08-08 09:14:5192 size_t key_end = std::min(val_end, colon_pos);
93 size_t val_begin = key_end + 1u;
Sebastian Jansson0ee80082019-08-14 11:16:2694 absl::string_view key(src.substr(i, key_end - i));
Sebastian Jansson55251c32019-08-08 09:14:5195 absl::string_view opt_value;
96 if (val_end >= val_begin)
Sebastian Jansson0ee80082019-08-14 11:16:2697 opt_value = src.substr(val_begin, val_end - val_begin);
Sebastian Jansson55251c32019-08-08 09:14:5198 i = val_end + 1u;
Sebastian Jansson0ee80082019-08-14 11:16:2699 bool found = false;
100 for (auto& member : members_) {
101 if (key == member.key) {
102 found = true;
103 if (!member.parser.parse(opt_value, member.member_ptr)) {
104 RTC_LOG(LS_WARNING) << "Failed to read field with key: '" << key
105 << "' in trial: \"" << src << "\"";
106 }
107 break;
Sebastian Jansson55251c32019-08-08 09:14:51108 }
Sebastian Jansson0ee80082019-08-14 11:16:26109 }
Ying Wang22e37d82021-02-02 14:36:16110 // "_" is be used to prefix keys that are part of the string for
111 // debugging purposes but not neccessarily used.
112 // e.g. WebRTC-Experiment/param: value, _DebuggingString
113 if (!found && (key.empty() || key[0] != '_')) {
Sebastian Jansson55251c32019-08-08 09:14:51114 RTC_LOG(LS_INFO) << "No field with key: '" << key
Sebastian Jansson0ee80082019-08-14 11:16:26115 << "' (found in trial: \"" << src << "\")";
Sebastian Jansson55251c32019-08-08 09:14:51116 }
117 }
118}
119
Sebastian Jansson0ee80082019-08-14 11:16:26120std::string StructParametersParser::Encode() const {
121 std::string res;
Sebastian Jansson55251c32019-08-08 09:14:51122 bool first = true;
Sebastian Jansson0ee80082019-08-14 11:16:26123 for (const auto& member : members_) {
Sebastian Jansson55251c32019-08-08 09:14:51124 if (!first)
Sebastian Jansson0ee80082019-08-14 11:16:26125 res += ",";
126 res += member.key;
127 res += ":";
128 member.parser.encode(member.member_ptr, &res);
Sebastian Jansson55251c32019-08-08 09:14:51129 first = false;
130 }
Sebastian Jansson0ee80082019-08-14 11:16:26131 return res;
Sebastian Jansson55251c32019-08-08 09:14:51132}
Sebastian Jansson55251c32019-08-08 09:14:51133
134} // namespace webrtc