<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-GB">
	<id>https://stockhub.co/index.php?action=history&amp;feed=atom&amp;title=Module%3ASandbox%2FCousteau%2Fstrsubst</id>
	<title>Module:Sandbox/Cousteau/strsubst - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://stockhub.co/index.php?action=history&amp;feed=atom&amp;title=Module%3ASandbox%2FCousteau%2Fstrsubst"/>
	<link rel="alternate" type="text/html" href="https://stockhub.co/index.php?title=Module:Sandbox/Cousteau/strsubst&amp;action=history"/>
	<updated>2026-06-19T07:22:14Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.5</generator>
	<entry>
		<id>https://stockhub.co/index.php?title=Module:Sandbox/Cousteau/strsubst&amp;diff=145270&amp;oldid=prev</id>
		<title>imported&gt;Cousteau: Minor comment corrections</title>
		<link rel="alternate" type="text/html" href="https://stockhub.co/index.php?title=Module:Sandbox/Cousteau/strsubst&amp;diff=145270&amp;oldid=prev"/>
		<updated>2018-04-15T23:35:01Z</updated>

		<summary type="html">&lt;p&gt;Minor comment corrections&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;local p = {}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
-- Usage: {{#invoke:&amp;lt;this&amp;gt;|char|&amp;lt;string&amp;gt;|&amp;lt;c1&amp;gt;|&amp;lt;replacement1&amp;gt;|&amp;lt;c2&amp;gt;|&amp;lt;replacement2&amp;gt;[|sep=&amp;lt;separator&amp;gt;]}}&lt;br /&gt;
--   string: string to parse&lt;br /&gt;
--   c1, c2...: single characters&lt;br /&gt;
--   replacement1, replacement2...: what those characters are replaced by&lt;br /&gt;
--   separator: separator between replacements&lt;br /&gt;
-- Unknown characters are ignored and may be used for spacing.&lt;br /&gt;
-- Example: {{#invoke:&amp;lt;this&amp;gt;|.--.|.|dit|-|daah|sep=-}} --&amp;gt; dit-daah-daah-dit&lt;br /&gt;
function p.char(frame)&lt;br /&gt;
    -- Extract separator&lt;br /&gt;
    local sep = &amp;#039;&amp;#039;&lt;br /&gt;
    if frame.args.sep ~= nil then sep = frame.args.sep end&lt;br /&gt;
    -- Extract key-value pairs&lt;br /&gt;
    local tokenlist = {}&lt;br /&gt;
    local i = 2 -- NB: #frame.args doesn&amp;#039;t work; iterating the old way&lt;br /&gt;
    while frame.args[i] ~= nil do&lt;br /&gt;
        if frame.args[i] == &amp;#039;&amp;#039; then -- assume empty = space (but be careful with those)&lt;br /&gt;
            tokenlist[&amp;#039; &amp;#039;] = frame.args[i+1]&lt;br /&gt;
        else&lt;br /&gt;
            tokenlist[frame.args[i] ] = frame.args[i+1]&lt;br /&gt;
        end&lt;br /&gt;
        i = i+2&lt;br /&gt;
    end&lt;br /&gt;
    --TODO-- check no repeated keys, odd number of args...&lt;br /&gt;
    &lt;br /&gt;
    -- Parse string: replace each matching token by closest match&lt;br /&gt;
    local str = frame.args[1]&lt;br /&gt;
    local res = &amp;#039;&amp;#039;&lt;br /&gt;
    local isfirst = true&lt;br /&gt;
    for i = 1,#str do&lt;br /&gt;
        local c = str:sub(i,i) -- get i-th character&lt;br /&gt;
        if tokenlist[c] ~= nil then -- if c is a recognized character&lt;br /&gt;
            if isfirst then&lt;br /&gt;
                isfirst = false&lt;br /&gt;
            else&lt;br /&gt;
                res = res .. sep -- add separator (except for the first time)&lt;br /&gt;
            end&lt;br /&gt;
            res = res .. tokenlist[c]&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    return res&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
-- Usage: {{#invoke:&amp;lt;this&amp;gt;|str|&amp;lt;string&amp;gt;|&amp;lt;key1&amp;gt;|&amp;lt;replacement1&amp;gt;|&amp;lt;key2&amp;gt;|&amp;lt;replacement2&amp;gt;[|sep=separator]}}&lt;br /&gt;
--   string: string to parse&lt;br /&gt;
--   key1, key2...: substrings to replace (one or more characters)&lt;br /&gt;
--   replacement1, replacement2...: what those substrings are replaced by&lt;br /&gt;
--   separator: separator between replacements&lt;br /&gt;
-- If more than one substring matches, the longest one is used.&lt;br /&gt;
-- If no substring matches, one character is chomped; therefore unknown characters may be used for spacing.&lt;br /&gt;
-- This function might be more expensive than the .char() version.&lt;br /&gt;
-- Example: {{#invoke:&amp;lt;this&amp;gt;|dit daah daah dit|dit|.|daah|-|sep=/}} --&amp;gt; ./-/-/.&lt;br /&gt;
function p.str(frame)&lt;br /&gt;
    -- Extract separator&lt;br /&gt;
    local sep = &amp;#039;&amp;#039;&lt;br /&gt;
    if frame.args.sep ~= nil then sep = frame.args.sep end&lt;br /&gt;
    -- Extract key-value pairs&lt;br /&gt;
    local tokenlist = {}&lt;br /&gt;
    local i = 2 -- NB: #frame.args doesn&amp;#039;t work; iterating the old way&lt;br /&gt;
    while frame.args[i] ~= nil do&lt;br /&gt;
        if frame.args[i] == &amp;#039;&amp;#039; then -- assume empty = space (but be careful with those)&lt;br /&gt;
            table.insert(tokenlist, {k=&amp;#039; &amp;#039;, v=frame.args[i+1]})&lt;br /&gt;
        else&lt;br /&gt;
            table.insert(tokenlist, {k=frame.args[i], v=frame.args[i+1]})&lt;br /&gt;
        end&lt;br /&gt;
        i = i+2&lt;br /&gt;
    end&lt;br /&gt;
    --TODO-- check no repeated keys, odd number of args...&lt;br /&gt;
    -- Sort by key length so that longest strings are tried first&lt;br /&gt;
    table.sort(tokenlist, function(a,b) return #a.k &amp;gt; #b.k end)&lt;br /&gt;
    &lt;br /&gt;
    -- Parse string: replace each matching token by closest match&lt;br /&gt;
    local str = frame.args[1]&lt;br /&gt;
    local res = &amp;#039;&amp;#039;&lt;br /&gt;
    local isfirst = true&lt;br /&gt;
    &lt;br /&gt;
    while str ~= &amp;#039;&amp;#039; do&lt;br /&gt;
        local found = false&lt;br /&gt;
        for _,token in ipairs(tokenlist) do&lt;br /&gt;
            if str:sub(1, #token.k) == token.k then -- if str begins with substring&lt;br /&gt;
                if isfirst then&lt;br /&gt;
                    isfirst = false&lt;br /&gt;
                else&lt;br /&gt;
                    res = res .. sep -- add separator (except for the first time)&lt;br /&gt;
                end&lt;br /&gt;
                res = res .. token.v -- append replacement to res&lt;br /&gt;
                str = str:sub(#token.k+1) -- discard that substring&lt;br /&gt;
                found = true&lt;br /&gt;
                break&lt;br /&gt;
            end&lt;br /&gt;
        end&lt;br /&gt;
        &lt;br /&gt;
        if not found then&lt;br /&gt;
            str = str:sub(2) -- discard one character and try again&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    return res&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
-- [TEST] Print the arguments passed&lt;br /&gt;
function p.test(frame)&lt;br /&gt;
    local res = &amp;#039;&amp;#039;&lt;br /&gt;
    for k,v in pairs(frame.args) do&lt;br /&gt;
        res = res .. &amp;#039; [&amp;#039; .. k .. &amp;#039;]=&amp;quot;&amp;#039; .. v .. &amp;#039;&amp;quot;\n&amp;#039;&lt;br /&gt;
    end&lt;br /&gt;
    return res&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
return p&lt;/div&gt;</summary>
		<author><name>imported&gt;Cousteau</name></author>
	</entry>
</feed>