#!/usr/bin/ruby -Ku # # grassurl.rb - URL to Grass language generator # # Copyright (C) 2008 aike. All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions # are met: # # 1. Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # 2. Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in # the documentation and/or other materials provided with the # distribution. # # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR # BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, # WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE # OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN # IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # $KCODE='UTF8' class GrassUrl RELEASE_DATE = '2008-10-22' def makeCharTable(s) @header = "wwWWwWWWwvwwWWwWWWwWWWWwvwwwwWWWwwWwwWWWWWWwwwwWwwvwwwWWwWWWWwvWWwwwwWwwwwWWWwwwwwwWwwwwwwWwwwwwwwWWWWWWwwWwwwwwwwwwwWWWWWWWWWwWwwwwwwwWWWWWWWWWWWwWwwwwwwWWWWWWWWWWWWWwwwwwwwWwwwwwwwwWWWWWWWWWWWWWWWwwwwwwwwwwwwWwwvwWWWWWWWWWWWWWWWWWWWWWWwvWWwWwwwwwwwwwwwwwwwwwwwwwwwwWWWwWWWWwWWWWWwWWWWWWwWWWWWWWwWWWWWWWWwWWWWWWWWWwWWWWWWWWWWwWWWWWWWWWWWwWWWWWWWWWWWWwWWWWWWWWWWWWWwWWWWWWWWWWWWWWwWWWWWWWWWWWWWWWWWWWWwwwwwwwwwwwwwwwvwWWWWWWWWWWWWWWWWWwvWWwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww" # scan the max character code from the string max = maxChar(s) table_size = ('b'..max).to_a.length # generate alphabet table (from 'b' to max character code) b_z = "" table_size.times {|i| b_z += "WW" i.times { b_z += "W" } b_z += "w" } # generate alias of OUT abs_out = "vwWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW" table_size.times { abs_out += "W" } abs_out += "wv" @header += b_z + abs_out # initialize address table @table_base = 3 + 10 + (table_size + 1) + 3 # [./:0-9a-z] + (3 OPs) @char_pos = {} sp = 0 "./0123456789:".each_byte {|c| @char_pos[c.chr] = sp sp += 1 } sp += 2 ('a'..max).each {|c| @char_pos[c] = sp sp += 1 } end def generate(s) s = "http://" + s # raise a exception if character set [^a-z0-9./:] used raise "illegal char" if checkString(s).nil? makeCharTable(s) ret = @header out_pos = 1 stack_pos = 0 s.each_byte {|c| n = out_pos + stack_pos m = @table_base - @char_pos[c.chr] + stack_pos n.times {ret += "W"} m.times {ret += "w"} @char_pos[c.chr] = @table_base + stack_pos stack_pos += 1 } ret end def maxChar(s) s.split(//).max end # check character set def checkString(s) if s =~ /[^a-z0-9:\.\/]/ nil else true end end end #print GrassUrl.new.generate("aikelab.net/grass/")