summaryrefslogtreecommitdiffstats
path: root/ksirc/stringparserstate.h
blob: 06167267ec4a862b4307aa139f1ccc15f5176db8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#ifndef __stringparserstate_h__
#define __stringparserstate_h__

// ### optimize me: it's stupid to do a linear search for each
// atEnd() invocation. This should be done once in the ctor and
// end() should be set appropriately
template <typename CharType, typename _SizeType = size_t> 
class StringParserState
{
public:
    typedef CharType ValueType;
    typedef const CharType * ConstIterator;
    typedef _SizeType SizeType;
    typedef TQValueList<ValueType> ValueList;

    StringParserState( ConstIterator start, SizeType maxSteps,
                       const ValueList &optionalEndItems = ValueList() )
    {
	m_begin = start;
        m_current = start;
        m_end = start + maxSteps;
        m_endItems = optionalEndItems;
    }

    void operator++() { ++m_current; }

    bool atBegin() const
    {
	return m_current == m_begin;
    }

    bool atEnd() const
    { 
        if ( m_current >= m_end )
            return true;
        return m_endItems.tqfindIndex( currentValue() ) != -1;
    }

    ConstIterator current() const { return m_current; }
    ValueType currentValue() const { return *current(); }

    ConstIterator begin() const { return m_begin; }
    ConstIterator end() const { return m_end; }

    SizeType stepsLeft() const { return m_end - m_current; }

    SizeType advanceTo( ValueType val )
    {
	ConstIterator start = m_current;
	while ( !atEnd() && currentValue() != val )
	    ++m_current;
	return m_current - start;
    }

    SizeType skip( ValueType valueToIgnore )
    {
	ConstIterator start = m_current;
	while ( !atEnd() && currentValue() == valueToIgnore )
	    ++m_current;
	return m_current - start;
    }

private:
    ConstIterator m_begin;
    ConstIterator m_current;
    ConstIterator m_end;
    ValueList m_endItems;
};

#endif