OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "services/service_manager/public/cpp/shared_file_util.h" | |
6 | |
7 #include "base/strings/string_number_conversions.h" | |
8 #include "base/strings/string_split.h" | |
9 | |
10 namespace service_manager { | |
11 | |
12 void SharedFileSwitchValueBuilder::AddEntry(const std::string& key_str, | |
13 int key_id) { | |
14 if (!switch_value_.empty()) { | |
15 switch_value_ += ","; | |
16 } | |
17 switch_value_ += key_str, switch_value_ += ":"; | |
18 switch_value_ += base::IntToString(key_id); | |
19 } | |
20 | |
21 bool ParseSharedFileSwitchValue(const std::string& value, | |
22 std::map<int, std::string>* values) { | |
dcheng
2017/02/15 08:05:28
Ditto: does it make sense to return a base::Option
Jay Civelli
2017/02/15 19:53:48
Yes, done.
| |
23 std::vector<std::string> string_pairs = base::SplitString( | |
24 value, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY); | |
25 for (const auto& pair : string_pairs) { | |
26 size_t colon_position = pair.find(":"); | |
27 if (colon_position == std::string::npos || colon_position == 0 || | |
28 colon_position == pair.size() - 1) { | |
29 LOG(ERROR) << "Found invalid entry parsing shared file string value:" | |
30 << pair; | |
31 return false; | |
32 } | |
33 std::string key = pair.substr(0, colon_position); | |
34 std::string number_string = | |
35 pair.substr(colon_position + 1, std::string::npos); | |
36 int key_int; | |
37 if (!base::StringToInt(number_string, &key_int)) { | |
38 LOG(ERROR) << "Found invalid entry parsing shared file string value:" | |
39 << number_string << " (not an int)."; | |
40 return false; | |
41 } | |
42 | |
43 (*values)[key_int] = key; | |
44 } | |
45 return true; | |
46 } | |
47 | |
48 } // namespace service_manager | |
OLD | NEW |