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?
-- internal replacement function
I got as far as this:
return text:gsub('@"%"','$$(' .. prefix .. '%)')
Which didn't work as intended, am I using the wrong function or have I simply written the pattern wrong?