简体   繁体   English

Javascript到Python的解压缩例程

[英]Decompression Routine In Javascript to Python

I'm trying to rewrite a javascript file decompression routine to python. 我正在尝试将javascript文件解压缩例程重写为python。 But I can't seem to get it right. 但我似乎无法正确解决。 The function always returns None on python. 该函数始终在python上返回None

Here is the original JS: 这是原始的JS:

var c = new Uint8Array(b),
    d = 0,
    e = new Uint32Array(4096),
    f = new Uint32Array(4096),
    g = 256,
    h = a.length,
    k = 0,
    l = 1,
    m = 0,
    n = 1;
c[d++] = a[0];
for (var r = 1;; r++) {
    n = r + (r >> 1);
    if (n + 1 >= h) break;
    var m = a[n + 1],
        n = a[n],
        p = r & 1 ? m << 4 | n >> 4 : (m & 15) << 8 | n;
    if (p < g)
        if (256 > p) m = d, n = 1, c[d++] = p;
        else
            for (var m = d, n = f[p], p = e[p], q = p + n; p < q;) c[d++] = c[p++];
    else if (p == g) {
        m = d;
        n = l + 1;
        p = k;
        for (q = k + l; p < q;) c[d++] = c[p++];
        c[d++] = c[k]
    } else break;
    e[g] = k;
    f[g++] = l + 1;
    k = m;
    l = n;
    g = 4096 <= g ? 256 : g
}
return d == b ? c : null

And here is my python implementation, what did I do wrong? 这是我的python实现,我做错了什么? ( Improved with @le_m answer but still returning None. ) 在@le_m答案中得到了改进,但仍未返回。

c = [0] * b
d = 0
e = [0] * 4096
f = [0] * 4096
g = 256
h = len(a)
k = 0
l = 1
m = 0
n = 1

c[d] = a[0]
d += 1

r = 1
while True:
    n = r + (r >> 1)
    if (n + 1) >= h:
        break
    m = a[n + 1]
    n = a[n]
    p = (m << 4 | n >> 4) if r & 1 else ((m & 15) << 8 | n)
    if (p < g):
        if (256 > p):
            m = d
            n = 1
            c[d] = p
            d += 1
        else:
            m = d
            n = f[p]
            p = e[p]
            q = p + n
            while p < q:
                c[d] = c[p]
                d += 1
                p += 1
    elif p == g:
        m = d
        n = 1 + 1
        p = k

        q = k + l
        while p < q:
            c[d] = c[p]
            d += 1
            p += 1
    else:
        break

    e[g] = k
    f[g] = l + 1
    g += 1
    k = m
    l = n
    g = 256 if 4096 <= g else g
    r += 1

Just from glancing at the code, I spot the following differences: 仅仅看一下代码,我就会发现以下差异:

  • new Uint8Array(b) translates to [0] * b as the typed array is initialized with zeros. new Uint8Array(b)转换为[0] * b因为类型化数组用零初始化。 Same holds for the other typed arrays. 其他类型的数组也是如此。
  • c[d++] = a[0] translates to c[d] = a[0] followed by d += 1 , not the other way round. c[d++] = a[0]转换为c[d] = a[0]后跟d += 1 ,而不是相反。 Same holds for the other post-increments, too. 其他后增量也一样。
  • The loop counter r += 1 should go to the end of the loop body or should be initialized with 0 instead. 循环计数器r += 1应该转到循环主体的末尾,或者应该用0初始化。

I recommend stepping through both implementations step by step with a debugger (your browser's developer console for JavaScript) and compare the variable values. 我建议使用调试器(您的浏览器的JavaScript开发者控制台)逐步完成这两种实现,并比较变量值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM