Coverage Report

Created: 2025-08-29 06:29

/src/logging-log4cxx/src/main/cpp/patternparser.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
#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
11
    (ch >= 0x61 /* 'a' */ && ch <= 0x7A /* 'z' */) ||
45
11
    (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
6
    && 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
void PatternParser::parse(
116
  const LogString& pattern,
117
  std::vector<PatternConverterPtr>& patternConverters,
118
  std::vector<FormattingInfoPtr>& formattingInfos,
119
  const PatternMap& rules)
120
1
{
121
122
1
  LogString currentLiteral;
123
124
1
  size_t patternLength = pattern.length();
125
1
  int state = LITERAL_STATE;
126
1
  logchar c;
127
1
  size_t i = 0;
128
1
  int minDigitCount{ 0 }, maxDigitCount{ 0 };
129
1
  FormattingInfoPtr formattingInfo(FormattingInfo::getDefault());
130
131
24
  while (i < patternLength)
132
23
  {
133
23
    c = pattern[i++];
134
135
23
    switch (state)
136
23
    {
137
16
      case LITERAL_STATE:
138
139
        // In literal state, the last char is always a literal.
140
16
        if (i == patternLength)
141
0
        {
142
0
          currentLiteral.append(1, c);
143
144
0
          continue;
145
0
        }
146
147
16
        if (c == ESCAPE_CHAR)
148
6
        {
149
          // peek at the next char.
150
6
          if (pattern[i] == ESCAPE_CHAR)
151
0
          {
152
0
            currentLiteral.append(1, c);
153
0
            i++; // move pointer
154
0
          }
155
6
          else
156
6
          {
157
6
            if (!currentLiteral.empty())
158
4
            {
159
4
              patternConverters.push_back(
160
4
                LiteralPatternConverter::newInstance(currentLiteral));
161
4
              formattingInfos.push_back(FormattingInfo::getDefault());
162
4
              currentLiteral.erase(currentLiteral.begin(), currentLiteral.end());
163
4
            }
164
165
6
            currentLiteral.append(1, c); // append %
166
6
            state = CONVERTER_STATE;
167
6
            formattingInfo = FormattingInfo::getDefault();
168
6
          }
169
6
        }
170
10
        else
171
10
        {
172
10
          currentLiteral.append(1, c);
173
10
        }
174
175
16
        break;
176
177
6
      case CONVERTER_STATE:
178
6
        currentLiteral.append(1, c);
179
180
6
        switch (c)
181
6
        {
182
0
          case 0x2D: // '-'
183
0
            formattingInfo = std::make_shared<FormattingInfo>(
184
0
                  true, formattingInfo->getMinLength(),
185
0
                  formattingInfo->getMaxLength());
186
187
0
            break;
188
189
0
          case 0x2E: // '.'
190
0
            state = DOT_STATE;
191
192
0
            break;
193
194
6
          default:
195
196
6
            if ((c >= 0x30 /* '0' */) && (c <= 0x39 /* '9' */))
197
1
            {
198
1
              formattingInfo = std::make_shared<FormattingInfo>(
199
1
                    formattingInfo->isLeftAligned(), c - 0x30 /* '0' */,
200
1
                    formattingInfo->getMaxLength());
201
1
              state = MIN_STATE;
202
1
              minDigitCount = 1;
203
1
            }
204
5
            else
205
5
            {
206
5
              i = finalizeConverter(
207
5
                  c, pattern, i, currentLiteral, formattingInfo,
208
5
                  rules, patternConverters, formattingInfos);
209
210
              // Next pattern is assumed to be a literal.
211
5
              state = LITERAL_STATE;
212
5
              formattingInfo = FormattingInfo::getDefault();
213
214
5
              if (!currentLiteral.empty())
215
0
              {
216
0
                currentLiteral.erase(currentLiteral.begin(), currentLiteral.end());
217
0
              }
218
5
            }
219
6
        } // switch
220
221
6
        break;
222
223
6
      case MIN_STATE:
224
1
        currentLiteral.append(1, c);
225
226
1
        if ((c >= 0x30 /* '0' */) && (c <= 0x39 /* '9' */) && minDigitCount < 3)
227
0
        {
228
0
          formattingInfo = std::make_shared<FormattingInfo>(
229
0
                formattingInfo->isLeftAligned(),
230
0
                (formattingInfo->getMinLength() * 10) + (c - 0x30 /* '0' */),
231
0
                formattingInfo->getMaxLength());
232
0
          ++minDigitCount;
233
0
        }
234
1
        else if (c == 0x2E /* '.' */)
235
0
        {
236
0
          state = DOT_STATE;
237
0
        }
238
1
        else
239
1
        {
240
1
          i = finalizeConverter(
241
1
              c, pattern, i, currentLiteral, formattingInfo,
242
1
              rules, patternConverters, formattingInfos);
243
1
          state = LITERAL_STATE;
244
1
          formattingInfo = FormattingInfo::getDefault();
245
246
1
          if (!currentLiteral.empty())
247
0
          {
248
0
            currentLiteral.erase(currentLiteral.begin(), currentLiteral.end());
249
0
          }
250
1
        }
251
252
1
        break;
253
254
0
      case DOT_STATE:
255
0
        currentLiteral.append(1, c);
256
257
0
        if ((c >= 0x30 /* '0' */) && (c <= 0x39 /* '9' */))
258
0
        {
259
0
          formattingInfo = std::make_shared<FormattingInfo>(
260
0
                formattingInfo->isLeftAligned(), formattingInfo->getMinLength(),
261
0
                c - 0x30 /* '0' */);
262
0
          state = MAX_STATE;
263
0
          maxDigitCount = 1;
264
0
        }
265
0
        else
266
0
        {
267
0
          LogLog::error(LOG4CXX_STR("Error in pattern, was expecting digit."));
268
269
0
          state = LITERAL_STATE;
270
0
        }
271
272
0
        break;
273
274
0
      case MAX_STATE:
275
0
        currentLiteral.append(1, c);
276
277
0
        if ((c >= 0x30 /* '0' */) && (c <= 0x39 /* '9' */) && maxDigitCount < 3)
278
0
        {
279
0
          formattingInfo = std::make_shared<FormattingInfo>(
280
0
                formattingInfo->isLeftAligned(), formattingInfo->getMinLength(),
281
0
                (formattingInfo->getMaxLength() * 10) + (c - 0x30 /* '0' */));
282
0
          ++maxDigitCount;
283
0
        }
284
0
        else
285
0
        {
286
0
          i = finalizeConverter(
287
0
              c, pattern, i, currentLiteral, formattingInfo,
288
0
              rules, patternConverters, formattingInfos);
289
0
          state = LITERAL_STATE;
290
0
          formattingInfo = FormattingInfo::getDefault();
291
292
0
          if (!currentLiteral.empty())
293
0
          {
294
0
            currentLiteral.erase(currentLiteral.begin(), currentLiteral.end());
295
0
          }
296
0
        }
297
298
0
        break;
299
23
    } // switch
300
23
  }
301
302
  // while
303
1
  if (currentLiteral.length() != 0)
304
0
  {
305
0
    patternConverters.push_back(
306
0
      LiteralPatternConverter::newInstance(currentLiteral));
307
0
    formattingInfos.push_back(FormattingInfo::getDefault());
308
0
  }
309
1
}
310
311
312
PatternConverterPtr PatternParser::createConverter(
313
  const LogString& converterId,
314
  LogString& currentLiteral,
315
  const PatternMap& rules,
316
  std::vector<LogString>& options)
317
6
{
318
319
6
  LogString converterName(converterId);
320
321
6
  for (size_t i = converterId.length(); i > 0; i--)
322
6
  {
323
6
    converterName = converterName.substr(0, i);
324
6
    PatternMap::const_iterator iter = rules.find(converterName);
325
326
6
    if (iter != rules.end())
327
6
    {
328
6
      currentLiteral.erase(currentLiteral.begin(),
329
6
        currentLiteral.end() - (converterId.length() - i));
330
6
      return (iter->second)(options);
331
6
    }
332
6
  }
333
334
0
  LogLog::error(LogString(LOG4CXX_STR("Unrecognized format specifier ")) + converterId);
335
336
0
  return PatternConverterPtr();
337
6
}
338
339
size_t PatternParser::finalizeConverter(
340
  logchar c, const LogString& pattern, size_t i,
341
  LogString& currentLiteral, const FormattingInfoPtr& formattingInfo,
342
  const PatternMap&  rules,
343
  std::vector<PatternConverterPtr>& patternConverters,
344
  std::vector<FormattingInfoPtr>&  formattingInfos)
345
6
{
346
6
  LogString convBuf;
347
6
  i = extractConverter(c, pattern, i, convBuf, currentLiteral);
348
349
6
  if (convBuf.empty())
350
0
  {
351
0
    LogLog::error(LOG4CXX_STR("Empty conversion specifier"));
352
0
    patternConverters.push_back(
353
0
      LiteralPatternConverter::newInstance(currentLiteral));
354
0
    formattingInfos.push_back(FormattingInfo::getDefault());
355
0
  }
356
6
  else
357
6
  {
358
6
    LogString converterId(convBuf);
359
360
6
    std::vector<LogString> options;
361
6
    i = extractOptions(pattern, i, options);
362
363
6
    PatternConverterPtr pc(
364
6
      createConverter(
365
6
        converterId, currentLiteral, rules, options));
366
367
6
    if (pc == NULL)
368
0
    {
369
0
      LogString msg(LOG4CXX_STR("Unrecognized conversion specifier ["));
370
0
      msg.append(converterId);
371
0
      msg.append(LOG4CXX_STR("] in conversion pattern."));
372
0
      LogLog::error(msg);
373
0
      patternConverters.push_back(
374
0
        LiteralPatternConverter::newInstance(currentLiteral));
375
0
      formattingInfos.push_back(FormattingInfo::getDefault());
376
0
    }
377
6
    else
378
6
    {
379
6
      patternConverters.push_back(pc);
380
6
      formattingInfos.push_back(formattingInfo);
381
382
6
      if (currentLiteral.length() > 0)
383
0
      {
384
0
        patternConverters.push_back(
385
0
          LiteralPatternConverter::newInstance(currentLiteral));
386
0
        formattingInfos.push_back(FormattingInfo::getDefault());
387
0
      }
388
6
    }
389
6
  }
390
391
6
  if (!currentLiteral.empty())
392
0
  {
393
0
    currentLiteral.erase(currentLiteral.begin(), currentLiteral.end());
394
0
  }
395
396
6
  return i;
397
6
}