5d747e72d1f2

Fix the decoding.  Not sure it's fully done yet.
[view raw] [browse files]
author Steve Losh <steve@stevelosh.com>
date Wed, 09 Dec 2015 12:12:55 +0000
parents 1f8c437ae3c8
children 2975b6f50397
branches/tags (none)
files autoload/bencode.vim

Changes

--- a/autoload/bencode.vim	Sat Dec 05 22:33:04 2015 +0000
+++ b/autoload/bencode.vim	Wed Dec 09 12:12:55 2015 +0000
@@ -40,7 +40,7 @@
 function! bencode#BdecodeAll(bstring) " {{{
     let results = []
     let remaining = a:bstring
-    while remaining != ""
+    while remaining !~# '\v^\s*$'
         let dec = s:ActuallyBdecode(remaining)
         call add(results, dec[0])
         let remaining = dec[1]
@@ -53,14 +53,15 @@
 endfunction " }}}
 
 function! s:ActuallyBdecode(bstring) " {{{
-    if a:bstring[0] == 'i'
-        return s:BdecodeInteger(a:bstring)
-    elseif a:bstring[0] =~ '[0-9]'
-        return s:BdecodeString(a:bstring)
-    elseif a:bstring[0] == 'l'
-        return s:BdecodeList(a:bstring)
-    elseif a:bstring[0] == 'd'
-        return s:BdecodeDict(a:bstring)
+    let bs = substitute(a:bstring, '\v^\s*', '', '')
+    if bs[0] == 'i'
+        return s:BdecodeInteger(bs)
+    elseif bs[0] =~ '[0-9]'
+        return s:BdecodeString(bs)
+    elseif bs[0] == 'l'
+        return s:BdecodeList(bs)
+    elseif bs[0] == 'd'
+        return s:BdecodeDict(bs)
     else
         throw "shits fucked, yo"
     endif
@@ -109,11 +110,9 @@
         let rest = dec[1]
 
         let dec = s:ActuallyBdecode(rest)
-        let val = dec[0]
+        let result[key] = dec[0]
         let rest = dec[1]
 
-        let result[key] = val
-
         if rest[0] == 'e'
             "                   strip off the trailing e from the dict
             return [result, rest[1:]]