/src/logging-log4cxx/src/main/cpp/stringhelper.cpp
Line | Count | Source |
1 | | /* |
2 | | * Licensed to the Apache Software Foundation (ASF) under one or more |
3 | | * contributor license agreements. See the NOTICE file distributed with |
4 | | * this work for additional information regarding copyright ownership. |
5 | | * The ASF licenses this file to You under the Apache License, Version 2.0 |
6 | | * (the "License"); you may not use this file except in compliance with |
7 | | * the License. You may obtain a copy of the License at |
8 | | * |
9 | | * http://www.apache.org/licenses/LICENSE-2.0 |
10 | | * |
11 | | * Unless required by applicable law or agreed to in writing, software |
12 | | * distributed under the License is distributed on an "AS IS" BASIS, |
13 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14 | | * See the License for the specific language governing permissions and |
15 | | * limitations under the License. |
16 | | */ |
17 | | |
18 | | #define __STDC_CONSTANT_MACROS |
19 | | #include <log4cxx/logstring.h> |
20 | | #include <log4cxx/helpers/stringhelper.h> |
21 | | #include <log4cxx/helpers/transcoder.h> |
22 | | #include <algorithm> |
23 | | #include <vector> |
24 | | #include <iterator> |
25 | | #include <algorithm> |
26 | | #include <cctype> |
27 | | |
28 | | using namespace LOG4CXX_NS; |
29 | | using namespace LOG4CXX_NS::helpers; |
30 | | |
31 | | bool StringHelper::equalsIgnoreCase(const LogString& s1, const logchar* upper, const logchar* lower) |
32 | 4.23M | { |
33 | 4.23M | for (const auto& item : s1) |
34 | 6.08M | { |
35 | 6.08M | if (0 == item || // OSS-Fuzz makes strings with embedded NUL characters |
36 | 6.01M | (item != *upper && item != *lower)) |
37 | 3.94M | { |
38 | 3.94M | return false; |
39 | 3.94M | } |
40 | 2.14M | ++upper; |
41 | 2.14M | ++lower; |
42 | 2.14M | } |
43 | | |
44 | 295k | return 0 == *upper; |
45 | 4.23M | } log4cxx::helpers::StringHelper::equalsIgnoreCase(std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> > const&, wchar_t const*, wchar_t const*) Line | Count | Source | 32 | 927k | { | 33 | 927k | for (const auto& item : s1) | 34 | 1.55M | { | 35 | 1.55M | if (0 == item || // OSS-Fuzz makes strings with embedded NUL characters | 36 | 1.54M | (item != *upper && item != *lower)) | 37 | 827k | { | 38 | 827k | return false; | 39 | 827k | } | 40 | 731k | ++upper; | 41 | 731k | ++lower; | 42 | 731k | } | 43 | | | 44 | 99.9k | return 0 == *upper; | 45 | 927k | } |
log4cxx::helpers::StringHelper::equalsIgnoreCase(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, char const*, char const*) Line | Count | Source | 32 | 3.30M | { | 33 | 3.30M | for (const auto& item : s1) | 34 | 4.52M | { | 35 | 4.52M | if (0 == item || // OSS-Fuzz makes strings with embedded NUL characters | 36 | 4.46M | (item != *upper && item != *lower)) | 37 | 3.11M | { | 38 | 3.11M | return false; | 39 | 3.11M | } | 40 | 1.40M | ++upper; | 41 | 1.40M | ++lower; | 42 | 1.40M | } | 43 | | | 44 | 195k | return 0 == *upper; | 45 | 3.30M | } |
|
46 | | |
47 | | bool StringHelper::equalsIgnoreCase(const LogString& s1, const LogString& upper, const LogString& lower) |
48 | 0 | { |
49 | 0 | LogString::const_iterator u = upper.begin(); |
50 | 0 | LogString::const_iterator l = lower.begin(); |
51 | 0 | LogString::const_iterator iter = s1.begin(); |
52 | |
|
53 | 0 | for (; |
54 | 0 | iter != s1.end() && u != upper.end() && l != lower.end(); |
55 | 0 | iter++, u++, l++) |
56 | 0 | { |
57 | 0 | if (*iter != *u && *iter != *l) |
58 | 0 | { |
59 | 0 | return false; |
60 | 0 | } |
61 | 0 | } |
62 | | |
63 | 0 | return u == upper.end() && iter == s1.end(); |
64 | 0 | } Unexecuted instantiation: log4cxx::helpers::StringHelper::equalsIgnoreCase(std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> > const&, std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> > const&, std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> > const&) Unexecuted instantiation: log4cxx::helpers::StringHelper::equalsIgnoreCase(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) |
65 | | |
66 | | |
67 | | |
68 | | LogString StringHelper::toLowerCase(const LogString& s) |
69 | 1.53M | { |
70 | | // Fold ASCII A-Z only. Passing a value not representable as unsigned char |
71 | | // (or EOF) to std::tolower(int) is undefined behaviour; with signed char, |
72 | | // any byte > 0x7F sign-extends to a negative int. The previous implementation |
73 | | // also produced locale-dependent output for the same input, which is wrong |
74 | | // for the callers (class names, color names, column names — all ASCII). |
75 | 1.53M | LogString d; |
76 | 1.53M | d.reserve(s.size()); |
77 | 1.53M | for (auto ch : s) |
78 | 784M | { |
79 | 784M | if (ch >= static_cast<logchar>('A') && ch <= static_cast<logchar>('Z')) |
80 | 82.1M | d.push_back(static_cast<logchar>(ch + ('a' - 'A'))); |
81 | 702M | else |
82 | 702M | d.push_back(ch); |
83 | 784M | } |
84 | 1.53M | return d; |
85 | 1.53M | } log4cxx::helpers::StringHelper::toLowerCase(std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> > const&) Line | Count | Source | 69 | 313k | { | 70 | | // Fold ASCII A-Z only. Passing a value not representable as unsigned char | 71 | | // (or EOF) to std::tolower(int) is undefined behaviour; with signed char, | 72 | | // any byte > 0x7F sign-extends to a negative int. The previous implementation | 73 | | // also produced locale-dependent output for the same input, which is wrong | 74 | | // for the callers (class names, color names, column names — all ASCII). | 75 | 313k | LogString d; | 76 | 313k | d.reserve(s.size()); | 77 | 313k | for (auto ch : s) | 78 | 20.5M | { | 79 | 20.5M | if (ch >= static_cast<logchar>('A') && ch <= static_cast<logchar>('Z')) | 80 | 1.24M | d.push_back(static_cast<logchar>(ch + ('a' - 'A'))); | 81 | 19.2M | else | 82 | 19.2M | d.push_back(ch); | 83 | 20.5M | } | 84 | 313k | return d; | 85 | 313k | } |
log4cxx::helpers::StringHelper::toLowerCase(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) Line | Count | Source | 69 | 1.22M | { | 70 | | // Fold ASCII A-Z only. Passing a value not representable as unsigned char | 71 | | // (or EOF) to std::tolower(int) is undefined behaviour; with signed char, | 72 | | // any byte > 0x7F sign-extends to a negative int. The previous implementation | 73 | | // also produced locale-dependent output for the same input, which is wrong | 74 | | // for the callers (class names, color names, column names — all ASCII). | 75 | 1.22M | LogString d; | 76 | 1.22M | d.reserve(s.size()); | 77 | 1.22M | for (auto ch : s) | 78 | 764M | { | 79 | 764M | if (ch >= static_cast<logchar>('A') && ch <= static_cast<logchar>('Z')) | 80 | 80.8M | d.push_back(static_cast<logchar>(ch + ('a' - 'A'))); | 81 | 683M | else | 82 | 683M | d.push_back(ch); | 83 | 764M | } | 84 | 1.22M | return d; | 85 | 1.22M | } |
|
86 | | |
87 | | LogString StringHelper::trim(const LogString& s) |
88 | 2.07M | { |
89 | 2.07M | LogString::size_type pos = s.find_first_not_of(' '); |
90 | | |
91 | 2.07M | if (pos == std::string::npos) |
92 | 142k | { |
93 | 142k | return LogString(); |
94 | 142k | } |
95 | | |
96 | 1.93M | LogString::size_type n = s.find_last_not_of(' ') - pos + 1; |
97 | 1.93M | return s.substr(pos, n); |
98 | 2.07M | } log4cxx::helpers::StringHelper::trim(std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> > const&) Line | Count | Source | 88 | 590k | { | 89 | 590k | LogString::size_type pos = s.find_first_not_of(' '); | 90 | | | 91 | 590k | if (pos == std::string::npos) | 92 | 12.6k | { | 93 | 12.6k | return LogString(); | 94 | 12.6k | } | 95 | | | 96 | 577k | LogString::size_type n = s.find_last_not_of(' ') - pos + 1; | 97 | 577k | return s.substr(pos, n); | 98 | 590k | } |
log4cxx::helpers::StringHelper::trim(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) Line | Count | Source | 88 | 1.48M | { | 89 | 1.48M | LogString::size_type pos = s.find_first_not_of(' '); | 90 | | | 91 | 1.48M | if (pos == std::string::npos) | 92 | 129k | { | 93 | 129k | return LogString(); | 94 | 129k | } | 95 | | | 96 | 1.35M | LogString::size_type n = s.find_last_not_of(' ') - pos + 1; | 97 | 1.35M | return s.substr(pos, n); | 98 | 1.48M | } |
|
99 | | |
100 | | bool StringHelper::startsWith(const LogString& s, const LogString& prefix) |
101 | 1.53M | { |
102 | 1.53M | if (s.length() < prefix.length()) |
103 | 75.0k | { |
104 | 75.0k | return false; |
105 | 75.0k | } |
106 | | |
107 | 1.45M | return s.compare(0, prefix.length(), prefix) == 0; |
108 | 1.53M | } log4cxx::helpers::StringHelper::startsWith(std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> > const&, std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> > const&) Line | Count | Source | 101 | 279k | { | 102 | 279k | if (s.length() < prefix.length()) | 103 | 7.10k | { | 104 | 7.10k | return false; | 105 | 7.10k | } | 106 | | | 107 | 271k | return s.compare(0, prefix.length(), prefix) == 0; | 108 | 279k | } |
log4cxx::helpers::StringHelper::startsWith(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) Line | Count | Source | 101 | 1.25M | { | 102 | 1.25M | if (s.length() < prefix.length()) | 103 | 67.9k | { | 104 | 67.9k | return false; | 105 | 67.9k | } | 106 | | | 107 | 1.18M | return s.compare(0, prefix.length(), prefix) == 0; | 108 | 1.25M | } |
|
109 | | |
110 | | bool StringHelper::endsWith(const LogString& s, const LogString& suffix) |
111 | 310k | { |
112 | 310k | if (suffix.length() <= s.length()) |
113 | 14.4k | { |
114 | 14.4k | return s.compare(s.length() - suffix.length(), suffix.length(), suffix) == 0; |
115 | 14.4k | } |
116 | | |
117 | 296k | return false; |
118 | 310k | } log4cxx::helpers::StringHelper::endsWith(std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> > const&, std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> > const&) Line | Count | Source | 111 | 163k | { | 112 | 163k | if (suffix.length() <= s.length()) | 113 | 9.61k | { | 114 | 9.61k | return s.compare(s.length() - suffix.length(), suffix.length(), suffix) == 0; | 115 | 9.61k | } | 116 | | | 117 | 153k | return false; | 118 | 163k | } |
log4cxx::helpers::StringHelper::endsWith(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) Line | Count | Source | 111 | 147k | { | 112 | 147k | if (suffix.length() <= s.length()) | 113 | 4.84k | { | 114 | 4.84k | return s.compare(s.length() - suffix.length(), suffix.length(), suffix) == 0; | 115 | 4.84k | } | 116 | | | 117 | 142k | return false; | 118 | 147k | } |
|
119 | | |
120 | | |
121 | | int StringHelper::toInt(const LogString& s) |
122 | 176k | { |
123 | 176k | return std::stoi(s); |
124 | 176k | } log4cxx::helpers::StringHelper::toInt(std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> > const&) Line | Count | Source | 122 | 63.6k | { | 123 | 63.6k | return std::stoi(s); | 124 | 63.6k | } |
log4cxx::helpers::StringHelper::toInt(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) Line | Count | Source | 122 | 113k | { | 123 | 113k | return std::stoi(s); | 124 | 113k | } |
|
125 | | |
126 | | int64_t StringHelper::toInt64(const LogString& s) |
127 | 0 | { |
128 | 0 | return std::stoll(s); |
129 | 0 | } Unexecuted instantiation: log4cxx::helpers::StringHelper::toInt64(std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> > const&) Unexecuted instantiation: log4cxx::helpers::StringHelper::toInt64(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) |
130 | | |
131 | | void StringHelper::toString(int n, LogString& dst) |
132 | 10.5M | { |
133 | | #if LOG4CXX_LOGCHAR_IS_WCHAR |
134 | | dst.append(std::to_wstring(n)); |
135 | | #elif LOG4CXX_LOGCHAR_IS_UTF8 |
136 | | dst.append(std::to_string(n)); |
137 | | #else |
138 | | Transcoder::decode(std::to_string(n), dst); |
139 | | #endif |
140 | 10.5M | } log4cxx::helpers::StringHelper::toString(int, std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> >&) Line | Count | Source | 132 | 3.98M | { | 133 | 3.98M | #if LOG4CXX_LOGCHAR_IS_WCHAR | 134 | 3.98M | dst.append(std::to_wstring(n)); | 135 | | #elif LOG4CXX_LOGCHAR_IS_UTF8 | 136 | | dst.append(std::to_string(n)); | 137 | | #else | 138 | | Transcoder::decode(std::to_string(n), dst); | 139 | | #endif | 140 | 3.98M | } |
log4cxx::helpers::StringHelper::toString(int, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&) Line | Count | Source | 132 | 6.56M | { | 133 | | #if LOG4CXX_LOGCHAR_IS_WCHAR | 134 | | dst.append(std::to_wstring(n)); | 135 | | #elif LOG4CXX_LOGCHAR_IS_UTF8 | 136 | | dst.append(std::to_string(n)); | 137 | | #else | 138 | | Transcoder::decode(std::to_string(n), dst); | 139 | | #endif | 140 | 6.56M | } |
|
141 | | |
142 | | void StringHelper::toString(bool val, LogString& dst) |
143 | 344 | { |
144 | 344 | if (val) |
145 | 315 | { |
146 | 315 | dst.append(LOG4CXX_STR("true")); |
147 | 315 | } |
148 | 29 | else |
149 | 29 | { |
150 | 29 | dst.append(LOG4CXX_STR("false")); |
151 | 29 | } |
152 | 344 | } log4cxx::helpers::StringHelper::toString(bool, std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> >&) Line | Count | Source | 143 | 241 | { | 144 | 241 | if (val) | 145 | 229 | { | 146 | 229 | dst.append(LOG4CXX_STR("true")); | 147 | 229 | } | 148 | 12 | else | 149 | 12 | { | 150 | 12 | dst.append(LOG4CXX_STR("false")); | 151 | 12 | } | 152 | 241 | } |
log4cxx::helpers::StringHelper::toString(bool, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&) Line | Count | Source | 143 | 103 | { | 144 | 103 | if (val) | 145 | 86 | { | 146 | 86 | dst.append(LOG4CXX_STR("true")); | 147 | 86 | } | 148 | 17 | else | 149 | 17 | { | 150 | 17 | dst.append(LOG4CXX_STR("false")); | 151 | 17 | } | 152 | 103 | } |
|
153 | | |
154 | | |
155 | | void StringHelper::toString(int64_t n, LogString& dst) |
156 | 4.12M | { |
157 | | #if LOG4CXX_LOGCHAR_IS_WCHAR |
158 | | dst.append(std::to_wstring(n)); |
159 | | #elif LOG4CXX_LOGCHAR_IS_UTF8 |
160 | | dst.append(std::to_string(n)); |
161 | | #else |
162 | | Transcoder::decode(std::to_string(n), dst); |
163 | | #endif |
164 | 4.12M | } log4cxx::helpers::StringHelper::toString(long, std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> >&) Line | Count | Source | 156 | 744k | { | 157 | 744k | #if LOG4CXX_LOGCHAR_IS_WCHAR | 158 | 744k | dst.append(std::to_wstring(n)); | 159 | | #elif LOG4CXX_LOGCHAR_IS_UTF8 | 160 | | dst.append(std::to_string(n)); | 161 | | #else | 162 | | Transcoder::decode(std::to_string(n), dst); | 163 | | #endif | 164 | 744k | } |
log4cxx::helpers::StringHelper::toString(long, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&) Line | Count | Source | 156 | 3.38M | { | 157 | | #if LOG4CXX_LOGCHAR_IS_WCHAR | 158 | | dst.append(std::to_wstring(n)); | 159 | | #elif LOG4CXX_LOGCHAR_IS_UTF8 | 160 | | dst.append(std::to_string(n)); | 161 | | #else | 162 | | Transcoder::decode(std::to_string(n), dst); | 163 | | #endif | 164 | 3.38M | } |
|
165 | | |
166 | | |
167 | | void StringHelper::toString(size_t n, LogString& dst) |
168 | 228k | { |
169 | 228k | auto ull = static_cast<unsigned long long>(n); |
170 | | #if LOG4CXX_LOGCHAR_IS_WCHAR |
171 | | dst.append(std::to_wstring(ull)); |
172 | | #elif LOG4CXX_LOGCHAR_IS_UTF8 |
173 | | dst.append(std::to_string(ull)); |
174 | | #else |
175 | | Transcoder::decode(std::to_string(ull), dst); |
176 | | #endif |
177 | 228k | } log4cxx::helpers::StringHelper::toString(unsigned long, std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> >&) Line | Count | Source | 168 | 97.0k | { | 169 | 97.0k | auto ull = static_cast<unsigned long long>(n); | 170 | 97.0k | #if LOG4CXX_LOGCHAR_IS_WCHAR | 171 | 97.0k | dst.append(std::to_wstring(ull)); | 172 | | #elif LOG4CXX_LOGCHAR_IS_UTF8 | 173 | | dst.append(std::to_string(ull)); | 174 | | #else | 175 | | Transcoder::decode(std::to_string(ull), dst); | 176 | | #endif | 177 | 97.0k | } |
log4cxx::helpers::StringHelper::toString(unsigned long, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&) Line | Count | Source | 168 | 131k | { | 169 | 131k | auto ull = static_cast<unsigned long long>(n); | 170 | | #if LOG4CXX_LOGCHAR_IS_WCHAR | 171 | | dst.append(std::to_wstring(ull)); | 172 | | #elif LOG4CXX_LOGCHAR_IS_UTF8 | 173 | | dst.append(std::to_string(ull)); | 174 | | #else | 175 | | Transcoder::decode(std::to_string(ull), dst); | 176 | | #endif | 177 | 131k | } |
|
178 | | |
179 | | LogString StringHelper::format(const LogString& pattern, const std::vector<LogString>& params) |
180 | 0 | { |
181 | |
|
182 | 0 | LogString result; |
183 | 0 | LogString::size_type i = 0; |
184 | |
|
185 | 0 | while (i < pattern.length()) |
186 | 0 | { |
187 | 0 | if (i + 2 < pattern.length() && |
188 | 0 | pattern[i] == 0x7B /* '{' */ && pattern[i + 1] >= 0x30 /* '0' */ && |
189 | 0 | pattern[i + 1] <= 0x39 /* '9' */ && pattern[i + 2] == 0x7D /* '}' */) |
190 | 0 | { |
191 | 0 | LogString::size_type arg = pattern[i + 1] - 0x30 /* '0' */; |
192 | 0 | if (arg < params.size()) |
193 | 0 | { |
194 | 0 | result = result + params[arg]; |
195 | 0 | i += 3; |
196 | 0 | continue; |
197 | 0 | } |
198 | 0 | } |
199 | 0 | result = result + pattern[i]; |
200 | 0 | i++; |
201 | 0 | } |
202 | |
|
203 | 0 | return result; |
204 | 0 | } Unexecuted instantiation: log4cxx::helpers::StringHelper::format(std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> > const&, std::__1::vector<std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> >, std::__1::allocator<std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> > > > const&) Unexecuted instantiation: log4cxx::helpers::StringHelper::format(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > const&) |
205 | | |