-------------------------------------------------------------------------------- -- Title: IPMnemonic.lua -- Description: Like a square peg in a round hole -- Author: Raphaël Szwarc http://alt.textdrive.com/lua/ -- Creation Date: August 30, 2008 -- Legal: Copyright (C) 2008 Raphaël Szwarc -- Under the terms of the MIT License -- http://www.opensource.org/licenses/mit-license.html -------------------------------------------------------------------------------- -- import dependencies local table = require( 'table' ) local getmetatable = getmetatable local setmetatable = setmetatable local tonumber = tonumber local tostring = tostring -------------------------------------------------------------------------------- -- IPMnemonic -------------------------------------------------------------------------------- module( 'IPMnemonic' ) _VERSION = '1.0' local self = setmetatable( _M, {} ) local meta = getmetatable( self ) -------------------------------------------------------------------------------- -- Utilities -------------------------------------------------------------------------------- -- IP Mnemonics as per -- http://gurno.com/adam/mne/ local map = { [ 0 ] = 'zip', 'ace', 'act', 'add', 'age', 'aim', 'air', 'and', 'ant', 'ape', 'arm', 'art', 'ash', 'ask', 'bad', 'bag', 'ban', 'bar', 'bat', 'bay', 'bed', 'bet', 'bid', 'big', 'bin', 'bit', 'bog', 'boo', 'box', 'bud', 'bug', 'bun', 'bus', 'cab', 'can', 'cap', 'car', 'cat', 'cop', 'cot', 'cow', 'cry', 'cub', 'cup', 'cut', 'day', 'den', 'did', 'die', 'dig', 'dim', 'dip', 'dog', 'dry', 'dub', 'dud', 'dug', 'ear', 'eat', 'eel', 'egg', 'elf', 'elk', 'elm', 'end', 'fan', 'far', 'fat', 'fed', 'few', 'fib', 'fig', 'fin', 'fit', 'fix', 'fly', 'fog', 'foo', 'fox', 'fry', 'fun', 'gab', 'gag', 'gap', 'gas', 'gel', 'gem', 'get', 'gin', 'got', 'gum', 'gut', 'had', 'has', 'hat', 'hen', 'hex', 'hid', 'hip', 'hit', 'hog', 'hop', 'hot', 'how', 'hub', 'hug', 'hum', 'hut', 'ice', 'ill', 'imp', 'ink', 'irk', 'jab', 'jam', 'jar', 'jaw', 'jet', 'jig', 'job', 'jog', 'jot', 'joy', 'key', 'kid', 'kin', 'kit', 'lab', 'lag', 'lap', 'law', 'lax', 'lay', 'leg', 'let', 'lid', 'lip', 'lit', 'lot', 'low', 'mad', 'map', 'mat', 'men', 'met', 'mix', 'mob', 'moo', 'mop', 'mud', 'mug', 'nab', 'nag', 'nap', 'net', 'new', 'nil', 'nip', 'nod', 'nor', 'now', 'nut', 'oak', 'oat', 'odd', 'off', 'old', 'orb', 'out', 'owl', 'own', 'pad', 'pal', 'pan', 'pay', 'pen', 'pet', 'pie', 'pig', 'pin', 'pit', 'ply', 'pod', 'pop', 'pot', 'pox', 'pry', 'pun', 'pup', 'put', 'rag', 'ran', 'rat', 'raw', 'red', 'rid', 'rig', 'rip', 'rot', 'row', 'rub', 'rug', 'run', 'rut', 'rye', 'sad', 'sag', 'sap', 'sat', 'saw', 'say', 'set', 'shy', 'sip', 'sit', 'ski', 'sky', 'sly', 'sob', 'soy', 'spa', 'spy', 'tab', 'tag', 'tan', 'tap', 'tar', 'tax', 'the', 'tie', 'tin', 'tip', 'top', 'toy', 'try', 'tub', 'tug', 'use', 'van', 'vat', 'vex', 'vow', 'wag', 'war', 'was', 'wax', 'web', 'wet', 'who', 'wig', 'win', 'wit', 'yes', 'yet', 'zoo', 'all' } local function Capitalize( aValue ) return ( aValue:lower():gsub( '(%l)([%w_\']*)', function( first, second ) return first:upper() .. second end ) ) end local function Mnemonic( anAddress ) local anAddress = anAddress or '' local first, second, third, fourth = anAddress:match( '(%d+)%.(%d+)%.(%d+)%.(%d+)' ) local aBuffer = {} aBuffer[ #aBuffer + 1 ] = Capitalize( map[ tonumber( first ) or 0 ] ) aBuffer[ #aBuffer + 1 ] = ' ' aBuffer[ #aBuffer + 1 ] = Capitalize( map[ tonumber( second ) or 0 ] ) aBuffer[ #aBuffer + 1 ] = map[ tonumber( third ) or 0 ] aBuffer[ #aBuffer + 1 ] = map[ tonumber( fourth ) or 0 ] return table.concat( aBuffer, '' ) end -------------------------------------------------------------------------------- -- Metamethods -------------------------------------------------------------------------------- function meta:__index( aKey ) return Mnemonic( aKey ) end function meta:__concat( aValue ) return tostring( self ) .. tostring( aValue ) end function meta:__tostring() return ( '%s/%s' ):format( self._NAME, self._VERSION ) end