Commits

ZyX_I committed b3ac7ea

@/base64: Added encodelines and decodelines functions

  • Participants
  • Parent commits 298bb6a

Comments (0)

Files changed (4)

         Encode string or a list of numbers each representing a single byte 
         (thus restricted to be between 0 and 255 inclusive).
 
+base64.encodelines :: [ str ] -> b64str                 *frawor-r-encodelines*
+base64.decodelines :: b64str -> [ str ]                 *frawor-r-decodelines*
+        Similar to above functions, but these work with a list of lines like 
+        returned by |readfile()| or |getline()| with two arguments (see 
+        |NL-used-for-Nul|).
+
 ==============================================================================
 5. Options                                                    *frawor-options*
                                                              *g:fraworOptions*

plugin/frawor/base64.vim

     finish
 endif
 let s:F.base64={}
-"▶1 and           :: UInt, UInt → UInt
+"▶1 and                :: UInt, UInt → UInt
 function s:F.and(v1, v2)
     let [v1, v2]=[a:v1, a:v2]
     let list=[]
     endwhile
     return r
 endfunction
-"▶1 or            :: UInt, UInt → UInt
+"▶1 or                 :: UInt, UInt → UInt
 function s:F.or(v1, v2)
     let [v1, v2]=[a:v1, a:v2]
     let list=[]
     endwhile
     return r
 endfunction
-"▶1 base64.decode :: b64str[, bytearray::Bool] → str | bytearray
+"▶1 base64.decode      :: b64str[, bytearray::Bool] → str | bytearray
 let s:cd64=map(split("|$$$}rstuvwxyz{$$$$$$$>?@ABCDEFGHIJKLMNOPQRSTUVW$$$$$$XYZ[\\]^_`abcdefghijklmnopq",
             \              '\v.@='),
             \        'char2nr(v:val)')
     endwhile
     return r
 endfunction
-"▶1 base64.encode :: str | bytearray → b64str
+"▶1 base64.encode      :: str | bytearray → b64str
 let s:eqsigncode=char2nr('=')
 let s:cb64=map(split("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",
             \        '\v.@='), 'char2nr(v:val)')
     endwhile
     return r
 endfunction
+"▶1 base64.encodelines :: [string] → b64str
+function s:F.base64.encodelines(lines)
+    let bytes=[]
+    let i=0
+    let lls=len(a:lines)
+    while i<lls
+        let j=0
+        let ll=len(a:lines[i])
+        while j<ll
+            let byte=char2nr(a:lines[i][j])
+            if byte==10 " NL
+                call add(bytes, 0)
+            else
+                call add(bytes, byte)
+            endif
+            let j+=1
+        endwhile
+        call add(bytes, 10)
+        let i+=1
+    endwhile
+    call remove(bytes, -1) " Remove last NL
+    return s:F.base64.encode(bytes)
+endfunction
+"▶1 base64.decodelines :: b64str → [string]
+function s:F.base64.decodelines(b64str)
+    let r=['']
+    for byte in s:F.base64.decode(a:b64str, 1)
+        if byte==10
+            call add(r, '')
+        elseif byte==0
+            let r[-1].="\n"
+        else
+            let r[-1].=eval(printf('"\x%02x"', byte))
+        endif
+    endfor
+    return r
+endfunction
 "▶1 post resource
 call s:_f.postresource('base64', s:F.base64)
 "▶1

plugin/frawor/mappings.vim

 "▶1 Define variables
 let s:_oprefix='frawormap'
 let s:_options={}
-let s:hasdictmap=(v:version==703 && has('patch32')) || v:version>703
 let s:ablhsreg='\v^%(\k+|%(\k@!.)+\k|\S*\k@!.)$'
 "▶1 Define messages
 if v:lang=~?'ru'
 endif
 "▶1 savemap       :: lhs, mode, abbr → Maybe mapdescr
 "▶2 savemap for newer vim
-if s:hasdictmap
+if (v:version==703 && has('patch32')) || v:version>703
     function s:F.savemap(lhs, mode, abbr)
         let map=maparg(a:lhs, a:mode, a:abbr, 1)
         if !empty(map)

test/rtp/plugin/base64.vim

             \['QUJD', 'ABC'],
             \['QUJDRA==', 'ABCD'],
             \["AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIj\nJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZH\nSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWpr\nbG1ub3BxcnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6P\nkJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq+wsbKz\ntLW2t7i5uru8vb6/wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX\n2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7\n/P3+/w==                                        ",
-            \ range(0, 0xFF)]
+            \ range(0, 0xFF)],
+            \['QUJDAApBQkMAREVGCgBBQkM=', ["ABC\n", "ABC\nDEF", "\nABC"]],
         \]
 let i=0
 for [in, out] in tests
     if type(out)==type([])
-        let d=s:_r.base64.decode(in, 1)
-        if d!=out
-            echom 'Failed '.i.':'
-            echom ' in: '.in
-            echom '  d: '.string(d)
-            echom 'out: '.string(out)
+        if type(out[0])==type('')
+            let d=s:_r.base64.decodelines(in)
+            let e=s:_r.base64.encodelines(d)
+            let d2=s:_r.base64.decodelines(e)
+        else
+            let d=s:_r.base64.decode(in, 1)
+            let e=s:_r.base64.encode(d)
+            let d2=s:_r.base64.decode(e, 1)
         endif
-        let e=s:_r.base64.encode(d)
-        let d2=s:_r.base64.decode(e, 1)
-        if d2!=out
-            echom 'Failed(2) '.i.':'
-            echom ' in: '.in
-            echom '  d: '.string(d)
-            echom 'out: '.string(out)
-        endif
-        unlet d e d2
     else
         let d=s:_r.base64.decode(in)
-        if d!=#out
-            echom 'Failed '.i.':'
-            echom ' in: '.in
-            echom '  d: '.d
-            echom 'out: '.out
-        endif
         let e=s:_r.base64.encode(d)
         let d2=s:_r.base64.decode(e)
-        if d2!=#out
-            echom 'Failed(2) '.i.':'
-            echom ' in: '.in
-            echom '  d: '.d
-            echom 'out: '.out
-        endif
-        unlet d e d2
     endif
+    if d!=#out
+        echom 'Failed '.i.':'
+        echom ' in: '.in
+        echom '  d: '.string(d)
+        echom 'out: '.string(out)
+    endif
+    if d2!=#out
+        echom 'Failed(2) '.i.':'
+        echom ' in: '.in
+        echom '  d: '.string(d)
+        echom 'out: '.string(out)
+    endif
+    unlet d e d2
     unlet in out
     let i+=1
 endfor