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)
27 #elif defined(_MSC_VER) 29 RAPIDJSON_DIAG_OFF(4512)
34 RAPIDJSON_DIAG_OFF(effc++)
36 RAPIDJSON_DIAG_OFF(implicit - fallthrough)
40 #ifndef RAPIDJSON_REGEX_VERBOSE 41 #define RAPIDJSON_REGEX_VERBOSE 0 50 template <
typename SourceStream,
typename Encoding>
88 template <
typename Encoding,
typename Allocator>
124 template <
typename Encoding,
typename Allocator = CrtAllocator>
129 typedef typename Encoding::Ch
Ch;
130 template <
typename,
typename>
135 , allocator_(allocator ? allocator : ownAllocator_)
136 , states_(allocator_, 256)
137 , ranges_(allocator_, 256)
138 , root_(kRegexInvalidState)
170 static const unsigned kAnyCharacterClass = 0xFFFFFFFF;
171 static const unsigned kRangeCharacterClass = 0xFFFFFFFE;
172 static const unsigned kRangeNegationFlag = 0x80000000;
202 return states_.template Bottom<State>()[index];
208 return states_.template Bottom<State>()[index];
214 return ranges_.template Bottom<Range>()[index];
220 return ranges_.template Bottom<Range>()[index];
223 template <
typename InputStream>
230 *atomCountStack.template Push<unsigned>() = 0;
233 while (ds.
Peek() != 0)
235 switch (codepoint = ds.
Take())
246 while (!operatorStack.
Empty() && *operatorStack.template Top<Operator>() < kAlternation)
247 if (!Eval(operandStack, *operatorStack.template Pop<Operator>(1)))
249 *operatorStack.template Push<Operator>() = kAlternation;
250 *atomCountStack.template Top<unsigned>() = 0;
254 *operatorStack.template Push<Operator>() = kLeftParenthesis;
255 *atomCountStack.template Push<unsigned>() = 0;
259 while (!operatorStack.
Empty() && *operatorStack.template Top<Operator>() != kLeftParenthesis)
260 if (!Eval(operandStack, *operatorStack.template Pop<Operator>(1)))
262 if (operatorStack.
Empty())
264 operatorStack.template Pop<Operator>(1);
265 atomCountStack.template Pop<unsigned>(1);
266 ImplicitConcatenation(atomCountStack, operatorStack);
270 if (!Eval(operandStack, kZeroOrOne))
275 if (!Eval(operandStack, kZeroOrMore))
280 if (!Eval(operandStack, kOneOrMore))
287 if (!ParseUnsigned(ds, &n))
290 if (ds.
Peek() ==
',')
293 if (ds.
Peek() ==
'}')
294 m = kInfinityQuantifier;
295 else if (!ParseUnsigned(ds, &m) || m < n)
301 if (!EvalQuantifier(operandStack, n, m) || ds.
Peek() !=
'}')
308 PushOperand(operandStack, kAnyCharacterClass);
309 ImplicitConcatenation(atomCountStack, operatorStack);
315 if (!ParseRange(ds, &range))
317 SizeType s = NewState(kRegexInvalidState, kRegexInvalidState, kRangeCharacterClass);
318 GetState(s).rangeStart = range;
319 *operandStack.template Push<Frag>() =
Frag(s, s, s);
321 ImplicitConcatenation(atomCountStack, operatorStack);
325 if (!CharacterEscape(ds, &codepoint))
330 PushOperand(operandStack, codepoint);
331 ImplicitConcatenation(atomCountStack, operatorStack);
335 while (!operatorStack.
Empty())
336 if (!Eval(operandStack, *operatorStack.template Pop<Operator>(1)))
342 Frag* e = operandStack.template Pop<Frag>(1);
343 Patch(e->
out, NewState(kRegexInvalidState, kRegexInvalidState, 0));
346 #if RAPIDJSON_REGEX_VERBOSE 347 printf(
"root: %d\n", root_);
348 for (
SizeType i = 0; i < stateCount_; i++)
351 printf(
"[%2d] out: %2d out1: %2d c: '%c'\n", i, s.
out, s.
out1, (
char)s.
codepoint);
360 State*
s = states_.template Push<State>();
365 return stateCount_++;
370 SizeType s = NewState(kRegexInvalidState, kRegexInvalidState, codepoint);
371 *operandStack.template Push<Frag>() =
Frag(s, s, s);
376 if (*atomCountStack.template Top<unsigned>())
377 *operatorStack.template Push<Operator>() = kConcatenation;
378 (*atomCountStack.template Top<unsigned>())++;
384 while (GetState(l1).out != kRegexInvalidState)
385 l1 = GetState(l1).out;
386 GetState(l1).out = l2;
394 next = GetState(l).out;
406 Frag e2 = *operandStack.template Pop<Frag>(1);
407 Frag e1 = *operandStack.template Pop<Frag>(1);
408 Patch(e1.out, e2.
start);
409 *operandStack.template Push<Frag>() =
Frag(e1.start, e2.
out, Min(e1.minIndex, e2.
minIndex));
416 Frag e2 = *operandStack.template Pop<Frag>(1);
417 Frag e1 = *operandStack.template Pop<Frag>(1);
419 *operandStack.template Push<Frag>() =
Frag(s, Append(e1.out, e2.
out), Min(e1.minIndex, e2.
minIndex));
427 Frag e = *operandStack.template Pop<Frag>(1);
429 *operandStack.template Push<Frag>() =
Frag(s, Append(e.
out, s), e.
minIndex);
437 Frag e = *operandStack.template Pop<Frag>(1);
440 *operandStack.template Push<Frag>() =
Frag(s, s, e.
minIndex);
448 Frag e = *operandStack.template Pop<Frag>(1);
471 else if (m == kInfinityQuantifier)
472 Eval(operandStack, kZeroOrMore);
475 Eval(operandStack, kZeroOrOne);
476 for (
unsigned i = 0; i < m - 1; i++)
477 CloneTopOperand(operandStack);
478 for (
unsigned i = 0; i < m - 1; i++)
479 Eval(operandStack, kConcatenation);
484 for (
unsigned i = 0; i < n - 1; i++)
485 CloneTopOperand(operandStack);
487 if (m == kInfinityQuantifier)
488 Eval(operandStack, kOneOrMore);
491 CloneTopOperand(operandStack);
492 Eval(operandStack, kZeroOrOne);
493 for (
unsigned i = n; i < m - 1; i++)
494 CloneTopOperand(operandStack);
495 for (
unsigned i = n; i < m; i++)
496 Eval(operandStack, kConcatenation);
499 for (
unsigned i = 0; i < n - 1; i++)
500 Eval(operandStack, kConcatenation);
507 return a < b ? a : b;
512 const Frag src = *operandStack.template Top<Frag>();
518 if (s[j].out != kRegexInvalidState)
520 if (s[j].out1 != kRegexInvalidState)
523 *operandStack.template Push<Frag>() =
Frag(src.
start + count, src.
out + count, src.
minIndex + count);
524 stateCount_ +=
count;
527 template <
typename InputStream>
531 if (ds.
Peek() <
'0' || ds.
Peek() >
'9')
533 while (ds.
Peek() >=
'0' && ds.
Peek() <=
'9')
535 if (r >= 429496729 && ds.
Peek() >
'5')
537 r = r * 10 + (ds.
Take() -
'0');
543 template <
typename InputStream>
552 while ((codepoint = ds.
Take()) != 0)
557 if (codepoint ==
'^')
567 if (start == kRegexInvalidRange)
573 GetRange(current).next = r;
576 GetRange(start).start |= kRangeNegationFlag;
581 if (ds.
Peek() ==
'b')
586 else if (!CharacterEscape(ds, &codepoint))
594 if (codepoint ==
'-')
604 if (current != kRegexInvalidRange)
605 GetRange(current).next = r;
606 if (start == kRegexInvalidRange)
615 GetRange(current).end = codepoint;
625 Range* r = ranges_.template Push<Range>();
628 return rangeCount_++;
631 template <
typename InputStream>
635 switch (codepoint = ds.
Take())
651 *escapedCodepoint = codepoint;
654 *escapedCodepoint = 0x000C;
657 *escapedCodepoint = 0x000A;
660 *escapedCodepoint = 0x000D;
663 *escapedCodepoint = 0x0009;
666 *escapedCodepoint = 0x000B;
681 static const unsigned kInfinityQuantifier = ~0u;
688 template <
typename RegexType,
typename Allocator = CrtAllocator>
693 typedef typename Encoding::Ch
Ch;
696 : regex_(regex), allocator_(allocator), ownAllocator_(0), state0_(allocator, 0), state1_(allocator, 0), stateSet_()
701 stateSet_ =
static_cast<unsigned*
>(allocator_->Malloc(GetStateSetSize()));
702 state0_.template Reserve<SizeType>(regex_.stateCount_);
703 state1_.template Reserve<SizeType>(regex_.stateCount_);
708 Allocator::Free(stateSet_);
712 template <
typename InputStream>
715 return SearchWithAnchoring(is,
true,
true);
724 template <
typename InputStream>
727 return SearchWithAnchoring(is, regex_.anchorBegin_, regex_.anchorEnd_);
737 typedef typename RegexType::State
State;
738 typedef typename RegexType::Range
Range;
740 template <
typename InputStream>
747 const size_t stateSetSize = GetStateSetSize();
748 std::memset(stateSet_, 0, stateSetSize);
750 bool matched = AddState(*current, regex_.root_);
752 while (!current->
Empty() && (codepoint = ds.
Take()) != 0)
754 std::memset(stateSet_, 0, stateSetSize);
757 for (
const SizeType*
s = current->template Bottom<SizeType>();
s != current->template End<SizeType>(); ++
s)
759 const State& sr = regex_.GetState(*
s);
760 if (sr.codepoint == codepoint || sr.codepoint == RegexType::kAnyCharacterClass ||
761 (sr.codepoint == RegexType::kRangeCharacterClass && MatchRange(sr.rangeStart, codepoint)))
763 matched = AddState(*next, sr.out) || matched;
764 if (!anchorEnd && matched)
768 AddState(*next, regex_.root_);
778 return (regex_.stateCount_ + 31) / 32 * 4;
786 const State&
s = regex_.GetState(index);
787 if (s.out1 != kRegexInvalidState)
789 bool matched = AddState(l, s.out);
790 return AddState(l, s.out1) || matched;
792 else if (!(stateSet_[index >> 5] & (1u << (index & 31))))
794 stateSet_[index >> 5] |= (1u << (index & 31));
795 *l.template PushUnsafe<SizeType>() = index;
803 bool yes = (regex_.GetRange(rangeIndex).start & RegexType::kRangeNegationFlag) == 0;
804 while (rangeIndex != kRegexInvalidRange)
806 const Range& r = regex_.GetRange(rangeIndex);
807 if (codepoint >= (r.start & ~RegexType::kRangeNegationFlag) && codepoint <= r.end)
832 #if defined(__clang__) || defined(_MSC_VER) 836 #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
Allocator * ownAllocator_
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)