• Announcement: Lua.org now officially recommends this forum as a meeting place for the Lua community

patsubst mimic didn't work correctly, tried fixing but hit infinite loop (1 Viewer)

awsdert

Newcomer
Joined
Apr 7, 2022
Messages
11
Reaction score
0
Since I know the rest of the functions work as intended (including the public patsubst function which calls this internally) I can only view this new version of the replacement loop as the culprit for the infinite loop, any ideas where I'm going wrong?

Code:
-- internal replacement function
local function rpatsubst( match, subst, arg )
    local swapped = true
    local i, e, tmp
    -- lua modifies the source by default, try to indicate a copy is wanted
    arg = '' .. arg
    while swapped == true do
        swapped = false
        for e = #arg, 0, -1 do
            -- Going backwards makes it easier to catch things like
            -- a(b(c)) which if traversed forwards would only work
            -- if there are no sub instances or on the 1st "instance" (b(c)
            for i = #arg, 0, -1 do
                tmp = matches( match, arg:sub(i,e) )
                if tmp then
                    local pfx = (arg:sub(1,-i) or '')
                    local sfx = (arg:sub(e+1) or '')
                    if match.qry == false then
                        arg = pfx .. subst.all .. sfx
                    else
                        arg = pfx .. subst.pfx .. tmp.txt
                        if subst.sfx ~= '' then
                            arg = arg .. subst.sfx .. sfx
                        end
                        arg = arg .. sfx
                    end
                    swapped = true
                    break
                end
            end
            if swapped == true then break end
        end
    end
    return arg
end
 

awsdert

Newcomer
Joined
Apr 7, 2022
Messages
11
Reaction score
0
Never mind, fixed it but also found encountered another infinite loop elsewhere, haven't identified the location of it yet though.

Code:
-- internal replacement function
local function rpatsubst( match, subst, arg )
    local i, e, tmp
    -- lua modifies the source by default, try to indicate a copy is wanted
    arg = '' .. arg
    e = #arg
    while e > 0 do
        -- Going backwards makes it easier to catch things like
        -- a(b(c)) which if traversed forwards would only work
        -- if there are no sub instances
        i = e
        while i > 0 do
            local txt = arg:sub(i,e)
            tmp = matches( match, txt )
            if tmp then
                local sfx = (arg:sub(e+1) or '')
                arg = (arg:sub(1,-i) or '')
                if match.qry == false then
                    arg = arg .. subst.all
                else
                    arg = arg .. subst.pfx .. tmp.txt
                    if subst.sfx ~= '' then
                        arg = arg .. subst.sfx
                    end
                end
                arg = arg .. sfx
                break
            end
            i = i - 1
        end
        e = e - 1
    end
    return arg
end
 
Top