RosettaCodeData/Task/Abbreviations-easy/Pluto/abbreviations-easy.pluto
2026-04-30 12:34:36 -04:00

79 lines
3.4 KiB
Text

do -- Abbreviatiions Easy - expand command name abbreviations
-- returns the table of space delimited words in s
local function getWords( s : string ) : table
local t, cmds, w = s, {}, s
while t:contains( " " ) do
w, t = t:partition( " " )
if w != "" then
cmds[ # cmds + 1 ] = w
end
end
if t != "" then
cmds[ # cmds + 1 ] = t
end
return cmds
end
local commandStrings = "Add ALTer BAckup Bottom CAppend Change SCHANGE CInsert CLAst COMPress COpy "
.. "COUnt COVerlay CURsor DELete CDelete Down DUPlicate Xedit EXPand EXTract Find "
.. "NFind NFINDUp NFUp CFind FINdup FUp FOrward GET Help HEXType Input POWerinput "
.. "Join SPlit SPLTJOIN LOAD Locate CLocate LOWercase UPPercase LPrefix MACRO "
.. "MErge MODify MOve MSG Next Overlay PARSE PREServe PURge PUT PUTD Query QUIT "
.. "READ RECover REFRESH RENum REPeat Replace CReplace RESet RESTore RGTLEFT "
.. "RIght LEft SAVE SET SHift SI SORT SOS STAck STATus TOP TRAnsfer Type Up"
local function expandEasyAbbreviations( cmds : string, input : string ) : table
local cmdTable, inputTable, outputTable = getWords( cmds ), getWords( input ), {}
local function isCommand( cmd : string, word : string ) : boolean
local result, uCmd, uWord = false, cmd:upper(), word:upper()
if # cmd >= # word then -- word is not longer than the command
if uCmd == uWord then -- the word is the command
result = true
else -- word is shorter than the command
local ab = cmd:gsub( "%l", "" ) -- remove lowercase
if # ab <= # word then -- the minimum abbreviation isn't longer than the word
result = uWord == uCmd:sub( 1, # word )
end
end
end
return result
end
for i = 1, # inputTable do
local w, u = inputTable[ i ], "*error*"
for c = 1, # cmdTable do
if isCommand( cmdTable[ c ], w ) then
u = cmdTable[ c ]:upper()
break
end
end
outputTable[ # outputTable + 1 ] = u
end
return { inputTable, outputTable }
end
local function printExpansion( expansion : table )
local fmt, input, output = {}, expansion[ 1 ], expansion[ 2 ]
for i, v in ipairs( output ) do
fmt[ i ] = " %" .. # v .. "s"
end
io.write( "Input: " )
for i, v in ipairs( input ) do
io.write( string.format( fmt[ i ], v ) )
end
io.write( "\nOutput:" )
for i, v in ipairs( output ) do
io.write( string.format( fmt[ i ], v ) )
end
io.write( "\n" )
end
printExpansion( expandEasyAbbreviations( commandStrings
, ( "riG rePEAT copies put mo rest "
.. "types fup. 6 poweRin"
)
)
)
end