-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathstring_util.cpp
More file actions
107 lines (89 loc) · 2.69 KB
/
string_util.cpp
File metadata and controls
107 lines (89 loc) · 2.69 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include "string_util.h"
#include <algorithm>
#include <iomanip>
#include <sstream>
namespace datadog {
namespace tracing {
namespace {
constexpr StringView k_spaces_characters = " \f\n\r\t\v";
template <typename Sequence, typename Func>
std::string join(const Sequence& elements, StringView separator,
Func&& append_element) {
auto iter = std::begin(elements);
const auto end = std::end(elements);
std::string result;
if (iter == end) {
return result;
}
append_element(result, *iter);
for (++iter; iter != end; ++iter) {
append(result, separator);
append_element(result, *iter);
}
return result;
}
} // namespace
void to_lower(std::string& text) {
std::transform(text.begin(), text.end(), text.begin(),
[](auto c) { return (char)std::tolower(c); });
}
std::string to_lower(StringView sv) {
std::string s;
s.reserve(sv.size());
std::transform(sv.begin(), sv.end(), std::back_inserter(s),
[](auto c) { return (char)std::tolower(c); });
return s;
}
std::string to_upper(StringView sv) {
std::string s;
s.reserve(sv.size());
std::transform(sv.begin(), sv.end(), std::back_inserter(s),
[](auto c) { return (char)std::toupper(c); });
return s;
}
std::string to_string(bool b) { return b ? "true" : "false"; }
std::string to_string(double d, size_t precision) {
std::stringstream stream;
stream << std::fixed << std::setprecision(precision) << d;
return stream.str();
}
std::string join(const std::vector<StringView>& values, StringView separator) {
return join(values, separator, [](std::string& result, StringView value) {
append(result, value);
});
}
std::string join_propagation_styles(
const std::vector<PropagationStyle>& values) {
return join(values, ",", [](std::string& result, PropagationStyle style) {
result += std::string(to_string_view(style));
});
}
std::string join_tags(
const std::unordered_map<std::string, std::string>& values) {
return join(values, ",", [](std::string& result, const auto& entry) {
const auto& [key, value] = entry;
result += key;
result += ':';
result += value;
});
}
bool starts_with(StringView subject, StringView prefix) {
auto s = subject.data();
auto p = prefix.data();
const auto prefix_end = p + prefix.size();
while (p < prefix_end) {
if (*s++ != *p++) {
return false;
}
}
return true;
}
StringView trim(StringView str) {
str.remove_prefix(
std::min(str.find_first_not_of(k_spaces_characters), str.size()));
const auto pos = str.find_last_not_of(k_spaces_characters);
if (pos != str.npos) str.remove_suffix(str.size() - pos - 1);
return str;
}
} // namespace tracing
} // namespace datadog