如何在ArrayBuffer,DataView和TypedArray中测试是否相等 [英] How to test for equality in ArrayBuffer, DataView, and TypedArray

查看:183
本文介绍了如何在ArrayBuffer,DataView和TypedArray中测试是否相等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以测试两个JavaScript ArrayBuffer是否相等?我想为消息编写方法编写测试.我发现的唯一方法是将ArrayBuffer转换为字符串,然后进行比较.我错过了什么吗?

Is there a way how to test if two JavaScript ArrayBuffers are equal? I would like to write test for message composing method. The only way I found is to convert the ArrayBuffer to string and then compare. Did I miss something?

以下代码给出的是假的,即使我认为这应该是真的:

Following code is giving false, even if I think that it should be true:

(function() {
    'use strict';

    /* Fill buffer with data of Verse header and user_auth
     * command */
    var buf_pos = 0;
    var name_len = 6
    var message_len = 4 + 1 + 1 + 1 + name_len + 1;

    var buf = new ArrayBuffer(message_len);
    var view = new DataView(buf);
    /* Verse header starts with version */
    view.setUint8(buf_pos, 1 << 4); /* First 4 bits are reserved for version of protocol */
    buf_pos += 2;
    /* The lenght of the message */
    view.setUint16(buf_pos, message_len);
    buf_pos += 2;

    buf_pos = 0;
    var buf2 = new ArrayBuffer(message_len);
    var view2 = new DataView(buf);
    /* Verse header starts with version */
    view2.setUint8(buf_pos, 1 << 4); /* First 4 bits are reserved for version of protocol */
    buf_pos += 2;
    /* The lenght of the message */
    view2.setUint16(buf_pos, message_len);
    buf_pos += 2;


    if(buf == buf2){
        console.log('true');
    }
    else{
        console.log('false');
    }


}());

如果我尝试比较view和view2,那又是错误的.

If I try to compare view and view2 it's false again.

推荐答案

您不能使用=====在JavaScript中直接比较两个对象.
这些运算符将仅检查引用的相等性(即表达式是否引用相同的对象).

You cannot compare two objects directly in JavaScript using == or ===.
These operators will only check the equality of references (i.e. if expressions reference the same object).

但是,您可以使用DataViewArrayView对象来检索ArrayBuffer对象特定部分的值并进行检查.

You can, however, use DataView or ArrayView objects to retrieve values of specific parts of ArrayBuffer objects and check them.

如果要检查标题,请执行以下操作:

If you want to check headers:

if (  view1.getUint8 (0) == view2.getUint8 (0)
   && view1.getUint16(2) == view2.getUint16(2)) ...

或者如果您要检查缓冲区的全局性:

Or if you want to check the globality of your buffers:

function equal (buf1, buf2)
{
    if (buf1.byteLength != buf2.byteLength) return false;
    var dv1 = new Int8Array(buf1);
    var dv2 = new Int8Array(buf2);
    for (var i = 0 ; i != buf1.byteLength ; i++)
    {
        if (dv1[i] != dv2[i]) return false;
    }
    return true;
}

如果要基于ArrayBuffer实现复杂的数据结构,建议创建自己的类,否则每次移动时都必须诉诸繁琐的原始DataView/ArrayView实例一根火柴进出建筑物.

If you want to implement a complex data structure based on ArrayBuffer, I suggest creating your own class, or else you will have to resort to cumbersome raw DataView / ArrayView instances each time you will want to move a matchstick in and out of the structure.

这篇关于如何在ArrayBuffer,DataView和TypedArray中测试是否相等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆