Module:Sandbox/Cousteau/strsubst

.mw-parser-output .documentation,.mw-parser-output .documentation-metadata{border:1px solid var(--border-color-base,#a2a9b1);background-color:#ecfcf4;color:inhe

Module:Sandbox/Cousteau/strsubst
local p = {}


-- Usage: {{#invoke:<this>|char|<string>|<c1>|<replacement1>|<c2>|<replacement2>[|sep=<separator>]}}
--   string: string to parse
--   c1, c2...: single characters
--   replacement1, replacement2...: what those characters are replaced by
--   separator: separator between replacements
-- Unknown characters are ignored and may be used for spacing.
-- Example: {{#invoke:<this>|.--.|.|dit|-|daah|sep=-}} --> dit-daah-daah-dit
function p.char(frame)
    -- Extract separator
    local sep = ''
    if frame.args.sep ~= nil then sep = frame.args.sep end
    -- Extract key-value pairs
    local tokenlist = {}
    local i = 2 -- NB: #frame.args doesn't work; iterating the old way
    while frame.args[i] ~= nil do
        if frame.args[i] == '' then -- assume empty = space (but be careful with those)
            tokenlist[' '] = frame.args[i+1]
        else
            tokenlist[frame.args[i] ] = frame.args[i+1]
        end
        i = i+2
    end
    --TODO-- check no repeated keys, odd number of args...
    
    -- Parse string: replace each matching token by closest match
    local str = frame.args[1]
    local res = ''
    local isfirst = true
    for i = 1,#str do
        local c = str:sub(i,i) -- get i-th character
        if tokenlist[c] ~= nil then -- if c is a recognized character
            if isfirst then
                isfirst = false
            else
                res = res .. sep -- add separator (except for the first time)
            end
            res = res .. tokenlist[c]
        end
    end
    
    return res
end


-- Usage: {{#invoke:<this>|str|<string>|<key1>|<replacement1>|<key2>|<replacement2>[|sep=separator]}}
--   string: string to parse
--   key1, key2...: substrings to replace (one or more characters)
--   replacement1, replacement2...: what those substrings are replaced by
--   separator: separator between replacements
-- If more than one substring matches, the longest one is used.
-- If no substring matches, one character is chomped; therefore unknown characters may be used for spacing.
-- This function might be more expensive than the .char() version.
-- Example: {{#invoke:<this>|dit daah daah dit|dit|.|daah|-|sep=/}} --> ./-/-/.
function p.str(frame)
    -- Extract separator
    local sep = ''
    if frame.args.sep ~= nil then sep = frame.args.sep end
    -- Extract key-value pairs
    local tokenlist = {}
    local i = 2 -- NB: #frame.args doesn't work; iterating the old way
    while frame.args[i] ~= nil do
        if frame.args[i] == '' then -- assume empty = space (but be careful with those)
            table.insert(tokenlist, {k=' ', v=frame.args[i+1]})
        else
            table.insert(tokenlist, {k=frame.args[i], v=frame.args[i+1]})
        end
        i = i+2
    end
    --TODO-- check no repeated keys, odd number of args...
    -- Sort by key length so that longest strings are tried first
    table.sort(tokenlist, function(a,b) return #a.k > #b.k end)
    
    -- Parse string: replace each matching token by closest match
    local str = frame.args[1]
    local res = ''
    local isfirst = true
    
    while str ~= '' do
        local found = false
        for _,token in ipairs(tokenlist) do
            if str:sub(1, #token.k) == token.k then -- if str begins with substring
                if isfirst then
                    isfirst = false
                else
                    res = res .. sep -- add separator (except for the first time)
                end
                res = res .. token.v -- append replacement to res
                str = str:sub(#token.k+1) -- discard that substring
                found = true
                break
            end
        end
        
        if not found then
            str = str:sub(2) -- discard one character and try again
        end
    end
    
    return res
end


-- [TEST] Print the arguments passed
function p.test(frame)
    local res = ''
    for k,v in pairs(frame.args) do
        res = res .. ' [' .. k .. ']="' .. v .. '"\n'
    end
    return res
end


return p

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.