Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Implementation of hash#Adler32 for earlier vim versions; g:tlib#hash#…

…use_adler32
  • Loading branch information...
commit 77a7da177eba7a2abd6e08e1b0848ce2846d6cb5 1 parent 8d003ef
Tom Link authored
Showing with 126 additions and 33 deletions.
  1. +92 −31 autoload/tlib/hash.vim
  2. +34 −2 spec/tlib/hash.vim
123 autoload/tlib/hash.vim
View
@@ -1,42 +1,69 @@
" @Author: Tom Link (mailto:micathom AT gmail com?subject=[vim])
" @License: GPL (see http://www.gnu.org/licenses/gpl.txt)
-" @Revision: 224
+" @Revision: 276
if !exists('g:tlib#hash#use_crc32')
- let g:tlib#hash#use_crc32 = 'auto' "{{{2
+ let g:tlib#hash#use_crc32 = '' "{{{2
+endif
+
+
+if !exists('g:tlib#hash#use_adler32')
+ let g:tlib#hash#use_adler32 = '' "{{{2
endif
function! tlib#hash#CRC32B(chars) "{{{3
- if has('ruby') && g:tlib#hash#use_crc32 =~ '\<\(auto\|ruby\)\>'
- let rv = ''
- ruby << EOR
- require 'zlib'
- VIM::command('let rv = "%08X"' % Zlib.crc32(VIM::evaluate("a:chars")))
-EOR
- " elseif has('python') && g:tlib#hash#use_crc32 =~ '\<\(auto\|python\)\>'
- " elseif has('perl') && g:tlib#hash#use_crc32 =~ '\<\(auto\|perl\)\>'
- " elseif has('tcl') && g:tlib#hash#use_crc32 =~ '\<\(auto\|tcl\)\>'
+ if !empty(g:tlib#hash#use_crc32)
+ let use = g:tlib#hash#use_crc32
+ elseif has('ruby')
+ let use = 'ruby'
else
- " based on http://rosettacode.org/wiki/CRC-32
- if !exists('s:crc_table')
- let cfile = tlib#persistent#Filename('tlib', 'crc_table', 1)
- let s:crc_table = tlib#persistent#Value(cfile, 'tlib#hash#CreateCrcTable', 0)
+ let use = 'vim'
+ endif
+ if exists('*tlib#hash#CRC32B_'. use)
+ return tlib#hash#CRC32B_{use}(a:chars)
+ else
+ throw "Unknown version of tlib#hash#CRC32B: ". use
+ endif
+endf
+
+
+function! tlib#hash#CRC32B_ruby(chars) "{{{3
+ if has('ruby')
+ let rv = ''
+ if !exists('s:loaded_ruby_zlib')
+ ruby require 'zlib'
+ let s:loaded_ruby_zlib = 1
endif
- let xFFFF_FFFF = repeat([1], 32)
- let crc = tlib#bitwise#XOR([0], xFFFF_FFFF, 'bits')
- for char in split(a:chars, '\zs')
- let octet = char2nr(char)
- let r1 = tlib#bitwise#ShiftRight(crc, 8)
- let i0 = tlib#bitwise#AND(crc, xFFFF_FFFF, 'bits')
- let i1 = tlib#bitwise#XOR(i0, octet, 'bits')
- let i2 = tlib#bitwise#Bits2Num(tlib#bitwise#AND(i1, 0xff, 'bits'))
- let r2 = s:crc_table[i2]
- let crc = tlib#bitwise#XOR(r1, r2, 'bits')
- endfor
- let crc = tlib#bitwise#XOR(crc, xFFFF_FFFF, 'bits')
- let rv = tlib#bitwise#Bits2Num(crc, 16)
+ ruby VIM::command('let rv = "%08X"' % Zlib.crc32(VIM::evaluate("a:chars")))
+ return rv
+ else
+ throw "tlib#hash#CRC32B_ruby not supported in this version of vim"
+ endif
+endf
+
+
+function! tlib#hash#CRC32B_vim(chars) "{{{3
+ if !exists('s:crc_table')
+ let cfile = tlib#persistent#Filename('tlib', 'crc_table', 1)
+ let s:crc_table = tlib#persistent#Value(cfile, 'tlib#hash#CreateCrcTable', 0)
+ endif
+ let xFFFF_FFFF = repeat([1], 32)
+ let crc = tlib#bitwise#XOR([0], xFFFF_FFFF, 'bits')
+ for char in split(a:chars, '\zs')
+ let octet = char2nr(char)
+ let r1 = tlib#bitwise#ShiftRight(crc, 8)
+ let i0 = tlib#bitwise#AND(crc, xFFFF_FFFF, 'bits')
+ let i1 = tlib#bitwise#XOR(i0, octet, 'bits')
+ let i2 = tlib#bitwise#Bits2Num(tlib#bitwise#AND(i1, 0xff, 'bits'))
+ let r2 = s:crc_table[i2]
+ let crc = tlib#bitwise#XOR(r1, r2, 'bits')
+ endfor
+ let crc = tlib#bitwise#XOR(crc, xFFFF_FFFF, 'bits')
+ let rv = tlib#bitwise#Bits2Num(crc, 16)
+ if len(rv) < 8
+ let rv = repeat('0', 8 - len(rv)) . rv
endif
return rv
endf
@@ -66,9 +93,42 @@ endf
function! tlib#hash#Adler32(chars) "{{{3
- if !exists('*or')
+ if !empty(g:tlib#hash#use_adler32)
+ let use = g:tlib#hash#use_adler32
+ elseif exists('*or')
+ let use = 'vim'
+ else
+ let use = 'tlib'
+ endif
+ if exists('*tlib#hash#Adler32_'. use)
+ return tlib#hash#Adler32_{use}(a:chars)
+ else
+ throw "Unknown version of tlib#hash#Adler32_: ". use
+ endif
+endf
+
+
+function! tlib#hash#Adler32_vim(chars) "{{{3
+ if exists('*or')
+ let mod_adler = 65521
+ let a = 1
+ let b = 0
+ for index in range(len(a:chars))
+ let c = char2nr(a:chars[index])
+ let a = (a + c) % mod_adler
+ let b = (b + a) % mod_adler
+ endfor
+ let bb = b * float2nr(pow(2, 16))
+ let checksum = or(bb, a)
+ " TLogVAR checksum, a, b, bb
+ return printf("%08X", checksum)
+ else
throw "TLIB: Vim version doesn't support bitwise or()"
endif
+endf
+
+
+function! tlib#hash#Adler32_tlib(chars) "{{{3
let mod_adler = 65521
let a = 1
let b = 0
@@ -77,8 +137,9 @@ function! tlib#hash#Adler32(chars) "{{{3
let a = (a + c) % mod_adler
let b = (b + a) % mod_adler
endfor
- let checksum = or(b * float2nr(pow(2, 16)), a)
- return printf("%08X", checksum)
+ let bb = tlib#bitwise#ShiftLeft(tlib#bitwise#Num2Bits(b), 16)
+ let checksum = tlib#bitwise#OR(bb, a, "bits")
+ return printf('%08s', tlib#bitwise#Bits2Num(checksum, 16))
endf
36 spec/tlib/hash.vim
View
@@ -1,7 +1,7 @@
" @Author: Tom Link (mailto:micathom AT gmail com?subject=[vim])
" @Website: http://www.vim.org/account/profile.php?user_id=4037
" @License: GPL (see http://www.gnu.org/licenses/gpl.txt)
-" @Revision: 13
+" @Revision: 31
SpecBegin 'title': 'tlib#hash'
@@ -21,6 +21,38 @@ Should be equal tlib#hash#CRC32B('The quick brown fox jumps over the lazy dog'),
Should be equal tlib#hash#CRC32B('foo'), '8C736521'
Should be equal tlib#hash#CRC32B('f'), '76D32BE0'
-let g:tlib#hash#use_crc32 = g:tlib_hash_use_crc32
+function! s:CompareHash(text) "{{{3
+ if !empty(a:text)
+ exec 'It should calculate the crc32b checksum for:' a:text
+ let crc32ruby = tlib#hash#CRC32B_ruby(a:text)
+ let crc32vim = tlib#hash#CRC32B_vim(a:text)
+ exec 'Should be equal' string(crc32ruby) ',' string(crc32vim)
+ exec 'It should calculate the adler32 checksum for:' a:text
+ let adler32tlib = tlib#hash#Adler32_tlib(a:text)
+ let adler32vim = tlib#hash#Adler32_vim(a:text)
+ exec 'Should be equal' string(adler32tlib) ',' string(adler32vim)
+ endif
+endf
+
+redir => s:scriptnames
+silent scriptnames
+redir END
+for s:script in split(s:scriptnames, '\n')
+ let s:scriptfile = matchstr(s:script, '^\s*\d\+:\s\+\zs.*$')
+ call s:CompareHash(s:scriptfile)
+ try
+ let s:scriptlines = readfile(s:scriptfile)
+ call s:CompareHash(join(s:scriptlines, "\n"))
+ for s:scriptline in s:scriptlines
+ call s:CompareHash(s:scriptline)
+ endfor
+ catch /^Vim\%((\a\+)\)\=:E484/
+ endtry
+endfor
+unlet s:scriptnames, :script, s:scriptfile, s:scriptlines, s:scriptline
+delf s:CompareHash
+
+
+let g:tlib#hash#use_crc32 = g:tlib_hash_use_crc32
Please sign in to comment.
Something went wrong with that request. Please try again.