root/HTTP/IPMnemonic.lua

Revision 1484 (checked in by rsz, 10 months ago)

cleanup

Line 
1 --------------------------------------------------------------------------------
2 -- Title:               IPMnemonic.lua
3 -- Description:         Like a square peg in a round hole
4 -- Author:              Raphaël Szwarc http://alt.textdrive.com/lua/
5 -- Creation Date:       August 30, 2008
6 -- Legal:               Copyright (C) 2008 Raphaël Szwarc
7 --                      Under the terms of the MIT License
8 --                      http://www.opensource.org/licenses/mit-license.html
9 --------------------------------------------------------------------------------
10
11 -- import dependencies
12 local table = require( 'table' )
13
14 local getmetatable = getmetatable
15 local setmetatable = setmetatable
16 local tonumber = tonumber
17 local tostring = tostring
18
19 --------------------------------------------------------------------------------
20 -- IPMnemonic
21 --------------------------------------------------------------------------------
22
23 module( 'IPMnemonic' )
24 _VERSION = '1.0'
25
26 local self = setmetatable( _M, {} )
27 local meta = getmetatable( self )
28
29 --------------------------------------------------------------------------------
30 -- Utilities
31 --------------------------------------------------------------------------------
32
33 -- IP Mnemonics as per
34 -- http://gurno.com/adam/mne/
35
36 local map =
37 {
38 [ 0 ] =
39 'zip', 'ace', 'act', 'add', 'age', 'aim', 'air', 'and', 'ant', 'ape', 'arm', 'art', 'ash', 'ask', 'bad', 'bag',
40 'ban', 'bar', 'bat', 'bay', 'bed', 'bet', 'bid', 'big', 'bin', 'bit', 'bog', 'boo', 'box', 'bud', 'bug', 'bun',
41 'bus', 'cab', 'can', 'cap', 'car', 'cat', 'cop', 'cot', 'cow', 'cry', 'cub', 'cup', 'cut', 'day', 'den', 'did',
42 'die', 'dig', 'dim', 'dip', 'dog', 'dry', 'dub', 'dud', 'dug', 'ear', 'eat', 'eel', 'egg', 'elf', 'elk', 'elm',
43 'end', 'fan', 'far', 'fat', 'fed', 'few', 'fib', 'fig', 'fin', 'fit', 'fix', 'fly', 'fog', 'foo', 'fox', 'fry',
44 'fun', 'gab', 'gag', 'gap', 'gas', 'gel', 'gem', 'get', 'gin', 'got', 'gum', 'gut', 'had', 'has', 'hat', 'hen',
45 'hex', 'hid', 'hip', 'hit', 'hog', 'hop', 'hot', 'how', 'hub', 'hug', 'hum', 'hut', 'ice', 'ill', 'imp', 'ink',
46 'irk', 'jab', 'jam', 'jar', 'jaw', 'jet', 'jig', 'job', 'jog', 'jot', 'joy', 'key', 'kid', 'kin', 'kit', 'lab',
47 'lag', 'lap', 'law', 'lax', 'lay', 'leg', 'let', 'lid', 'lip', 'lit', 'lot', 'low', 'mad', 'map', 'mat', 'men',
48 'met', 'mix', 'mob', 'moo', 'mop', 'mud', 'mug', 'nab', 'nag', 'nap', 'net', 'new', 'nil', 'nip', 'nod', 'nor',
49 'now', 'nut', 'oak', 'oat', 'odd', 'off', 'old', 'orb', 'out', 'owl', 'own', 'pad', 'pal', 'pan', 'pay', 'pen',
50 'pet', 'pie', 'pig', 'pin', 'pit', 'ply', 'pod', 'pop', 'pot', 'pox', 'pry', 'pun', 'pup', 'put', 'rag', 'ran',
51 'rat', 'raw', 'red', 'rid', 'rig', 'rip', 'rot', 'row', 'rub', 'rug', 'run', 'rut', 'rye', 'sad', 'sag', 'sap',
52 'sat', 'saw', 'say', 'set', 'shy', 'sip', 'sit', 'ski', 'sky', 'sly', 'sob', 'soy', 'spa', 'spy', 'tab', 'tag',
53 'tan', 'tap', 'tar', 'tax', 'the', 'tie', 'tin', 'tip', 'top', 'toy', 'try', 'tub', 'tug', 'use', 'van', 'vat',
54 'vex', 'vow', 'wag', 'war', 'was', 'wax', 'web', 'wet', 'who', 'wig', 'win', 'wit', 'yes', 'yet', 'zoo', 'all'
55 }
56
57 local function Capitalize( aValue )
58     return ( aValue:lower():gsub( '(%l)([%w_\']*)', function( first, second ) return first:upper() .. second end ) )
59 end
60
61 local function Mnemonic( anAddress )
62     local anAddress = anAddress or ''
63     local first, second, third, fourth = anAddress:match( '(%d+)%.(%d+)%.(%d+)%.(%d+)' )
64     local aBuffer = {}
65    
66     aBuffer[ #aBuffer + 1 ] = Capitalize( map[ tonumber( first ) or 0 ] )
67     aBuffer[ #aBuffer + 1 ] = ' '
68     aBuffer[ #aBuffer + 1 ] = Capitalize( map[ tonumber( second ) or 0 ] )
69     aBuffer[ #aBuffer + 1 ] = map[ tonumber( third ) or 0 ]
70     aBuffer[ #aBuffer + 1 ] = map[ tonumber( fourth ) or 0 ]
71    
72     return table.concat( aBuffer, '' )
73 end
74
75 --------------------------------------------------------------------------------
76 -- Metamethods
77 --------------------------------------------------------------------------------
78
79 function meta:__index( aKey )
80     return Mnemonic( aKey )
81 end
82
83 function meta:__concat( aValue )
84     return tostring( self ) .. tostring( aValue )
85 end
86
87 function meta:__tostring()
88     return ( '%s/%s' ):format( self._NAME, self._VERSION )
89 end
90
Note: See TracBrowser for help on using the browser.