Coverage Report

Created: 2026-06-08 06:19

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/logging-log4cxx/src/main/cpp/patternparser.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
#include <log4cxx/logstring.h>
19
#include <log4cxx/pattern/patternparser.h>
20
#include <log4cxx/pattern/literalpatternconverter.h>
21
#include <log4cxx/helpers/loglog.h>
22
23
using namespace LOG4CXX_NS;
24
using namespace LOG4CXX_NS::pattern;
25
using namespace LOG4CXX_NS::helpers;
26
27
const logchar PatternParser::ESCAPE_CHAR = 0x25; // '%'
28
29
30
/**
31
 * Private constructor.
32
 */
33
PatternParser::PatternParser()
34
0
{
35
0
}
36
37
bool PatternParser::isUnicodeIdentifierStart(logchar ch)
38
6.91k
{
39
  //
40
  //   greatly simplified version checks if
41
  //     character is USACII alpha or number
42
  //
43
6.91k
  return (ch >= 0x41 /* 'A' */ && ch <= 0x5A /* 'Z' */) ||
44
6.91k
    (ch >= 0x61 /* 'a' */ && ch <= 0x7A /* 'z' */) ||
45
3.22k
    (ch >= 0x30 /* '0' */ && ch <= 0x39 /* '9' */);
46
6.91k
}
47
48
bool PatternParser::isUnicodeIdentifierPart(logchar ch)
49
3.22k
{
50
  //
51
  //   greatly simplified version checks if
52
  //     character is USACII alpha or number
53
  //
54
3.22k
  return isUnicodeIdentifierStart(ch)
55
3.22k
    || (ch == 0x5F /* '_' */);
56
3.22k
}
57
58
size_t PatternParser::extractConverter(
59
  logchar lastChar, const LogString& pattern,
60
  LogString::size_type i, LogString& convBuf,
61
  LogString& currentLiteral)
62
3.68k
{
63
3.68k
  if (!convBuf.empty())
64
0
  {
65
0
    convBuf.erase(convBuf.begin(), convBuf.end());
66
0
  }
67
68
  // When this method is called, lastChar points to the first character of the
69
  // conversion word. For example:
70
  // For "%hello"     lastChar = 'h'
71
  // For "%-5hello"   lastChar = 'h'
72
  //System.out.println("lastchar is "+lastChar);
73
3.68k
  if (!isUnicodeIdentifierStart(lastChar))
74
0
  {
75
0
    return i;
76
0
  }
77
78
3.68k
  convBuf.append(1, lastChar);
79
80
3.68k
  while (
81
3.68k
    (i < pattern.length())
82
3.22k
    && isUnicodeIdentifierPart(pattern[i]))
83
0
  {
84
0
    convBuf.append(1, pattern[i]);
85
0
    currentLiteral.append(1, pattern[i]);
86
87
    //System.out.println("conv buffer is now ["+convBuf+"].");
88
0
    i++;
89
0
  }
90
91
3.68k
  return i;
92
3.68k
}
93
94
95
size_t PatternParser::extractOptions(const LogString& pattern, LogString::size_type i,
96
  std::vector<LogString>& options)
97
3.68k
{
98
6.45k
  while ((i < pattern.length()) && (pattern[i] == 0x7B /* '{' */))
99
2.76k
  {
100
2.76k
    size_t end = pattern.find(0x7D /* '}' */, i);
101
102
2.76k
    if (end == pattern.npos)
103
0
    {
104
0
      break;
105
0
    }
106
107
2.76k
    LogString r(pattern.substr(i + 1, end - i - 1));
108
2.76k
    options.push_back(r);
109
2.76k
    i = end + 1;
110
2.76k
  }
111
112
3.68k
  return i;
113
3.68k
}
114
115
#if LOG4CXX_ABI_VERSION <= 15
116
void PatternParser::parse(
117
  const LogString& pattern,
118
  std::vector<PatternConverterPtr>& patternConverters,
119
  std::vector<FormattingInfoPtr>& formattingInfos,
120
  const PatternMap& rules)
121
0
{
122
0
  patternConverters = parse(pattern, rules);
123
0
}
124
#endif
125
126
PatternConverterList PatternParser::parse
127
  ( const LogString&      pattern
128
  , const PatternMap&     rules
129
  )
130
2.76k
{
131
2.76k
  PatternConverterList patternConverters;
132
2.76k
  LogString currentLiteral;
133
134
2.76k
  size_t patternLength = pattern.length();
135
2.76k
  int state = LITERAL_STATE;
136
2.76k
  logchar c;
137
2.76k
  size_t i = 0;
138
2.76k
  int minDigitCount{ 0 }, maxDigitCount{ 0 };
139
2.76k
  auto formattingInfo = FormattingInfo::getDefault();
140
141
31.2k
  while (i < patternLength)
142
28.5k
  {
143
28.5k
    c = pattern[i++];
144
145
28.5k
    switch (state)
146
28.5k
    {
147
24.8k
      case LITERAL_STATE:
148
149
        // In literal state, the last char is always a literal.
150
24.8k
        if (i == patternLength)
151
2.30k
        {
152
2.30k
          currentLiteral.append(1, c);
153
154
2.30k
          continue;
155
2.30k
        }
156
157
22.5k
        if (c == ESCAPE_CHAR)
158
3.68k
        {
159
          // peek at the next char.
160
3.68k
          if (pattern[i] == ESCAPE_CHAR)
161
0
          {
162
0
            currentLiteral.append(1, c);
163
0
            i++; // move pointer
164
0
          }
165
3.68k
          else
166
3.68k
          {
167
3.68k
            if (!currentLiteral.empty())
168
2.76k
            {
169
2.76k
              patternConverters.push_back(
170
2.76k
                LiteralPatternConverter::newInstance(currentLiteral));
171
2.76k
              currentLiteral.erase(currentLiteral.begin(), currentLiteral.end());
172
2.76k
            }
173
174
3.68k
            currentLiteral.append(1, c); // append %
175
3.68k
            state = CONVERTER_STATE;
176
3.68k
            formattingInfo = FormattingInfo::getDefault();
177
3.68k
          }
178
3.68k
        }
179
18.8k
        else
180
18.8k
        {
181
18.8k
          currentLiteral.append(1, c);
182
18.8k
        }
183
184
22.5k
        break;
185
186
3.68k
      case CONVERTER_STATE:
187
3.68k
        currentLiteral.append(1, c);
188
189
3.68k
        switch (c)
190
3.68k
        {
191
0
          case 0x2D: // '-'
192
0
            formattingInfo = std::make_shared<FormattingInfo>(
193
0
                  true, formattingInfo->getMinLength(),
194
0
                  formattingInfo->getMaxLength());
195
196
0
            break;
197
198
0
          case 0x2E: // '.'
199
0
            state = DOT_STATE;
200
201
0
            break;
202
203
3.68k
          default:
204
205
3.68k
            if ((c >= 0x30 /* '0' */) && (c <= 0x39 /* '9' */))
206
0
            {
207
0
              formattingInfo = std::make_shared<FormattingInfo>(
208
0
                    formattingInfo->isLeftAligned(), c - 0x30 /* '0' */,
209
0
                    formattingInfo->getMaxLength());
210
0
              state = MIN_STATE;
211
0
              minDigitCount = 1;
212
0
            }
213
3.68k
            else
214
3.68k
            {
215
3.68k
              i = finalizeConverter(
216
3.68k
                  c, pattern, i, currentLiteral, formattingInfo,
217
3.68k
                  rules, patternConverters);
218
219
              // Next pattern is assumed to be a literal.
220
3.68k
              state = LITERAL_STATE;
221
3.68k
              formattingInfo = FormattingInfo::getDefault();
222
223
3.68k
              if (!currentLiteral.empty())
224
0
              {
225
0
                currentLiteral.erase(currentLiteral.begin(), currentLiteral.end());
226
0
              }
227
3.68k
            }
228
3.68k
        } // switch
229
230
3.68k
        break;
231
232
3.68k
      case MIN_STATE:
233
0
        currentLiteral.append(1, c);
234
235
0
        if ((c >= 0x30 /* '0' */) && (c <= 0x39 /* '9' */) && minDigitCount < 3)
236
0
        {
237
0
          formattingInfo = std::make_shared<FormattingInfo>(
238
0
                formattingInfo->isLeftAligned(),
239
0
                (formattingInfo->getMinLength() * 10) + (c - 0x30 /* '0' */),
240
0
                formattingInfo->getMaxLength());
241
0
          ++minDigitCount;
242
0
        }
243
0
        else if (c == 0x2E /* '.' */)
244
0
        {
245
0
          state = DOT_STATE;
246
0
        }
247
0
        else
248
0
        {
249
0
          i = finalizeConverter(
250
0
              c, pattern, i, currentLiteral, formattingInfo,
251
0
              rules, patternConverters);
252
0
          state = LITERAL_STATE;
253
0
          formattingInfo = FormattingInfo::getDefault();
254
255
0
          if (!currentLiteral.empty())
256
0
          {
257
0
            currentLiteral.erase(currentLiteral.begin(), currentLiteral.end());
258
0
          }
259
0
        }
260
261
0
        break;
262
263
0
      case DOT_STATE:
264
0
        currentLiteral.append(1, c);
265
266
0
        if ((c >= 0x30 /* '0' */) && (c <= 0x39 /* '9' */))
267
0
        {
268
0
          formattingInfo = std::make_shared<FormattingInfo>(
269
0
                formattingInfo->isLeftAligned(), formattingInfo->getMinLength(),
270
0
                c - 0x30 /* '0' */);
271
0
          state = MAX_STATE;
272
0
          maxDigitCount = 1;
273
0
        }
274
0
        else
275
0
        {
276
0
          LogLog::error(LOG4CXX_STR("Error in pattern, was expecting digit."));
277
278
0
          state = LITERAL_STATE;
279
0
        }
280
281
0
        break;
282
283
0
      case MAX_STATE:
284
0
        currentLiteral.append(1, c);
285
286
0
        if ((c >= 0x30 /* '0' */) && (c <= 0x39 /* '9' */) && maxDigitCount < 3)
287
0
        {
288
0
          formattingInfo = std::make_shared<FormattingInfo>(
289
0
                formattingInfo->isLeftAligned(), formattingInfo->getMinLength(),
290
0
                (formattingInfo->getMaxLength() * 10) + (c - 0x30 /* '0' */));
291
0
          ++maxDigitCount;
292
0
        }
293
0
        else
294
0
        {
295
0
          i = finalizeConverter(
296
0
              c, pattern, i, currentLiteral, formattingInfo,
297
0
              rules, patternConverters);
298
0
          state = LITERAL_STATE;
299
0
          formattingInfo = FormattingInfo::getDefault();
300
301
0
          if (!currentLiteral.empty())
302
0
          {
303
0
            currentLiteral.erase(currentLiteral.begin(), currentLiteral.end());
304
0
          }
305
0
        }
306
307
0
        break;
308
28.5k
    } // switch
309
28.5k
  }
310
311
  // while
312
2.76k
  if (currentLiteral.length() != 0)
313
2.30k
  {
314
2.30k
    patternConverters.push_back(
315
2.30k
      LiteralPatternConverter::newInstance(currentLiteral));
316
2.30k
  }
317
2.76k
  return patternConverters;
318
2.76k
}
319
320
321
PatternConverterPtr PatternParser::createConverter(
322
  const LogString& converterId,
323
  LogString& currentLiteral,
324
  const PatternMap& rules,
325
  std::vector<LogString>& options)
326
3.68k
{
327
328
3.68k
  LogString converterName(converterId);
329
330
3.68k
  for (size_t i = converterId.length(); i > 0; i--)
331
3.68k
  {
332
3.68k
    converterName = converterName.substr(0, i);
333
3.68k
    PatternMap::const_iterator iter = rules.find(converterName);
334
335
3.68k
    if (iter != rules.end())
336
3.68k
    {
337
3.68k
      currentLiteral.erase(currentLiteral.begin(),
338
3.68k
        currentLiteral.end() - (converterId.length() - i));
339
3.68k
      return (iter->second)(options);
340
3.68k
    }
341
3.68k
  }
342
343
0
  LogLog::error(LogString(LOG4CXX_STR("Unrecognized format specifier ")) + converterId);
344
345
0
  return PatternConverterPtr();
346
3.68k
}
347
348
size_t PatternParser::finalizeConverter(
349
  logchar c, const LogString& pattern, size_t i,
350
  LogString& currentLiteral, const FormattingInfoPtr& formattingInfo,
351
  const PatternMap&  rules,
352
  PatternConverterList& patternConverters)
353
3.68k
{
354
3.68k
  LogString convBuf;
355
3.68k
  i = extractConverter(c, pattern, i, convBuf, currentLiteral);
356
357
3.68k
  if (convBuf.empty())
358
0
  {
359
0
    LogLog::error(LOG4CXX_STR("Empty conversion specifier"));
360
0
    patternConverters.push_back(
361
0
      LiteralPatternConverter::newInstance(currentLiteral));
362
0
  }
363
3.68k
  else
364
3.68k
  {
365
3.68k
    LogString converterId(convBuf);
366
367
3.68k
    std::vector<LogString> options;
368
3.68k
    i = extractOptions(pattern, i, options);
369
370
3.68k
    PatternConverterPtr pc(
371
3.68k
      createConverter(
372
3.68k
        converterId, currentLiteral, rules, options));
373
374
3.68k
    if (pc == NULL)
375
0
    {
376
0
      LogString msg(LOG4CXX_STR("Unrecognized conversion specifier ["));
377
0
      msg.append(converterId);
378
0
      msg.append(LOG4CXX_STR("] in conversion pattern."));
379
0
      LogLog::error(msg);
380
0
      patternConverters.push_back(
381
0
        LiteralPatternConverter::newInstance(currentLiteral));
382
0
    }
383
3.68k
    else
384
3.68k
    {
385
3.68k
      patternConverters.push_back(pc);
386
3.68k
      pc->setFormattingInfo(formattingInfo);
387
388
3.68k
      if (currentLiteral.length() > 0)
389
0
      {
390
0
        patternConverters.push_back(
391
0
          LiteralPatternConverter::newInstance(currentLiteral));
392
0
      }
393
3.68k
    }
394
3.68k
  }
395
396
3.68k
  if (!currentLiteral.empty())
397
0
  {
398
0
    currentLiteral.erase(currentLiteral.begin(), currentLiteral.end());
399
0
  }
400
401
3.68k
  return i;
402
3.68k
}