Sebastian Jansson | 55251c3 | 2019-08-08 11:14:51 +0200 | [diff] [blame] | 1 | /* |
| 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 | |
| 12 | #include <algorithm> |
| 13 | |
| 14 | #include "rtc_base/logging.h" |
| 15 | #include "rtc_base/strings/string_builder.h" |
| 16 | |
| 17 | namespace webrtc { |
| 18 | namespace struct_parser_impl { |
| 19 | namespace { |
| 20 | size_t FindOrEnd(absl::string_view str, size_t start, char delimiter) { |
| 21 | size_t pos = str.find(delimiter, start); |
| 22 | pos = (pos == std::string::npos) ? str.length() : pos; |
| 23 | return pos; |
| 24 | } |
| 25 | } // namespace |
| 26 | |
| 27 | void ParseConfigParams( |
| 28 | absl::string_view config_str, |
| 29 | std::map<std::string, std::function<bool(absl::string_view)>> field_map) { |
| 30 | size_t i = 0; |
| 31 | while (i < config_str.length()) { |
| 32 | size_t val_end = FindOrEnd(config_str, i, ','); |
| 33 | size_t colon_pos = FindOrEnd(config_str, i, ':'); |
| 34 | size_t key_end = std::min(val_end, colon_pos); |
| 35 | size_t val_begin = key_end + 1u; |
| 36 | std::string key(config_str.substr(i, key_end - i)); |
| 37 | absl::string_view opt_value; |
| 38 | if (val_end >= val_begin) |
| 39 | opt_value = config_str.substr(val_begin, val_end - val_begin); |
| 40 | i = val_end + 1u; |
| 41 | auto field = field_map.find(key); |
| 42 | if (field != field_map.end()) { |
| 43 | if (!field->second(opt_value)) { |
| 44 | RTC_LOG(LS_WARNING) << "Failed to read field with key: '" << key |
| 45 | << "' in trial: \"" << config_str << "\""; |
| 46 | } |
| 47 | } else { |
| 48 | RTC_LOG(LS_INFO) << "No field with key: '" << key |
| 49 | << "' (found in trial: \"" << config_str << "\")"; |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | std::string EncodeStringStringMap(std::map<std::string, std::string> mapping) { |
| 55 | rtc::StringBuilder sb; |
| 56 | bool first = true; |
| 57 | for (const auto& kv : mapping) { |
| 58 | if (!first) |
| 59 | sb << ","; |
| 60 | sb << kv.first << ":" << kv.second; |
| 61 | first = false; |
| 62 | } |
| 63 | return sb.Release(); |
| 64 | } |
| 65 | } // namespace struct_parser_impl |
| 66 | |
| 67 | } // namespace webrtc |