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 | |
| 13 | #include "api/transport/webrtc_key_value_config.h" |
| 14 | #include "rtc_base/checks.h" |
| 15 | #include "system_wrappers/include/field_trial.h" |
| 16 | |
| 17 | namespace webrtc { |
| 18 | namespace test { |
| 19 | |
| 20 | ExplicitKeyValueConfig::ExplicitKeyValueConfig(const std::string& s) { |
| 21 | std::string::size_type field_start = 0; |
| 22 | while (field_start < s.size()) { |
| 23 | std::string::size_type separator_pos = s.find('/', field_start); |
| 24 | RTC_CHECK_NE(separator_pos, std::string::npos) |
| 25 | << "Missing separator '/' after field trial key."; |
| 26 | RTC_CHECK_GT(separator_pos, field_start) |
| 27 | << "Field trial key cannot be empty."; |
| 28 | std::string key = s.substr(field_start, separator_pos - field_start); |
| 29 | field_start = separator_pos + 1; |
| 30 | |
| 31 | RTC_CHECK_LT(field_start, s.size()) |
| 32 | << "Missing value after field trial key. String ended."; |
| 33 | separator_pos = s.find('/', field_start); |
| 34 | RTC_CHECK_NE(separator_pos, std::string::npos) |
| 35 | << "Missing terminating '/' in field trial string."; |
| 36 | RTC_CHECK_GT(separator_pos, field_start) |
| 37 | << "Field trial value cannot be empty."; |
| 38 | std::string value = s.substr(field_start, separator_pos - field_start); |
| 39 | field_start = separator_pos + 1; |
| 40 | |
| 41 | key_value_map_[key] = value; |
| 42 | } |
| 43 | // This check is technically redundant due to earlier checks. |
| 44 | // We nevertheless keep the check to make it clear that the entire |
| 45 | // string has been processed, and without indexing past the end. |
| 46 | RTC_CHECK_EQ(field_start, s.size()); |
| 47 | } |
| 48 | |
| 49 | std::string ExplicitKeyValueConfig::Lookup(absl::string_view key) const { |
| 50 | auto it = key_value_map_.find(std::string(key)); |
| 51 | if (it != key_value_map_.end()) |
| 52 | return it->second; |
| 53 | return ""; |
| 54 | } |
| 55 | |
| 56 | } // namespace test |
| 57 | } // namespace webrtc |