xref: /aosp_15_r20/external/webrtc/test/explicit_key_value_config.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
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 
13 #include "absl/strings/string_view.h"
14 #include "rtc_base/checks.h"
15 
16 namespace webrtc {
17 namespace test {
18 
ExplicitKeyValueConfig(absl::string_view s)19 ExplicitKeyValueConfig::ExplicitKeyValueConfig(absl::string_view s) {
20   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.";
27     std::string key(s.substr(field_start, separator_pos - field_start));
28     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.";
37     std::string value(s.substr(field_start, separator_pos - field_start));
38     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 
GetValue(absl::string_view key) const48 std::string ExplicitKeyValueConfig::GetValue(absl::string_view key) const {
49   auto it = key_value_map_.find(key);
50   if (it != key_value_map_.end())
51     return it->second;
52   return "";
53 }
54 
55 }  // namespace test
56 }  // namespace webrtc
57