1 # Part of the zip-doc suite
2 # By Stian Haklev (shaklev@gmail.com), 2007
3 # Released under MIT and GPL licenses
5 # Just a few utility functions that are used in several places
6 # I started out with having these as extensions of String and IO, but
7 # I guess that's not very nice in a library? I keep the pop though -
8 # cannot believe it isn't standard.
13 return string.unpack('H40V4' * (string.size/36))
16 def pack(sha1, bstart, bsize, start, size)
17 return [sha1, bstart, bsize, start, size].pack('H40V4')
20 def sha1subset(four, no = 4)
21 four[0..(no-1)].to_i(16)
24 def sha1_w_sub(string, no = 4)
25 sha1 = Digest::SHA1.hexdigest( string )
26 firstfour = sha1subset( sha1, no )
27 return sha1, firstfour
30 def writeloc(file, text, offset)
35 def readloc(file, size, offset)
40 def strip_whitespace(txt)
41 return txt.gsub(/\t/, " ").gsub(' ',' ').gsub("\n", '')
44 # from http://railsruby.blogspot.com/2006/07/url-escape-and-url-unescape.html
45 def url_unescape(string)
46 string.tr('+', ' ').gsub(/((?:%[0-9a-fA-F]{2})+)/n) do
47 [$1.delete('%')].pack('H*')
55 module_function :unpack, :pack, :sha1subset, :writeloc, :readloc, :strip_whitespace
56 module_function :url_unescape, :npp, :sha1_w_sub