Coverage Report

Created: 2025-07-01 06:08

/src/logging-log4cxx/src/main/cpp/stringhelper.cpp
Line
Count
Source (jump to first uncovered line)
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
9.80k
{
33
9.80k
  for (const auto& item : s1)
34
52.4k
  {
35
52.4k
    if (0 == item || // OSS-Fuzz makes strings with embedded NUL characters
36
52.4k
      (item != *upper && item != *lower))
37
3.23k
    {
38
3.23k
      return false;
39
3.23k
    }
40
49.2k
    ++upper;
41
49.2k
    ++lower;
42
49.2k
  }
43
44
6.56k
  return 0 == *upper;
45
9.80k
}
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
}
65
66
67
68
LogString StringHelper::toLowerCase(const LogString& s)
69
228
{
70
228
  LogString d;
71
228
  std::transform(s.begin(), s.end(),
72
228
    std::insert_iterator<LogString>(d, d.begin()), tolower);
73
228
  return d;
74
228
}
75
76
LogString StringHelper::trim(const LogString& s)
77
0
{
78
0
  LogString::size_type pos = s.find_first_not_of(' ');
79
80
0
  if (pos == std::string::npos)
81
0
  {
82
0
    return LogString();
83
0
  }
84
85
0
  LogString::size_type n = s.find_last_not_of(' ') - pos + 1;
86
0
  return s.substr(pos, n);
87
0
}
88
89
bool StringHelper::startsWith(const LogString& s, const LogString& prefix)
90
0
{
91
0
  if (s.length() < prefix.length())
92
0
  {
93
0
    return false;
94
0
  }
95
96
0
  return s.compare(0, prefix.length(), prefix) == 0;
97
0
}
98
99
bool StringHelper::endsWith(const LogString& s, const LogString& suffix)
100
0
{
101
0
  if (suffix.length() <= s.length())
102
0
  {
103
0
    return s.compare(s.length() - suffix.length(), suffix.length(), suffix) == 0;
104
0
  }
105
106
0
  return false;
107
0
}
108
109
110
int StringHelper::toInt(const LogString& s)
111
0
{
112
#if LOG4CXX_LOGCHAR_IS_UNICHAR
113
  std::string as;
114
  Transcoder::encode(s, as);
115
  return std::stoi(as);
116
#else
117
0
  return std::stoi(s);
118
0
#endif
119
0
}
120
121
int64_t StringHelper::toInt64(const LogString& s)
122
0
{
123
#if LOG4CXX_LOGCHAR_IS_UNICHAR
124
  std::string as;
125
  Transcoder::encode(s, as);
126
  return std::stoll(as);
127
#else
128
0
  return std::stoll(s);
129
0
#endif
130
0
}
131
132
void StringHelper::toString(int n, Pool& pool, LogString& dst)
133
13.8k
{
134
13.8k
#if LOG4CXX_LOGCHAR_IS_WCHAR
135
13.8k
  dst.append(std::to_wstring(n));
136
#else
137
  Transcoder::decode(std::to_string(n), dst);
138
#endif
139
13.8k
}
140
141
void StringHelper::toString(bool val, LogString& dst)
142
0
{
143
0
  if (val)
144
0
  {
145
0
    dst.append(LOG4CXX_STR("true"));
146
0
  }
147
0
  else
148
0
  {
149
0
    dst.append(LOG4CXX_STR("false"));
150
0
  }
151
0
}
152
153
154
void StringHelper::toString(int64_t n, Pool& pool, LogString& dst)
155
0
{
156
0
#if LOG4CXX_LOGCHAR_IS_WCHAR
157
0
  dst.append(std::to_wstring(n));
158
#else
159
  Transcoder::decode(std::to_string(n), dst);
160
#endif
161
0
}
162
163
164
void StringHelper::toString(size_t n, Pool& pool, LogString& dst)
165
0
{
166
0
#if LOG4CXX_LOGCHAR_IS_WCHAR
167
0
  dst.append(std::to_wstring(n));
168
#else
169
  Transcoder::decode(std::to_string(n), dst);
170
#endif
171
0
}
172
173
LogString StringHelper::format(const LogString& pattern, const std::vector<LogString>& params)
174
0
{
175
176
0
  LogString result;
177
0
  int i = 0;
178
179
0
  while (pattern[i] != 0)
180
0
  {
181
0
    if (pattern[i] == 0x7B /* '{' */ && pattern[i + 1] >= 0x30 /* '0' */ &&
182
0
      pattern[i + 1] <= 0x39 /* '9' */ && pattern[i + 2] == 0x7D /* '}' */)
183
0
    {
184
0
      int arg = pattern[i + 1] - 0x30 /* '0' */;
185
0
      result = result + params[arg];
186
0
      i += 3;
187
0
    }
188
0
    else
189
0
    {
190
0
      result = result + pattern[i];
191
0
      i++;
192
0
    }
193
0
  }
194
195
0
  return result;
196
0
}
197