2 * Copyright 2010, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Copyright 2015, Axel Dörfler, axeld@pinc-software.de.
4 * Distributed under the terms of the MIT License.
8 #include "PatternEvaluator.h"
16 // #pragma mark - PatternEvaluator
20 PatternEvaluator::Evaluate(const char* pattern
, PlaceholderMapper
& mapper
)
24 bool isBefore
= false;
26 bool hadResult
= false;
29 while (*pattern
!= '\0') {
30 // find next placeholder
31 const char* placeholder
= strchr(pattern
, '%');
33 if (placeholder
!= NULL
)
34 length
= placeholder
- pattern
;
38 // append skipped chars
39 if (placeholder
!= pattern
) {
41 before
.SetTo(pattern
, length
);
43 } else if (!isAfter
|| hadResult
) {
44 result
.Append(pattern
, length
);
50 if (placeholder
== NULL
)
53 pattern
= placeholder
+ 1;
55 // check for special placeholders
63 // An optional before string
64 isBefore
= began
= true;
69 // An optional after string
76 // End of any other section; ignore
82 // Count non alpha numeric characters to the before section
83 while (pattern
[0] != '\0' && !isalnum(pattern
[0])) {
84 before
.Append(pattern
[0], 1);
88 // parse a number, if there is one
90 bool hasNumber
= false;
91 if (isdigit(*pattern
)) {
93 number
= strtoll(pattern
, &numberEnd
, 10);
99 if (*pattern
!= '\0' && mapper
.MapPlaceholder(*pattern
,
100 number
, hasNumber
, mappedValue
)) {
101 // mapped successfully -- append the replacement string
102 if (began
&& !mappedValue
.IsEmpty())
104 if (!before
.IsEmpty() && !mappedValue
.IsEmpty()) {
109 result
+= mappedValue
;
112 // something went wrong -- just append the literal part of the
114 result
.Append(placeholder
, length
);
122 // #pragma mark - PlaceholderMapper
125 PatternEvaluator::PlaceholderMapper::~PlaceholderMapper()