Commits

ZyX_I committed 3260979

Added @/base64 module

Comments (0)

Files changed (11)

         4.2. os resource                          |frawor-r-os|
         4.3. py resource                          |frawor-r-py|
         4.4. printtable resource                  |frawor-r-printtable|
+        4.5. base64 resource                      |frawor-r-base64|
     5. Options                                    |frawor-options|
         5.1. Mapping options                      |frawor-mapoptions|
     6. Type definitions                           |frawor-types|
              header. `hls' and `hhls' have the same relation to `hl' and `hhl' 
              as `aligns' has to `align'.
 
+------------------------------------------------------------------------------
+4.5. base64 resource                                         *frawor-r-base64*
+
+Resource `base64' is defined in plugin/frawor/base64. It is used for 
+encoding/decoding base64 data.
+
+                                                      *frawor-r-base64.decode*
+base64.decode :: b64str[, bytearray] -> str | bytearray
+        Decode base64 encoded string. If optional argument is 1, then function 
+        will return a |List| of numbers, each number representing one byte. 
+        Otherwise it will return a decoded string.
+
+base64.encode :: str | bytearray -> b64str            *frawor-r-base64.encode*
+        Encode string or a list of numbers each representing a single byte 
+        (thus restricted to be between 0 and 255 inclusive).
+
 ==============================================================================
 5. Options                                                    *frawor-options*
                                                              *g:fraworOptions*

plugin/frawor/base64.vim

+"▶1 Header
+scriptencoding utf-8
+execute frawor#Setup('0.0', {'@/resources': '0.0'}, 1)
+let s:F.base64={}
+"▶1 and           :: UInt, UInt → UInt
+function s:F.and(v1, v2)
+    let [v1, v2]=[a:v1, a:v2]
+    let list=[]
+    while v1 || v2
+        let [nv1, nv2]=[v1/2, v2/2]
+        call add(list, ((nv1*2!=v1)&&(nv2*2!=v2)))
+        let [v1, v2]=[nv1, nv2]
+    endwhile
+    let r=0
+    while !empty(list)
+        let r=(r*2) + remove(list, -1)
+    endwhile
+    return r
+endfunction
+"▶1 or            :: UInt, UInt → UInt
+function s:F.or(v1, v2)
+    let [v1, v2]=[a:v1, a:v2]
+    let list=[]
+    while v1 || v2
+        let [nv1, nv2]=[v1/2, v2/2]
+        call add(list, ((nv1*2!=v1)||(nv2*2!=v2)))
+        let [v1, v2]=[nv1, nv2]
+    endwhile
+    let r=0
+    while !empty(list)
+        let r=(r*2) + remove(list, -1)
+    endwhile
+    return r
+endfunction
+"▶1 base64.decode :: b64str[, bytearray::Bool] → str | bytearray
+let s:cd64=map(split("|$$$}rstuvwxyz{$$$$$$$>?@ABCDEFGHIJKLMNOPQRSTUVW$$$$$$XYZ[\\]^_`abcdefghijklmnopq",
+            \              '\v.@='),
+            \        'char2nr(v:val)')
+function s:F.base64.decode(str, ...)
+    let str=map(split(substitute(a:str, '[^a-zA-Z0-9+/]', '', 'g'), '\v.@='),
+                \'char2nr(v:val)')+[-1]
+    let in=repeat([0], 4)
+    let v=0
+    let len=0
+    let i=0
+    let bytearray=(a:0 && a:1)
+    if bytearray
+        let r=[]
+    else
+        let r=''
+    endif
+    while !empty(str)
+        let i=0
+        let len=0
+        while i<4 && !empty(str)
+            let v=0
+            while !empty(str) && v==0
+                let v=remove(str, 0)
+                let v=(((v<43)||(v>122))?(0):(s:cd64[v-43]))
+                if v
+                    let v=((v==36)?(0):(v-61))
+                endif
+            endwhile
+            if !empty(str)
+                let len+=1
+                if v
+                    let in[i]=v-1
+                endif
+            else
+                let in[i]=0
+            endif
+            let i+=1
+        endwhile
+        if len
+            let out=[    s:F.or(        in[0]*4,         in[1]/16),
+                        \s:F.or(        in[1]*16,        in[2]/4),
+                        \s:F.or(s:F.and(in[2]*64, 0xC0), in[3])]
+            call map(out, 's:F.and(v:val, 0xFF)')
+            if bytearray
+                let r+=out[:(len-2)]
+            else
+                let r.=join(map(out[:(len-2)],
+                            \   'eval(printf(''"\x%02x"'', v:val))'),
+                            \'')
+            endif
+        endif
+    endwhile
+    return r
+endfunction
+"▶1 base64.encode :: str | bytearray → b64str
+let s:eqsigncode=char2nr('=')
+let s:cb64=map(split("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",
+            \        '\v.@='), 'char2nr(v:val)')
+function s:F.base64.encode(str)
+    let r=''
+    let bytearray=(type(a:str)==type([]))
+    let in=repeat([0], 3)
+    let idx=0
+    let slen=len(a:str)
+    while idx<slen
+        let len=0
+        let i=0
+        while i<3
+            if idx<slen
+                let cur=a:str[idx]
+                if !bytearray
+                    let cur=char2nr(cur)
+                endif
+                let in[i]=cur
+                let len+=1
+                let idx+=1
+            else
+                let in[i]=0
+            endif
+            let i+=1
+        endwhile
+        if len
+            let out=[    s:cb64[in[0]/4],
+                        \s:cb64[s:F.or(((s:F.and(in[0], 0x03))*16),
+                        \              ((s:F.and(in[1], 0xF0))/16))],
+                        \((len>1)?
+                        \   (s:cb64[s:F.or(s:F.and(in[1], 0x0F)*4,
+                        \                  s:F.and(in[2], 0xC0)/64)]):
+                        \   (s:eqsigncode)),
+                        \((len>2)?
+                        \   (s:cb64[s:F.and(in[2], 0x3F)]):
+                        \   (s:eqsigncode))]
+            let r.=join(map(copy(out), 'eval(printf(''"\x%02x"'', v:val))'), '')
+        endif
+    endwhile
+    return r
+endfunction
+"▶1 post resource
+call s:_f.postresource('base64', s:F.base64)
+"▶1
+call frawor#Lockvar(s:, '')
+" vim: fmr=▶,▲ sw=4 ts=4 sts=4 et tw=80
+:let &rtp.=",".escape($TESTDIR, ',\').'/rtp'
+:let g:testfile="plugin/".g:curtest.".vim"
+:source test.vim
+: Testing base64. Test should not output anything

test/global-functions-reload.ok

 1      echo 1
    endfunction
 
-   function <SNR>26_testFunction()
+   function <SNR>27_testFunction()
 1      echo 2
    endfunction
 
 1      echo 1
    endfunction
 
-   function <SNR>26_testFunction()
+   function <SNR>27_testFunction()
 1      echo 2
    endfunction

test/ignoredeps-feature.ok

 load: plugin/frawor/fwc/intfuncs
 register: plugin/frawor/fwc/topconstructs
 load: plugin/frawor/fwc/topconstructs
+register: plugin/frawor/base64
+load: plugin/frawor/base64
 register: plugin/frawor/python
 load: plugin/frawor/python
 register: plugin/frawor/functions

test/reload-frawor.ok

 load: plugin/frawor/fwc/intfuncs
 register: plugin/frawor/fwc/topconstructs
 load: plugin/frawor/fwc/topconstructs
+register: plugin/frawor/base64
+load: plugin/frawor/base64
 register: plugin/frawor/python
 load: plugin/frawor/python
 register: plugin/frawor/functions
 register: plugin/writefile-feature
 load: plugin/writefile-feature
 unloadpre: autoload/frawor
+unloadpre: plugin/frawor/base64
 unloadpre: plugin/frawor/commands
 unloadpre: plugin/frawor/functions
 unloadpre: plugin/frawor/lua
 unloadpre: plugin/frawor/resources
 unloadpre: plugin/frawor
 unload: autoload/frawor
+unload: plugin/frawor/base64
 unload: plugin/frawor/commands
 unload: plugin/frawor/functions
 unload: plugin/frawor/lua
 register: plugin/frawor/functions
 load: plugin/frawor/functions
 register: plugin/frawor/commands
-load: plugin/frawor/commands
+load: plugin/frawor/commands
+register: plugin/frawor/base64
+load: plugin/frawor/base64

test/reload-ignoredeps.ok

 load: plugin/frawor/fwc/intfuncs
 register: plugin/frawor/fwc/topconstructs
 load: plugin/frawor/fwc/topconstructs
+register: plugin/frawor/base64
+load: plugin/frawor/base64
 register: plugin/frawor/python
 load: plugin/frawor/python
 register: plugin/frawor/functions
 load: plugin/frawor/fwc/intfuncs
 register: plugin/frawor/fwc/topconstructs
 load: plugin/frawor/fwc/topconstructs
+register: plugin/frawor/base64
+load: plugin/frawor/base64
 register: plugin/frawor/python
 load: plugin/frawor/python
 register: plugin/frawor/functions

test/reload-writefile.ok

 load: plugin/frawor/fwc/intfuncs
 register: plugin/frawor/fwc/topconstructs
 load: plugin/frawor/fwc/topconstructs
+register: plugin/frawor/base64
+load: plugin/frawor/base64
 register: plugin/frawor/python
 load: plugin/frawor/python
 register: plugin/frawor/functions
 load: plugin/frawor/fwc/intfuncs
 register: plugin/frawor/fwc/topconstructs
 load: plugin/frawor/fwc/topconstructs
+register: plugin/frawor/base64
+load: plugin/frawor/base64
 register: plugin/frawor/python
 load: plugin/frawor/python
 register: plugin/frawor/functions

test/rtp/plugin/base64.vim

+execute frawor#Setup('0.0', {'@/base64': '0.0'}, 1)
+W: Testing base64. Test should not output anything
+let tests=[
+            \['', ''],
+            \['QQ==', 'A'],
+            \['QUJ=', 'AB'],
+            \['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)]
+        \]
+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)
+        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
+    unlet in out
+    let i+=1
+endfor
+unlet i
+let base64=s:_r.base64

test/unload-self.ok

 load: plugin/frawor/fwc/topconstructs
 register: plugin/unload-self-feature
 load: plugin/unload-self-feature
+register: plugin/frawor/base64
+load: plugin/frawor/base64
 register: plugin/frawor/python
 load: plugin/frawor/python
 register: plugin/frawor/functions
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.