File indexing completed on 2025-02-09 04:28:40

0001 /*
0002   This file is part of the KTextTemplate library
0003 
0004   SPDX-FileCopyrightText: 2011 Stephen Kelly <steveire@gmail.com>
0005 
0006   SPDX-License-Identifier: LGPL-2.1-or-later
0007 
0008 */
0009 
0010 #ifndef KTEXTTEMPLATE_TEXTPROCESSINGMACHINE_P_H
0011 #define KTEXTTEMPLATE_TEXTPROCESSINGMACHINE_P_H
0012 
0013 #include "statemachine_p.h"
0014 
0015 namespace KTextTemplate
0016 {
0017 
0018 struct CharTransitionInterface {
0019     virtual bool characterTest(QString::const_iterator)
0020     {
0021         return false;
0022     }
0023 
0024     virtual void onTransition()
0025     {
0026     }
0027 
0028 protected:
0029     virtual ~CharTransitionInterface()
0030     {
0031     }
0032 };
0033 
0034 class TextProcessingMachine : public StateMachine<CharTransitionInterface>
0035 {
0036 public:
0037     void processCharacter(QString::const_iterator character);
0038 
0039 protected:
0040     bool doProcessCharacter(QString::const_iterator character, State<CharTransitionInterface> *state);
0041 };
0042 
0043 typedef CharTransitionInterface NullTest;
0044 
0045 template<typename T, typename U>
0046 struct AndTest {
0047     static bool characterTest(QString::const_iterator ch)
0048     {
0049         return T::characterTest(ch) && U::characterTest(ch);
0050     }
0051 };
0052 
0053 template<typename T, typename U>
0054 struct OrTest {
0055     static bool characterTest(QString::const_iterator ch)
0056     {
0057         return T::characterTest(ch) || U::characterTest(ch);
0058     }
0059 };
0060 
0061 template<typename T>
0062 struct Negate {
0063     static bool characterTest(QString::const_iterator ch)
0064     {
0065         return !T::characterTest(ch);
0066     }
0067 };
0068 
0069 struct IsSpace {
0070     static bool characterTest(QString::const_iterator ch)
0071     {
0072         return ch->isSpace();
0073     }
0074 };
0075 
0076 template<char c>
0077 struct CharacterTest {
0078     static bool characterTest(QString::const_iterator ch)
0079     {
0080         return *ch == QLatin1Char(c);
0081     }
0082 };
0083 }
0084 
0085 #endif