blob: 90690c0514d6f255ea771f112602190d37f90620 [file] [log] [blame]
Björn Tereliusae1892d2020-06-17 09:55:551/*
2 * Copyright (c) 2020 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
11#include "test/explicit_key_value_config.h"
12
Danil Chapovalov385b6c52022-04-08 14:01:5013#include "absl/strings/string_view.h"
Björn Tereliusae1892d2020-06-17 09:55:5514#include "rtc_base/checks.h"
Björn Tereliusae1892d2020-06-17 09:55:5515
16namespace webrtc {
17namespace test {
18
Danil Chapovalov385b6c52022-04-08 14:01:5019ExplicitKeyValueConfig::ExplicitKeyValueConfig(absl::string_view s) {
Björn Tereliusae1892d2020-06-17 09:55:5520 std::string::size_type field_start = 0;
21 while (field_start < s.size()) {
22 std::string::size_type separator_pos = s.find('/', field_start);
23 RTC_CHECK_NE(separator_pos, std::string::npos)
24 << "Missing separator '/' after field trial key.";
25 RTC_CHECK_GT(separator_pos, field_start)
26 << "Field trial key cannot be empty.";
Danil Chapovalov385b6c52022-04-08 14:01:5027 std::string key(s.substr(field_start, separator_pos - field_start));
Björn Tereliusae1892d2020-06-17 09:55:5528 field_start = separator_pos + 1;
29
30 RTC_CHECK_LT(field_start, s.size())
31 << "Missing value after field trial key. String ended.";
32 separator_pos = s.find('/', field_start);
33 RTC_CHECK_NE(separator_pos, std::string::npos)
34 << "Missing terminating '/' in field trial string.";
35 RTC_CHECK_GT(separator_pos, field_start)
36 << "Field trial value cannot be empty.";
Danil Chapovalov385b6c52022-04-08 14:01:5037 std::string value(s.substr(field_start, separator_pos - field_start));
Björn Tereliusae1892d2020-06-17 09:55:5538 field_start = separator_pos + 1;
39
40 key_value_map_[key] = value;
41 }
42 // This check is technically redundant due to earlier checks.
43 // We nevertheless keep the check to make it clear that the entire
44 // string has been processed, and without indexing past the end.
45 RTC_CHECK_EQ(field_start, s.size());
46}
47
Emil Lundmark1c8103d2022-09-21 13:20:2248std::string ExplicitKeyValueConfig::GetValue(absl::string_view key) const {
Danil Chapovalov385b6c52022-04-08 14:01:5049 auto it = key_value_map_.find(key);
Björn Tereliusae1892d2020-06-17 09:55:5550 if (it != key_value_map_.end())
51 return it->second;
52 return "";
53}
54
55} // namespace test
56} // namespace webrtc