Coverage Report

Created: 2026-02-26 06:58

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