Perl Nopaste Service

Download: Here New: Here

Nopaste from: Sat Nov 7 15:34:14 2015

Expires on: never

From: Enter your full name here (93.146.123.73)

Code language: Lua

Description: The user has not left a description.

Code:

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:
local function char(...) return string.char(...) end

function tokenize(C, text)
    local sep, fields = char(C), nil
    local pattern = string.format("([^%s]+)", sep)
    text:gsub(pattern, function(c) fields = (not fields) and c or fields..char(0)..c end)
    return fields
end

local function numtok(text, C)
    local count, tokens = 0, tokenize(C, text)
    for token in tokens:gmatch("%Z+") do count = count+1 end
    return count
end

local function gettok(sInput, iPosition, Separator, iRange)
    local count, iReturn = 1, nil
    local iSize, tTokens = numtok(sInput, Separator), tokenize(Separator, sInput)
    if iPosition == 0 then return sInput end
    local iStart, iStop = (iPosition > 0) and iPosition or (iSize + iPosition + 1)
    print(iStart, iStop)
    if not iRange or iPosition == iRange then
        tTokens:gsub("%Z+", function(c)
            if (iStart <= count) then
                iReturn = (not iReturn) and c or iReturn..char(Separator)..c
            end
            count = count+1
        end)
        return iReturn
    end
    if iRange > 0 then
        iStop = iRange
    elseif iRange == 0 or (iStart + iRange) > iSize then
        iStop = iSize
    else
        iStop = iStart + iRange + 1
    end
    if iStart == iStop then
        tTokens:gsub("%Z+", function(c)
            if (iStart <= count) then
                iReturn = (not iReturn) and c or iReturn..char(Separator)..c
            end
            count = count+1
        end)
    end
    if iStart > iStop then
        iStart, iStop = iStop, iStart
        tTokens:gsub("%Z+", function(c)
            if (iStart <= count <= iStop) then
                iReturn = (not iReturn) and c or iReturn..char(Separator)..c
            end
            count = count+1
        end)
    end
    return iReturn
end