Repository navigation
Expand file tree
/
Copy pathtracing_options.cc
More file actions
75 lines (67 loc) · 2.42 KB
/
Copy pathtracing_options.cc
File metadata and controls
75 lines (67 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "google/cloud/tracing_options.h"
#include "absl/types/optional.h"
#include <algorithm>
#include <cstdint>
#include <string>
namespace google {
namespace cloud {
inline namespace GOOGLE_CLOUD_CPP_NS {
namespace {
absl::optional<bool> ParseBoolean(std::string const& str) {
for (auto const* t : {"Y", "y", "T", "t", "1", "on"}) {
if (str == t) return true;
}
for (auto const* f : {"N", "n", "F", "f", "0", "off"}) {
if (str == f) return false;
}
return {};
}
absl::optional<std::int64_t> ParseInteger(std::string const& str) {
std::size_t econv = -1;
auto val = std::stoll(str, &econv);
if (econv != str.size()) return {};
return val;
}
} // namespace
bool operator==(TracingOptions const& a, TracingOptions const& b) {
return a.single_line_mode_ == b.single_line_mode_ &&
a.use_short_repeated_primitives_ == b.use_short_repeated_primitives_ &&
a.truncate_string_field_longer_than_ ==
b.truncate_string_field_longer_than_;
}
TracingOptions& TracingOptions::SetOptions(std::string const& str) {
auto const beg = str.begin();
auto const end = str.end();
for (auto pos = beg;;) {
auto const comma = std::find(pos, end, ',');
auto const equal = std::find(pos, comma, '=');
std::string const opt{pos, equal};
std::string const val{equal + (equal == comma ? 0 : 1), comma};
if (opt == "single_line_mode") {
if (auto v = ParseBoolean(val)) single_line_mode_ = *v;
} else if (opt == "use_short_repeated_primitives") {
if (auto v = ParseBoolean(val)) use_short_repeated_primitives_ = *v;
} else if (opt == "truncate_string_field_longer_than") {
if (auto v = ParseInteger(val)) truncate_string_field_longer_than_ = *v;
}
if (comma == end) break;
pos = comma + 1;
}
return *this;
}
} // namespace GOOGLE_CLOUD_CPP_NS
} // namespace cloud
} // namespace google