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 111 112 113 114 115 116 117 118
|
/*
* lineranges.cpp
*
* Created on: Sep 17, 2008
* Author: Lorenzo Bettini <http://www.lorenzobettini.it>, (C) 2008
* Copyright: See COPYING file that comes with this distribution
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "lineranges.h"
#include <sstream>
#include <boost/regex.hpp>
#include <cstdlib>
namespace srchilite {
/// regular expression for a single line
static boost::regex singleNumber("\\s*([[:digit:]]+)\\s*");
/// regular expression for an interval
static boost::regex rangeExp("\\s*([[:digit:]]+)\\s*-\\s*([[:digit:]]+)\\s*");
/// regular expression for an interval with only the first element
static boost::regex rangeExp1("\\s*([[:digit:]]+)\\s*-\\s*");
/// regular expression for an interval with only the second element
static boost::regex rangeExp2("\\s*-\\s*([[:digit:]]+)\\s*");
using namespace std;
LineRanges::LineRanges(unsigned int context) :
searchFromTheStart(true), contextLines(context) {
}
LineRanges::~LineRanges() {
}
RangeError LineRanges::addRange(const std::string &range) {
boost::smatch match;
if (boost::regex_match(range, match, singleNumber)) {
lineRangeSet.insert(make_pair(strtol(match[1].str().c_str(), 0, 0), 0));
} else if (boost::regex_match(range, match, rangeExp)) {
lineRangeSet.insert(make_pair(strtol(match[1].str().c_str(), 0, 0),
strtol(match[2].str().c_str(), 0, 0)));
} else if (boost::regex_match(range, match, rangeExp1)) {
lineRangeSet.insert(make_pair(strtol(match[1].str().c_str(), 0, 0), -1));
} else if (boost::regex_match(range, match, rangeExp2)) {
lineRangeSet.insert(make_pair(-1, strtol(match[1].str().c_str(), 0, 0)));
} else {
return INVALID_RANGE_NUMBER;
}
return NO_ERROR;
}
RangeResult LineRanges::isInRange(const RangeElemType e) {
if (searchFromTheStart) {
currentRange = lineRangeSet.begin();
searchFromTheStart = false;
}
while (currentRange != lineRangeSet.end()) {
if (currentRange->first < 0) {
// first and second cannot be both < 0 (already checked during add)
if (e <= currentRange->second) {
return IN_RANGE;
}
} else if (currentRange->second < 0) {
if (e >= currentRange->first) {
return IN_RANGE;
} else {
if ((contextLines > 0) && (currentRange->first - e)
<= contextLines) {
return CONTEXT_RANGE;
}
// makes no sense checking further ranges
return NOT_IN_RANGE;
}
} else if (currentRange->second == 0) {
// check perfect match
if (e == currentRange->first) {
return IN_RANGE;
} else if (e < currentRange->first) {
if (contextLines > 0) {
if (((currentRange->first - e) <= contextLines)) {
return CONTEXT_RANGE;
}
}
// makes no sense checking further ranges
return NOT_IN_RANGE;
} else if (contextLines > 0 && ((e - currentRange->first)
<= contextLines)) {
return CONTEXT_RANGE;
}
} else if (e >= currentRange->first && e <= currentRange->second) {
return IN_RANGE;
} else if (contextLines > 0 && (((e < currentRange->first)
&& ((currentRange->first - e) <= contextLines)) || ((e
> currentRange->second) && ((e - currentRange->second)
<= contextLines)))) {
return CONTEXT_RANGE;
} else if (e < currentRange->first) {
// makes no sense checking further ranges
return NOT_IN_RANGE;
}
// if we're here we try with another range in the set
currentRange++;
}
return NOT_IN_RANGE;
}
}
|