Björn Terelius | ae1892d | 2020-06-17 09:55:55 | [diff] [blame] | 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 | |
Danil Chapovalov | 385b6c5 | 2022-04-08 14:01:50 | [diff] [blame] | 13 | #include "absl/strings/string_view.h" |
Björn Terelius | ae1892d | 2020-06-17 09:55:55 | [diff] [blame] | 14 | #include "rtc_base/checks.h" |
Björn Terelius | ae1892d | 2020-06-17 09:55:55 | [diff] [blame] | 15 | |
| 16 | namespace webrtc { |
| 17 | namespace test { |
| 18 | |
Danil Chapovalov | 385b6c5 | 2022-04-08 14:01:50 | [diff] [blame] | 19 | ExplicitKeyValueConfig::ExplicitKeyValueConfig(absl::string_view s) { |
Björn Terelius | ae1892d | 2020-06-17 09:55:55 | [diff] [blame] | 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."; |
Danil Chapovalov | 385b6c5 | 2022-04-08 14:01:50 | [diff] [blame] | 27 | std::string key(s.substr(field_start, separator_pos - field_start)); |
Björn Terelius | ae1892d | 2020-06-17 09:55:55 | [diff] [blame] | 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."; |
Danil Chapovalov | 385b6c5 | 2022-04-08 14:01:50 | [diff] [blame] | 37 | std::string value(s.substr(field_start, separator_pos - field_start)); |
Björn Terelius | ae1892d | 2020-06-17 09:55:55 | [diff] [blame] | 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 | |
Emil Lundmark | 1c8103d | 2022-09-21 13:20:22 | [diff] [blame] | 48 | std::string ExplicitKeyValueConfig::GetValue(absl::string_view key) const { |
Danil Chapovalov | 385b6c5 | 2022-04-08 14:01:50 | [diff] [blame] | 49 | auto it = key_value_map_.find(key); |
Björn Terelius | ae1892d | 2020-06-17 09:55:55 | [diff] [blame] | 50 | if (it != key_value_map_.end()) |
| 51 | return it->second; |
| 52 | return ""; |
| 53 | } |
| 54 | |
| 55 | } // namespace test |
| 56 | } // namespace webrtc |