Coverage Report

Created: 2026-06-10 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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
149k
{
33
149k
  for (const auto& item : s1)
34
364k
  {
35
364k
    if (0 == item || // OSS-Fuzz makes strings with embedded NUL characters
36
363k
      (item != *upper && item != *lower))
37
115k
    {
38
115k
      return false;
39
115k
    }
40
249k
    ++upper;
41
249k
    ++lower;
42
249k
  }
43
44
34.2k
  return 0 == *upper;
45
149k
}
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
74.9k
{
33
74.9k
  for (const auto& item : s1)
34
166k
  {
35
166k
    if (0 == item || // OSS-Fuzz makes strings with embedded NUL characters
36
166k
      (item != *upper && item != *lower))
37
59.6k
    {
38
59.6k
      return false;
39
59.6k
    }
40
106k
    ++upper;
41
106k
    ++lower;
42
106k
  }
43
44
15.2k
  return 0 == *upper;
45
74.9k
}
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
74.2k
{
33
74.2k
  for (const auto& item : s1)
34
197k
  {
35
197k
    if (0 == item || // OSS-Fuzz makes strings with embedded NUL characters
36
196k
      (item != *upper && item != *lower))
37
55.3k
    {
38
55.3k
      return false;
39
55.3k
    }
40
142k
    ++upper;
41
142k
    ++lower;
42
142k
  }
43
44
18.9k
  return 0 == *upper;
45
74.2k
}
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<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&)
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&)
65
66
67
68
LogString StringHelper::toLowerCase(const LogString& s)
69
4.00k
{
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
4.00k
  LogString d;
76
4.00k
  d.reserve(s.size());
77
4.00k
  for (auto ch : s)
78
2.92M
  {
79
2.92M
    if (ch >= static_cast<logchar>('A') && ch <= static_cast<logchar>('Z'))
80
34.0k
      d.push_back(static_cast<logchar>(ch + ('a' - 'A')));
81
2.89M
    else
82
2.89M
      d.push_back(ch);
83
2.92M
  }
84
4.00k
  return d;
85
4.00k
}
log4cxx::helpers::StringHelper::toLowerCase(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Line
Count
Source
69
2.09k
{
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
2.09k
  LogString d;
76
2.09k
  d.reserve(s.size());
77
2.09k
  for (auto ch : s)
78
2.59M
  {
79
2.59M
    if (ch >= static_cast<logchar>('A') && ch <= static_cast<logchar>('Z'))
80
26.9k
      d.push_back(static_cast<logchar>(ch + ('a' - 'A')));
81
2.56M
    else
82
2.56M
      d.push_back(ch);
83
2.59M
  }
84
2.09k
  return d;
85
2.09k
}
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
1.91k
{
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.91k
  LogString d;
76
1.91k
  d.reserve(s.size());
77
1.91k
  for (auto ch : s)
78
330k
  {
79
330k
    if (ch >= static_cast<logchar>('A') && ch <= static_cast<logchar>('Z'))
80
7.15k
      d.push_back(static_cast<logchar>(ch + ('a' - 'A')));
81
323k
    else
82
323k
      d.push_back(ch);
83
330k
  }
84
1.91k
  return d;
85
1.91k
}
86
87
LogString StringHelper::trim(const LogString& s)
88
10.1k
{
89
10.1k
  LogString::size_type pos = s.find_first_not_of(' ');
90
91
10.1k
  if (pos == std::string::npos)
92
641
  {
93
641
    return LogString();
94
641
  }
95
96
9.47k
  LogString::size_type n = s.find_last_not_of(' ') - pos + 1;
97
9.47k
  return s.substr(pos, n);
98
10.1k
}
log4cxx::helpers::StringHelper::trim(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Line
Count
Source
88
5.84k
{
89
5.84k
  LogString::size_type pos = s.find_first_not_of(' ');
90
91
5.84k
  if (pos == std::string::npos)
92
329
  {
93
329
    return LogString();
94
329
  }
95
96
5.51k
  LogString::size_type n = s.find_last_not_of(' ') - pos + 1;
97
5.51k
  return s.substr(pos, n);
98
5.84k
}
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
4.27k
{
89
4.27k
  LogString::size_type pos = s.find_first_not_of(' ');
90
91
4.27k
  if (pos == std::string::npos)
92
312
  {
93
312
    return LogString();
94
312
  }
95
96
3.95k
  LogString::size_type n = s.find_last_not_of(' ') - pos + 1;
97
3.95k
  return s.substr(pos, n);
98
4.27k
}
99
100
bool StringHelper::startsWith(const LogString& s, const LogString& prefix)
101
1.20k
{
102
1.20k
  if (s.length() < prefix.length())
103
642
  {
104
642
    return false;
105
642
  }
106
107
560
  return s.compare(0, prefix.length(), prefix) == 0;
108
1.20k
}
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.20k
{
102
1.20k
  if (s.length() < prefix.length())
103
642
  {
104
642
    return false;
105
642
  }
106
107
560
  return s.compare(0, prefix.length(), prefix) == 0;
108
1.20k
}
Unexecuted instantiation: 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&)
109
110
bool StringHelper::endsWith(const LogString& s, const LogString& suffix)
111
0
{
112
0
  if (suffix.length() <= s.length())
113
0
  {
114
0
    return s.compare(s.length() - suffix.length(), suffix.length(), suffix) == 0;
115
0
  }
116
117
0
  return false;
118
0
}
Unexecuted instantiation: 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&)
Unexecuted instantiation: 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&)
119
120
121
int StringHelper::toInt(const LogString& s)
122
33.5k
{
123
#if LOG4CXX_LOGCHAR_IS_UNICHAR
124
  std::string as;
125
  Transcoder::encode(s, as);
126
  return std::stoi(as);
127
#else
128
33.5k
  return std::stoi(s);
129
33.5k
#endif
130
33.5k
}
log4cxx::helpers::StringHelper::toInt(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Line
Count
Source
122
17.1k
{
123
#if LOG4CXX_LOGCHAR_IS_UNICHAR
124
  std::string as;
125
  Transcoder::encode(s, as);
126
  return std::stoi(as);
127
#else
128
17.1k
  return std::stoi(s);
129
17.1k
#endif
130
17.1k
}
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
16.4k
{
123
#if LOG4CXX_LOGCHAR_IS_UNICHAR
124
  std::string as;
125
  Transcoder::encode(s, as);
126
  return std::stoi(as);
127
#else
128
16.4k
  return std::stoi(s);
129
16.4k
#endif
130
16.4k
}
131
132
int64_t StringHelper::toInt64(const LogString& s)
133
0
{
134
#if LOG4CXX_LOGCHAR_IS_UNICHAR
135
  std::string as;
136
  Transcoder::encode(s, as);
137
  return std::stoll(as);
138
#else
139
0
  return std::stoll(s);
140
0
#endif
141
0
}
Unexecuted instantiation: log4cxx::helpers::StringHelper::toInt64(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Unexecuted instantiation: log4cxx::helpers::StringHelper::toInt64(std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> > const&)
142
143
void StringHelper::toString(int n, LogString& dst)
144
756k
{
145
#if LOG4CXX_LOGCHAR_IS_WCHAR
146
  dst.append(std::to_wstring(n));
147
#elif LOG4CXX_LOGCHAR_IS_UTF8
148
  dst.append(std::to_string(n));
149
#else
150
  Transcoder::decode(std::to_string(n), dst);
151
#endif
152
756k
}
log4cxx::helpers::StringHelper::toString(int, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&)
Line
Count
Source
144
531k
{
145
#if LOG4CXX_LOGCHAR_IS_WCHAR
146
  dst.append(std::to_wstring(n));
147
#elif LOG4CXX_LOGCHAR_IS_UTF8
148
  dst.append(std::to_string(n));
149
#else
150
  Transcoder::decode(std::to_string(n), dst);
151
#endif
152
531k
}
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
144
224k
{
145
224k
#if LOG4CXX_LOGCHAR_IS_WCHAR
146
224k
  dst.append(std::to_wstring(n));
147
#elif LOG4CXX_LOGCHAR_IS_UTF8
148
  dst.append(std::to_string(n));
149
#else
150
  Transcoder::decode(std::to_string(n), dst);
151
#endif
152
224k
}
153
154
void StringHelper::toString(bool val, LogString& dst)
155
0
{
156
0
  if (val)
157
0
  {
158
0
    dst.append(LOG4CXX_STR("true"));
159
0
  }
160
0
  else
161
0
  {
162
0
    dst.append(LOG4CXX_STR("false"));
163
0
  }
164
0
}
Unexecuted instantiation: log4cxx::helpers::StringHelper::toString(bool, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&)
Unexecuted instantiation: log4cxx::helpers::StringHelper::toString(bool, std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> >&)
165
166
167
void StringHelper::toString(int64_t n, LogString& dst)
168
6.20k
{
169
#if LOG4CXX_LOGCHAR_IS_WCHAR
170
  dst.append(std::to_wstring(n));
171
#elif LOG4CXX_LOGCHAR_IS_UTF8
172
  dst.append(std::to_string(n));
173
#else
174
  Transcoder::decode(std::to_string(n), dst);
175
#endif
176
6.20k
}
log4cxx::helpers::StringHelper::toString(long, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&)
Line
Count
Source
168
51
{
169
#if LOG4CXX_LOGCHAR_IS_WCHAR
170
  dst.append(std::to_wstring(n));
171
#elif LOG4CXX_LOGCHAR_IS_UTF8
172
  dst.append(std::to_string(n));
173
#else
174
  Transcoder::decode(std::to_string(n), dst);
175
#endif
176
51
}
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
168
6.15k
{
169
6.15k
#if LOG4CXX_LOGCHAR_IS_WCHAR
170
6.15k
  dst.append(std::to_wstring(n));
171
#elif LOG4CXX_LOGCHAR_IS_UTF8
172
  dst.append(std::to_string(n));
173
#else
174
  Transcoder::decode(std::to_string(n), dst);
175
#endif
176
6.15k
}
177
178
179
void StringHelper::toString(size_t n, LogString& dst)
180
5.09k
{
181
#if LOG4CXX_LOGCHAR_IS_WCHAR
182
  dst.append(std::to_wstring(n));
183
#elif LOG4CXX_LOGCHAR_IS_UTF8
184
  dst.append(std::to_string(n));
185
#else
186
  Transcoder::decode(std::to_string(n), dst);
187
#endif
188
5.09k
}
log4cxx::helpers::StringHelper::toString(unsigned long, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&)
Line
Count
Source
180
5.03k
{
181
#if LOG4CXX_LOGCHAR_IS_WCHAR
182
  dst.append(std::to_wstring(n));
183
#elif LOG4CXX_LOGCHAR_IS_UTF8
184
  dst.append(std::to_string(n));
185
#else
186
  Transcoder::decode(std::to_string(n), dst);
187
#endif
188
5.03k
}
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
180
64
{
181
64
#if LOG4CXX_LOGCHAR_IS_WCHAR
182
64
  dst.append(std::to_wstring(n));
183
#elif LOG4CXX_LOGCHAR_IS_UTF8
184
  dst.append(std::to_string(n));
185
#else
186
  Transcoder::decode(std::to_string(n), dst);
187
#endif
188
64
}
189
190
LogString StringHelper::format(const LogString& pattern, const std::vector<LogString>& params)
191
0
{
192
193
0
  LogString result;
194
0
  LogString::size_type i = 0;
195
196
0
  while (i < pattern.length())
197
0
  {
198
0
    if (i + 2 < pattern.length() &&
199
0
      pattern[i] == 0x7B /* '{' */ && pattern[i + 1] >= 0x30 /* '0' */ &&
200
0
      pattern[i + 1] <= 0x39 /* '9' */ && pattern[i + 2] == 0x7D /* '}' */)
201
0
    {
202
0
      LogString::size_type arg = pattern[i + 1] - 0x30 /* '0' */;
203
0
      if (arg < params.size())
204
0
      {
205
0
        result = result + params[arg];
206
0
        i += 3;
207
0
        continue;
208
0
      }
209
0
    }
210
0
    result = result + pattern[i];
211
0
    i++;
212
0
  }
213
214
0
  return result;
215
0
}
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&)
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&)
216
217
#if LOG4CXX_ABI_VERSION <= 15
218
0
void StringHelper::toString(int n, Pool& pool, LogString& dst) { toString(n, dst); }
Unexecuted instantiation: log4cxx::helpers::StringHelper::toString(int, log4cxx::helpers::Pool&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&)
Unexecuted instantiation: log4cxx::helpers::StringHelper::toString(int, log4cxx::helpers::Pool&, std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> >&)
219
0
void StringHelper::toString(int64_t n, Pool& pool, LogString& dst) { toString(n, dst); }
Unexecuted instantiation: log4cxx::helpers::StringHelper::toString(long, log4cxx::helpers::Pool&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&)
Unexecuted instantiation: log4cxx::helpers::StringHelper::toString(long, log4cxx::helpers::Pool&, std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> >&)
220
0
void StringHelper::toString(size_t n, Pool& pool, LogString& dst) { toString(n, dst); }
Unexecuted instantiation: log4cxx::helpers::StringHelper::toString(unsigned long, log4cxx::helpers::Pool&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&)
Unexecuted instantiation: log4cxx::helpers::StringHelper::toString(unsigned long, log4cxx::helpers::Pool&, std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> >&)
221
#endif