Coverage Report

Created: 2026-07-30 07:12

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
11
{
39
  //
40
  //   greatly simplified version checks if
41
  //     character is USACII alpha or number
42
  //
43
11
  return (ch >= 0x41 /* 'A' */ && ch <= 0x5A /* 'Z' */) ||
44
10
    (ch >= 0x61 /* 'a' */ && ch <= 0x7A /* 'z' */) ||
45
5
    (ch >= 0x30 /* '0' */ && ch <= 0x39 /* '9' */);
46
11
}
47
48
bool PatternParser::isUnicodeIdentifierPart(logchar ch)
49
5
{
50
  //
51
  //   greatly simplified version checks if
52
  //     character is USACII alpha or number
53
  //
54
5
  return isUnicodeIdentifierStart(ch)
55
5
    || (ch == 0x5F /* '_' */);
56
5
}
57
58
size_t PatternParser::extractConverter(
59
  logchar lastChar, const LogString& pattern,
60
  LogString::size_type i, LogString& convBuf,
61
  LogString& currentLiteral)
62
6
{
63
6
  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
6
  if (!isUnicodeIdentifierStart(lastChar))
74
0
  {
75
0
    return i;
76
0
  }
77
78
6
  convBuf.append(1, lastChar);
79
80
6
  while (
81
6
    (i < pattern.length())
82
5
    && 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
6
  return i;
92
6
}
93
94
95
size_t PatternParser::extractOptions(const LogString& pattern, LogString::size_type i,
96
  std::vector<LogString>& options)
97
6
{
98
6
  while ((i < pattern.length()) && (pattern[i] == 0x7B /* '{' */))
99
0
  {
100
0
    size_t end = pattern.find(0x7D /* '}' */, i);
101
102
0
    if (end == pattern.npos)
103
0
    {
104
0
      break;
105
0
    }
106
107
0
    LogString r(pattern.substr(i + 1, end - i - 1));
108
0
    options.push_back(r);
109
0
    i = end + 1;
110
0
  }
111
112
6
  return i;
113
6
}
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
1
{
131
1
  PatternConverterList patternConverters;
132
1
  LogString currentLiteral;
133
134
1
  size_t patternLength = pattern.length();
135
1
  int state = LITERAL_STATE;
136
1
  logchar c;
137
1
  size_t i = 0;
138
1
  int minDigitCount{ 0 }, maxDigitCount{ 0 };
139
1
  auto formattingInfo = FormattingInfo::getDefault();
140
141
24
  while (i < patternLength)
142
23
  {
143
23
    c = pattern[i++];
144
145
23
    switch (state)
146
23
    {
147
16
      case LITERAL_STATE:
148
149
        // In literal state, the last char is always a literal.
150
16
        if (i == patternLength)
151
0
        {
152
0
          currentLiteral.append(1, c);
153
154
0
          continue;
155
0
        }
156
157
16
        if (c == ESCAPE_CHAR)
158
6
        {
159
          // peek at the next char.
160
6
          if (pattern[i] == ESCAPE_CHAR)
161
0
          {
162
0
            currentLiteral.append(1, c);
163
0
            i++; // move pointer
164
0
          }
165
6
          else
166
6
          {
167
6
            if (!currentLiteral.empty())
168
4
            {
169
4
              patternConverters.push_back(
170
4
                LiteralPatternConverter::newInstance(currentLiteral));
171
4
              currentLiteral.erase(currentLiteral.begin(), currentLiteral.end());
172
4
            }
173
174
6
            currentLiteral.append(1, c); // append %
175
6
            state = CONVERTER_STATE;
176
6
            formattingInfo = FormattingInfo::getDefault();
177
6
          }
178
6
        }
179
10
        else
180
10
        {
181
10
          currentLiteral.append(1, c);
182
10
        }
183
184
16
        break;
185
186
6
      case CONVERTER_STATE:
187
6
        currentLiteral.append(1, c);
188
189
6
        switch (c)
190
6
        {
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
6
          default:
204
205
6
            if ((c >= 0x30 /* '0' */) && (c <= 0x39 /* '9' */))
206
1
            {
207
1
              formattingInfo = std::make_shared<FormattingInfo>(
208
1
                    formattingInfo->isLeftAligned(), c - 0x30 /* '0' */,
209
1
                    formattingInfo->getMaxLength());
210
1
              state = MIN_STATE;
211
1
              minDigitCount = 1;
212
1
            }
213
5
            else
214
5
            {
215
5
              i = finalizeConverter(
216
5
                  c, pattern, i, currentLiteral, formattingInfo,
217
5
                  rules, patternConverters);
218
219
              // Next pattern is assumed to be a literal.
220
5
              state = LITERAL_STATE;
221
5
              formattingInfo = FormattingInfo::getDefault();
222
223
5
              if (!currentLiteral.empty())
224
0
              {
225
0
                currentLiteral.erase(currentLiteral.begin(), currentLiteral.end());
226
0
              }
227
5
            }
228
6
        } // switch
229
230
6
        break;
231
232
6
      case MIN_STATE:
233
1
        currentLiteral.append(1, c);
234
235
1
        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
1
        else if (c == 0x2E /* '.' */)
244
0
        {
245
0
          state = DOT_STATE;
246
0
        }
247
1
        else
248
1
        {
249
1
          i = finalizeConverter(
250
1
              c, pattern, i, currentLiteral, formattingInfo,
251
1
              rules, patternConverters);
252
1
          state = LITERAL_STATE;
253
1
          formattingInfo = FormattingInfo::getDefault();
254
255
1
          if (!currentLiteral.empty())
256
0
          {
257
0
            currentLiteral.erase(currentLiteral.begin(), currentLiteral.end());
258
0
          }
259
1
        }
260
261
1
        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
23
    } // switch
309
23
  }
310
311
  // while
312
1
  if (currentLiteral.length() != 0)
313
0
  {
314
0
    patternConverters.push_back(
315
0
      LiteralPatternConverter::newInstance(currentLiteral));
316
0
  }
317
1
  return patternConverters;
318
1
}
319
320
321
PatternConverterPtr PatternParser::createConverter(
322
  const LogString& converterId,
323
  LogString& currentLiteral,
324
  const PatternMap& rules,
325
  std::vector<LogString>& options)
326
6
{
327
328
6
  LogString converterName(converterId);
329
330
6
  for (size_t i = converterId.length(); i > 0; i--)
331
6
  {
332
6
    converterName = converterName.substr(0, i);
333
6
    PatternMap::const_iterator iter = rules.find(converterName);
334
335
6
    if (iter != rules.end())
336
6
    {
337
6
      currentLiteral.erase(currentLiteral.begin(),
338
6
        currentLiteral.end() - (converterId.length() - i));
339
6
      return (iter->second)(options);
340
6
    }
341
6
  }
342
343
0
  LogLog::error(LogString(LOG4CXX_STR("Unrecognized format specifier ")) + converterId);
344
345
0
  return PatternConverterPtr();
346
6
}
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
6
{
354
6
  LogString convBuf;
355
6
  i = extractConverter(c, pattern, i, convBuf, currentLiteral);
356
357
6
  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
6
  else
364
6
  {
365
6
    LogString converterId(convBuf);
366
367
6
    std::vector<LogString> options;
368
6
    i = extractOptions(pattern, i, options);
369
370
6
    PatternConverterPtr pc(
371
6
      createConverter(
372
6
        converterId, currentLiteral, rules, options));
373
374
6
    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
6
    else
384
6
    {
385
6
      patternConverters.push_back(pc);
386
6
      pc->setFormattingInfo(formattingInfo);
387
388
6
      if (currentLiteral.length() > 0)
389
0
      {
390
0
        patternConverters.push_back(
391
0
          LiteralPatternConverter::newInstance(currentLiteral));
392
0
      }
393
6
    }
394
6
  }
395
396
6
  if (!currentLiteral.empty())
397
0
  {
398
0
    currentLiteral.erase(currentLiteral.begin(), currentLiteral.end());
399
0
  }
400
401
6
  return i;
402
6
}