File: lineranges.h

package info (click to toggle)
source-highlight 3.1.7-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 10,332 kB
  • ctags: 5,233
  • sloc: sh: 11,270; cpp: 10,206; ansic: 9,515; makefile: 1,865; lex: 1,200; yacc: 1,021; php: 213; perl: 211; awk: 98; erlang: 94; lisp: 90; java: 75; ruby: 69; python: 61; asm: 43; ml: 38; ada: 36; haskell: 27; xml: 23; cs: 11; sql: 8; tcl: 6; sed: 4
file content (110 lines) | stat: -rw-r--r-- 3,088 bytes parent folder | download | duplicates (8)
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
 * lineranges.h
 *
 *  Created on: Sep 17, 2008
 *      Author: Lorenzo Bettini <http://www.lorenzobettini.it>, (C) 2008
 *  Copyright: See COPYING file that comes with this distribution
 */

#ifndef LINERANGES_H_
#define LINERANGES_H_

#include <set>
#include <string>

namespace srchilite {

/// a possible error in specifying a range
enum RangeError {
    NO_ERROR = 0, INVALID_RANGE_NUMBER
};

/// result for a check whether a number is in a range (or in a context)
enum RangeResult {
    NOT_IN_RANGE = 0, CONTEXT_RANGE, IN_RANGE
};

/**
 * Functionalities for detecting whether a line is in one of the
 * stored line ranges (or in the context of a range).
 *
 * This also performs some optimization: ranges are stored in a set
 * in ascending order; if we have ranges, e.g., 1-20, 50-70, 100-200, and
 * we check whether 23 is in range, we first check if it's in 1-20, then,
 * since it's not there, we check whether it's in 50-70; it is not, and since the
 * range is 50-70 it makes no sense searching for it in other ranges.  The next search
 * will start from range 50-70, since we assume that line numbers are always increasing.
 */
class LineRanges {
public:
    LineRanges(unsigned int contextLines = 0);
    ~LineRanges();

    typedef int RangeElemType;
    typedef std::pair<RangeElemType, RangeElemType> RangeType;

    typedef std::set<RangeType> LineRangeSet;

    /**
     * Adds a range to the set.
     * The argument can be:
     *
     * - a single element (means only one line)
     * - a complete range (e.g., 20-35)
     * - a partial range (e.g., 10- : from line 10 to the end,
     *   -20 : from the beginning to line 20)
     *
     * @param range the string representing the range.
     * @return code specifying a possible error
     */
    RangeError addRange(const std::string &range);

    const LineRangeSet &getLineRangeSet() const {
        return lineRangeSet;
    }

    /**
     * The next isInRange search will start from the first element of
     * the set.  This should be called before searching for lines of a file,
     * that we started to process.
     */
    void reset() {
        searchFromTheStart = true;
    }

    /**
     * Checks whether the passed element is in a range of this set.
     * If it's not in the range it might be in the surrounding context.
     *
     * @param e
     * @return whether the passed element is in a range or in the surrounding context.
     */
    RangeResult isInRange(const RangeElemType e);

    void setContextLines(unsigned int context) {
        contextLines = context;
    }

private:
    LineRangeSet lineRangeSet;

    /**
     * whether to perform the search from the first element of the set
     */
    bool searchFromTheStart;

    /**
     * The current range for performing the search of isInRange.
     */
    LineRangeSet::const_iterator currentRange;

    /**
     * The number of lines making the context (i.e., the number of lines
     * that are not part of a range but are in the specified line number context)
     */
    int contextLines;
};

}

#endif /* LINERANGES_H_ */