15 #ifndef RAPIDJSON_INTERNAL_REGEX_H_ 16 #define RAPIDJSON_INTERNAL_REGEX_H_ 18 #include "../allocators.h" 19 #include "../stream.h" 24 RAPIDJSON_DIAG_OFF(padded)
25 RAPIDJSON_DIAG_OFF(switch-enum)
26 RAPIDJSON_DIAG_OFF(implicit-fallthrough)
31 RAPIDJSON_DIAG_OFF(effc++)
33 RAPIDJSON_DIAG_OFF(implicit-fallthrough)
39 RAPIDJSON_DIAG_OFF(4512)
42 #ifndef RAPIDJSON_REGEX_VERBOSE 43 #define RAPIDJSON_REGEX_VERBOSE 0 52 template <
typename SourceStream,
typename Encoding>
80 template <
typename Encoding,
typename Allocator>
115 template <
typename Encoding,
typename Allocator = CrtAllocator>
119 typedef typename Encoding::Ch
Ch;
123 states_(allocator, 256), ranges_(allocator, 256), root_(kRegexInvalidState), stateCount_(), rangeCount_(),
124 anchorBegin_(), anchorEnd_()
147 static const unsigned kAnyCharacterClass = 0xFFFFFFFF;
148 static const unsigned kRangeCharacterClass = 0xFFFFFFFE;
149 static const unsigned kRangeNegationFlag = 0x80000000;
173 return states_.template Bottom<State>()[index];
178 return states_.template Bottom<State>()[index];
183 return ranges_.template Bottom<Range>()[index];
188 return ranges_.template Bottom<Range>()[index];
191 template <
typename InputStream>
198 *atomCountStack.template Push<unsigned>() = 0;
201 while (ds.
Peek() != 0) {
202 switch (codepoint = ds.
Take()) {
212 while (!operatorStack.
Empty() && *operatorStack.template Top<Operator>() < kAlternation)
213 if (!Eval(operandStack, *operatorStack.template Pop<Operator>(1)))
215 *operatorStack.template Push<Operator>() = kAlternation;
216 *atomCountStack.template Top<unsigned>() = 0;
220 *operatorStack.template Push<Operator>() = kLeftParenthesis;
221 *atomCountStack.template Push<unsigned>() = 0;
225 while (!operatorStack.
Empty() && *operatorStack.template Top<Operator>() != kLeftParenthesis)
226 if (!Eval(operandStack, *operatorStack.template Pop<Operator>(1)))
228 if (operatorStack.
Empty())
230 operatorStack.template Pop<Operator>(1);
231 atomCountStack.template Pop<unsigned>(1);
232 ImplicitConcatenation(atomCountStack, operatorStack);
236 if (!Eval(operandStack, kZeroOrOne))
241 if (!Eval(operandStack, kZeroOrMore))
246 if (!Eval(operandStack, kOneOrMore))
253 if (!ParseUnsigned(ds, &n))
256 if (ds.
Peek() ==
',') {
258 if (ds.
Peek() ==
'}')
259 m = kInfinityQuantifier;
260 else if (!ParseUnsigned(ds, &m) || m < n)
266 if (!EvalQuantifier(operandStack, n, m) || ds.
Peek() !=
'}')
273 PushOperand(operandStack, kAnyCharacterClass);
274 ImplicitConcatenation(atomCountStack, operatorStack);
280 if (!ParseRange(ds, &range))
282 SizeType s = NewState(kRegexInvalidState, kRegexInvalidState, kRangeCharacterClass);
283 GetState(s).rangeStart = range;
284 *operandStack.template Push<Frag>() =
Frag(s, s, s);
286 ImplicitConcatenation(atomCountStack, operatorStack);
290 if (!CharacterEscape(ds, &codepoint))
295 PushOperand(operandStack, codepoint);
296 ImplicitConcatenation(atomCountStack, operatorStack);
300 while (!operatorStack.
Empty())
301 if (!Eval(operandStack, *operatorStack.template Pop<Operator>(1)))
306 Frag* e = operandStack.template Pop<Frag>(1);
307 Patch(e->
out, NewState(kRegexInvalidState, kRegexInvalidState, 0));
310 #if RAPIDJSON_REGEX_VERBOSE 311 printf(
"root: %d\n", root_);
312 for (
SizeType i = 0; i < stateCount_ ; i++) {
314 printf(
"[%2d] out: %2d out1: %2d c: '%c'\n", i, s.
out, s.
out1, (
char)s.
codepoint);
322 State*
s = states_.template Push<State>();
327 return stateCount_++;
331 SizeType s = NewState(kRegexInvalidState, kRegexInvalidState, codepoint);
332 *operandStack.template Push<Frag>() =
Frag(s, s, s);
336 if (*atomCountStack.template Top<unsigned>())
337 *operatorStack.template Push<Operator>() = kConcatenation;
338 (*atomCountStack.template Top<unsigned>())++;
343 while (GetState(l1).out != kRegexInvalidState)
344 l1 = GetState(l1).out;
345 GetState(l1).out = l2;
351 next = GetState(l).out;
361 Frag e2 = *operandStack.template Pop<Frag>(1);
362 Frag e1 = *operandStack.template Pop<Frag>(1);
363 Patch(e1.out, e2.
start);
364 *operandStack.template Push<Frag>() =
Frag(e1.start, e2.
out, Min(e1.minIndex, e2.
minIndex));
370 Frag e2 = *operandStack.template Pop<Frag>(1);
371 Frag e1 = *operandStack.template Pop<Frag>(1);
373 *operandStack.template Push<Frag>() =
Frag(s, Append(e1.out, e2.
out), Min(e1.minIndex, e2.
minIndex));
380 Frag e = *operandStack.template Pop<Frag>(1);
382 *operandStack.template Push<Frag>() =
Frag(s, Append(e.
out, s), e.
minIndex);
389 Frag e = *operandStack.template Pop<Frag>(1);
392 *operandStack.template Push<Frag>() =
Frag(s, s, e.
minIndex);
400 Frag e = *operandStack.template Pop<Frag>(1);
417 else if (m == kInfinityQuantifier)
418 Eval(operandStack, kZeroOrMore);
420 Eval(operandStack, kZeroOrOne);
421 for (
unsigned i = 0; i < m - 1; i++)
422 CloneTopOperand(operandStack);
423 for (
unsigned i = 0; i < m - 1; i++)
424 Eval(operandStack, kConcatenation);
429 for (
unsigned i = 0; i < n - 1; i++)
430 CloneTopOperand(operandStack);
432 if (m == kInfinityQuantifier)
433 Eval(operandStack, kOneOrMore);
435 CloneTopOperand(operandStack);
436 Eval(operandStack, kZeroOrOne);
437 for (
unsigned i = n; i < m - 1; i++)
438 CloneTopOperand(operandStack);
439 for (
unsigned i = n; i < m; i++)
440 Eval(operandStack, kConcatenation);
443 for (
unsigned i = 0; i < n - 1; i++)
444 Eval(operandStack, kConcatenation);
452 const Frag src = *operandStack.template Top<Frag>();
454 State*
s = states_.template Push<State>(count);
456 for (
SizeType j = 0; j < count; j++) {
457 if (s[j].out != kRegexInvalidState)
459 if (s[j].out1 != kRegexInvalidState)
462 *operandStack.template Push<Frag>() =
Frag(src.
start + count, src.
out + count, src.
minIndex + count);
463 stateCount_ += count;
466 template <
typename InputStream>
469 if (ds.
Peek() <
'0' || ds.
Peek() >
'9')
471 while (ds.
Peek() >=
'0' && ds.
Peek() <=
'9') {
472 if (r >= 429496729 && ds.
Peek() >
'5')
474 r = r * 10 + (ds.
Take() -
'0');
480 template <
typename InputStream>
488 while ((codepoint = ds.
Take()) != 0) {
491 if (codepoint ==
'^') {
499 if (start == kRegexInvalidRange)
504 GetRange(current).next = r;
507 GetRange(start).start |= kRangeNegationFlag;
512 if (ds.
Peek() ==
'b') {
516 else if (!CharacterEscape(ds, &codepoint))
523 if (codepoint ==
'-') {
532 if (current != kRegexInvalidRange)
533 GetRange(current).next = r;
534 if (start == kRegexInvalidRange)
543 GetRange(current).end = codepoint;
552 Range* r = ranges_.template Push<Range>();
555 return rangeCount_++;
558 template <
typename InputStream>
561 switch (codepoint = ds.
Take()) {
576 *escapedCodepoint = codepoint;
return true;
577 case 'f': *escapedCodepoint = 0x000C;
return true;
578 case 'n': *escapedCodepoint = 0x000A;
return true;
579 case 'r': *escapedCodepoint = 0x000D;
return true;
580 case 't': *escapedCodepoint = 0x0009;
return true;
581 case 'v': *escapedCodepoint = 0x000B;
return true;
593 static const unsigned kInfinityQuantifier = ~0u;
600 template <
typename RegexType,
typename Allocator = CrtAllocator>
604 typedef typename Encoding::Ch
Ch;
607 regex_(regex), allocator_(allocator), ownAllocator_(0),
608 state0_(allocator, 0), state1_(allocator, 0), stateSet_()
613 stateSet_ =
static_cast<unsigned*
>(allocator_->Malloc(GetStateSetSize()));
614 state0_.template Reserve<SizeType>(regex_.stateCount_);
615 state1_.template Reserve<SizeType>(regex_.stateCount_);
619 Allocator::Free(stateSet_);
623 template <
typename InputStream>
625 return SearchWithAnchoring(is,
true,
true);
633 template <
typename InputStream>
635 return SearchWithAnchoring(is, regex_.anchorBegin_, regex_.anchorEnd_);
644 typedef typename RegexType::State
State;
645 typedef typename RegexType::Range
Range;
647 template <
typename InputStream>
653 const size_t stateSetSize = GetStateSetSize();
654 std::memset(stateSet_, 0, stateSetSize);
656 bool matched = AddState(*current, regex_.root_);
658 while (!current->
Empty() && (codepoint = ds.
Take()) != 0) {
659 std::memset(stateSet_, 0, stateSetSize);
662 for (
const SizeType*
s = current->template Bottom<SizeType>();
s != current->template End<SizeType>(); ++
s) {
663 const State& sr = regex_.GetState(*
s);
664 if (sr.codepoint == codepoint ||
665 sr.codepoint == RegexType::kAnyCharacterClass ||
666 (sr.codepoint == RegexType::kRangeCharacterClass && MatchRange(sr.rangeStart, codepoint)))
668 matched = AddState(*next, sr.out) || matched;
669 if (!anchorEnd && matched)
673 AddState(*next, regex_.root_);
682 return (regex_.stateCount_ + 31) / 32 * 4;
689 const State&
s = regex_.GetState(index);
690 if (s.out1 != kRegexInvalidState) {
691 bool matched = AddState(l, s.out);
692 return AddState(l, s.out1) || matched;
694 else if (!(stateSet_[index >> 5] & (1u << (index & 31)))) {
695 stateSet_[index >> 5] |= (1u << (index & 31));
696 *l.template PushUnsafe<SizeType>() = index;
702 bool yes = (regex_.GetRange(rangeIndex).start & RegexType::kRangeNegationFlag) == 0;
703 while (rangeIndex != kRegexInvalidRange) {
704 const Range& r = regex_.GetRange(rangeIndex);
705 if (codepoint >= (r.start & ~RegexType::kRangeNegationFlag) && codepoint <= r.end)
734 #endif // RAPIDJSON_INTERNAL_REGEX_H_
Allocator * ownAllocator_
bool Eval(Stack< Allocator > &operandStack, Operator op)
SizeType NewRange(unsigned codepoint)
const Range & GetRange(SizeType index) const
const CharType(& source)[N]
const State & GetState(SizeType index) const
RAPIDJSON_NAMESPACE_BEGIN typedef unsigned SizeType
Size type (for string lengths, array sizes, etc.)
SizeType out
link-list of all output states
#define RAPIDJSON_ASSERT(x)
Assertion.
#define RAPIDJSON_NAMESPACE_END
provide custom rapidjson namespace (closing expression)
GenericRegexSearch< Regex > RegexSearch
Stack< Allocator > state0_
Stack< Allocator > ranges_
SizeType out1
Equals to non-kInvalid for split.
A type-unsafe stack for storing different types of data.
Regular expression engine with subset of ECMAscript grammar.
bool AddState(Stack< Allocator > &l, SizeType index)
GenericRegex< UTF8<> > Regex
#define RAPIDJSON_NAMESPACE_BEGIN
provide custom rapidjson namespace (opening expression)
void CloneTopOperand(Stack< Allocator > &operandStack)
SizeType out
Equals to kInvalid for matching state.
#define RAPIDJSON_NEW(TypeName)
! customization point for global new
SizeType NewState(SizeType out, SizeType out1, unsigned codepoint)
void PushOperand(Stack< Allocator > &operandStack, unsigned codepoint)
bool SearchWithAnchoring(InputStream &is, bool anchorBegin, bool anchorEnd)
DecodedStream(SourceStream &ss)
GenericRegexSearch(const RegexType ®ex, Allocator *allocator=0)
size_t GetStateSetSize() const
static const SizeType kRegexInvalidState
Represents an invalid index in GenericRegex::State::out, out1.
Stack< Allocator > state1_
void Swap(T &a, T &b) RAPIDJSON_NOEXCEPT
Custom swap() to avoid dependency on C++ <algorithm> header.
#define RAPIDJSON_DELETE(x)
! customization point for global delete
void Parse(DecodedStream< InputStream, Encoding > &ds)
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
bool ParseRange(DecodedStream< InputStream, Encoding > &ds, SizeType *range)
bool Search(InputStream &is)
static SizeType Min(SizeType a, SizeType b)
State & GetState(SizeType index)
Frag(SizeType s, SizeType o, SizeType m)
bool MatchRange(SizeType rangeIndex, unsigned codepoint) const
bool EvalQuantifier(Stack< Allocator > &operandStack, unsigned n, unsigned m)
bool Match(InputStream &is)
void Patch(SizeType l, SizeType s)
bool ParseUnsigned(DecodedStream< InputStream, Encoding > &ds, unsigned *u)
RegexType::EncodingType Encoding
SizeType Append(SizeType l1, SizeType l2)
Range & GetRange(SizeType index)
bool CharacterEscape(DecodedStream< InputStream, Encoding > &ds, unsigned *escapedCodepoint)
void ImplicitConcatenation(Stack< Allocator > &atomCountStack, Stack< Allocator > &operatorStack)
static const SizeType kRegexInvalidRange
Stack< Allocator > states_
GenericRegex(const Ch *source, Allocator *allocator=0)