lotrointerface.com
Search Downloads

LoTROInterface SVN SequenceBars

[/] [trunk/] [Thurallor/] [Common/] [Utils/] [Series.lua] - Blame information for rev 114

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 114 Thurallor-7095
Series = class(Turbine.Object);
2 Thurallor-7095
Thurallor.Utils.Series = Series;
3 Thurallor-7095
 
4 Thurallor-7095
function Series:Constructor(str)
5 Thurallor-7095
 
6 Thurallor-7095
    -- Eliminate unneeded chars
7 Thurallor-7095
    str = string.gsub(str, "([0-9]) +([0-9])", "%1;%2"); -- accept space-delimited numbers
8 Thurallor-7095
    str = string.gsub(str, "[^0-9,;.-]", "");            -- remove garbage chars
9 Thurallor-7095
    str = string.gsub(str, "^[^0-9]+", "");              -- remove leading nonnumeric chars
10 Thurallor-7095
    str = string.gsub(str, "[^0-9]$", "");               -- remove trailing nonnumeric chars
11 Thurallor-7095
    str = string.gsub(str, "%.+", "-");                  -- replace "..." with "-"
12 Thurallor-7095
    str = string.gsub(str, ",", ";");                    -- replace "," with ";"
13 Thurallor-7095
    str = string.gsub(str, "[^0-9][^0-9]+", ";");        -- eliminate unterminated ranges
14 Thurallor-7095
    self.str = str;
15 Thurallor-7095
 
16 Thurallor-7095
    self.caret = 1;
17 Thurallor-7095
    self.prev = nil;
18 Thurallor-7095
end
19 Thurallor-7095
 
20 Thurallor-7095
-- For-loop expression for getting the numbers in order (with optional "safety" limit value)
21 Thurallor-7095
function Series:numbers(safety)
22 Thurallor-7095
    self.safety = safety;
23 Thurallor-7095
 
24 Thurallor-7095
    self.iterator = function(state, num)
25 Thurallor-7095
        local last = state[4];
26 Thurallor-7095
        if (last) then
27 Thurallor-7095
            -- Continue current interval
28 Thurallor-7095
            if (num < last) then
29 Thurallor-7095
                return num + 1;
30 Thurallor-7095
            elseif (num > last) then
31 Thurallor-7095
                return num - 1;
32 Thurallor-7095
            end
33 Thurallor-7095
        end
34 Thurallor-7095
 
35 Thurallor-7095
        -- End of interval; get the next semicolon-delimited element from self.str
36 Thurallor-7095
        local f, s, var = unpack(state);
37 Thurallor-7095
        var = f(s, var);
38 Thurallor-7095
        if (var == nil) then
39 Thurallor-7095
            return nil;
40 Thurallor-7095
        end
41 Thurallor-7095
 
42 Thurallor-7095
        -- Get the next range and apply "safety" limit (if any)
43 Thurallor-7095
        local first = tonumber(string.match(var, "^[0-9]+"));
44 Thurallor-7095
        last = tonumber(string.match(var, "[0-9]+$"));
45 Thurallor-7095
        if (self.safety) then
46 Thurallor-7095
            first = math.min(first, self.safety);
47 Thurallor-7095
            last = math.min(last, self.safety);
48 Thurallor-7095
        end
49 Thurallor-7095
 
50 Thurallor-7095
        -- Save end of range; return first number in range
51 Thurallor-7095
        state[4] = last;
52 Thurallor-7095
        return first;
53 Thurallor-7095
    end
54 Thurallor-7095
 
55 Thurallor-7095
    -- Iterate over semicolon-delimited elements in self.str
56 Thurallor-7095
    return self.iterator, { string.gmatch(self.str, "[^;]+") };
57 Thurallor-7095
end
58 Thurallor-7095
 
59 Thurallor-7095
function Series:tostring()
60 Thurallor-7095
    local str = string.gsub(self.str, "-", "...");   -- replace "..." with "-"
61 Thurallor-7095
    return str;
62 Thurallor-7095
end

All times are GMT -5. The time now is 03:22 PM.


Our Network
EQInterface | EQ2Interface | Minion | WoWInterface | ESOUI | LoTROInterface | MMOUI | Swtorui