18#include "helpers/formatstring.h"
34 std::string stateAsString;
38 case CurrentState::Initial:
39 stateAsString =
"Initial";
42 case CurrentState::NormalText:
43 stateAsString =
"NormalText";
46 case CurrentState::DoubleQuotedText:
47 stateAsString =
"DoubleQuotedText";
50 case CurrentState::SingleQuotedText:
51 stateAsString =
"SingleQuotedText";
54 case CurrentState::EscapedDoubleQuoteText:
55 stateAsString =
"EscapedDoubleQuoteText";
58 case CurrentState::EscapedSingleQuoteText:
59 stateAsString =
"EscapedSingleQuoteText";
62 case CurrentState::StartComment:
63 stateAsString =
"StartComment";
66 case CurrentState::LineComment:
67 stateAsString =
"LineComment";
70 case CurrentState::BlockComment:
71 stateAsString =
"BlockComment";
74 case CurrentState::EndBlockComment:
75 stateAsString =
"EndBlockComment";
78 case CurrentState::Done:
79 stateAsString =
"Done";
617 case CurrentState::NormalText:
618 stateBehavior = std::make_shared<State_NormalText>();
621 case CurrentState::DoubleQuotedText:
622 stateBehavior = std::make_shared<State_DoubleQuotedText>();
625 case CurrentState::SingleQuotedText:
626 stateBehavior = std::make_shared<State_SingleQuotedText>();
629 case CurrentState::EscapedDoubleQuoteText:
630 stateBehavior = std::make_shared<State_EscapedDoubleQuoteText>();
633 case CurrentState::EscapedSingleQuoteText:
634 stateBehavior = std::make_shared<State_EscapedSingleQuoteText>();
637 case CurrentState::StartComment:
638 stateBehavior = std::make_shared<State_StartComment>();
641 case CurrentState::LineComment:
642 stateBehavior = std::make_shared<State_LineComment>();
645 case CurrentState::BlockComment:
646 stateBehavior = std::make_shared<State_BlockComment>();
649 case CurrentState::EndBlockComment:
650 stateBehavior = std::make_shared<State_EndBlockComment>();
653 case CurrentState::Done:
654 stateBehavior = std::make_shared<State_Done>();
661 throw std::runtime_error(msg.c_str());
665 return stateBehavior;
691 std::string _inputText;
701 std::ostringstream _outputText;
703 using StateMapping = std::map<CurrentState, IStateBehavior::shared_ptr_t>;
709 StateMapping _stateBehaviors;
739 if (newState != _currentState)
741 StateMapping::const_iterator foundIter = _stateBehaviors.find(newState);
743 if (foundIter == std::cend(_stateBehaviors))
745 _stateBehaviors[newState] = State_Factory::CreateState(newState);
754 _currentStateBehavior = _stateBehaviors[newState];
755 _currentState = newState;
764 StateContext_ClassImpl()
781 std::string RemoveComments(std::string text)
790 while (_currentState != CurrentState::Done)
792 CurrentState nextState = _currentStateBehavior->GoNext(
this);
796 return _outputText.str();
813 if (_textIndex < _inputText.size())
815 character = _inputText[_textIndex];
829 _outputText << character;
851 : _stateContextimpl(std::make_unique<StateContext_ClassImpl>())
858 StateContext_ClassImpl* stateImpl =
dynamic_cast<StateContext_ClassImpl*
>(
_stateContextimpl.get());
859 if (stateImpl !=
nullptr)
861 return stateImpl->RemoveComments(text);
863 return std::string();
Declaration of the IStateContext and IStateBehavior interfaces, as well as the StateContext_Class cla...
std::string RemoveComments(std::string text)
Entry point for callers to filter text. Removes C++-style line and block comments from the text.
std::unique_ptr< IStateContext > _stateContextimpl
Pointer to the actual implementation.
StateContext_Class()
Default constructor.
The namespace containing all Design Pattern Examples implemented in C++.
const char EOF_CHAR
Indicates End-of-file (no more data available).
CurrentState
Represents the current state of the state machine.
@ SingleQuotedText
‘’` transitions to EscapedSingleQuoteText, \ transitions to NormalText, EOF_CHAR transitions to Done
@ DoubleQuotedText
\ transitions to EscapedDoubleQuoteText, " transitions to NormalText, EOF_CHAR transitions to Done
@ BlockComment
* transitions to EndBlockComment, EOF_CHAR transitions to Done
@ Initial
State before the state machine actually starts. transitions to NormalText.
@ EndBlockComment
/ transitions to NormalText, EOF_CHAR transitions to Done, all else transitions to BlockComment
@ LineComment
\\n transitions to NormalText, EOF_CHAR transitions to Done
@ Done
Indicates processing is done.
@ StartComment
/ transitions to LineComment, * transitions to BlockComment, EOF_CHAR transitions to Done,...
@ NormalText
" transitions to QuotedText, / transitions to StartComment, EOF_CHAR transitions to Done
@ EscapedDoubleQuoteText
\ transitions to QuotedText, EOF_CHAR transitions to Done
@ EscapedSingleQuoteText
\ transitions to SingleQuotedText, EOF_CHAR transitions to Done
std::string formatstring(const char *fmt,...)
Use the given string and arguments to return a buffer containing the formatted string....
Represents a class that implements one state of the state machine.
std::shared_ptr< IStateBehavior > shared_ptr_t
Alias to make using a shared pointer easier.
virtual CurrentState GoNext(IStateContext *context)=0
Represents the context as passed to each state class.
virtual void OutputCharacter(char character)=0
Write the character to the context-> This is how the parser accumulates the filtered text.
virtual char GetNextCharacter()=0
Get the next character from the input.