Fix crash on logout
[kdenetwork.git] / ksirc / stringparserstate.h
blobf12c11d7cc4b0f0a8923ce537af198b8d5a36ee1
1 #ifndef __stringparserstate_h__
2 #define __stringparserstate_h__
4 // ### optimize me: it's stupid to do a linear search for each
5 // atEnd() invocation. This should be done once in the ctor and
6 // end() should be set appropriately
7 template <typename CharType, typename _SizeType = size_t>
8 class StringParserState
10 public:
11 typedef CharType ValueType;
12 typedef const CharType * ConstIterator;
13 typedef _SizeType SizeType;
14 typedef QList<ValueType> ValueList;
16 StringParserState( ConstIterator start, SizeType maxSteps,
17 const ValueList &optionalEndItems = ValueList() )
19 m_begin = start;
20 m_current = start;
21 m_end = start + maxSteps;
22 m_endItems = optionalEndItems;
25 void operator++() { ++m_current; }
27 bool atBegin() const
29 return m_current == m_begin;
32 bool atEnd() const
34 if ( m_current >= m_end )
35 return true;
36 return m_endItems.findIndex( currentValue() ) != -1;
39 ConstIterator current() const { return m_current; }
40 ValueType currentValue() const { return *current(); }
42 ConstIterator begin() const { return m_begin; }
43 ConstIterator end() const { return m_end; }
45 SizeType stepsLeft() const { return m_end - m_current; }
47 SizeType advanceTo( ValueType val )
49 ConstIterator start = m_current;
50 while ( !atEnd() && currentValue() != val )
51 ++m_current;
52 return m_current - start;
55 SizeType skip( ValueType valueToIgnore )
57 ConstIterator start = m_current;
58 while ( !atEnd() && currentValue() == valueToIgnore )
59 ++m_current;
60 return m_current - start;
63 private:
64 ConstIterator m_begin;
65 ConstIterator m_current;
66 ConstIterator m_end;
67 ValueList m_endItems;
70 #endif