buf.compare(target[, targetStart[, targetEnd[, sourceStart[, sourceEnd]]]])
-
target
<Buffer> | <Uint8Array> 用于比较buf
的Buffer
或Uint8Array
。¥
target
<Buffer> | <Uint8Array> ABuffer
orUint8Array
with which to comparebuf
. -
targetStart
<integer>target
内开始比较的偏移量。默认值:0
。¥
targetStart
<integer> The offset withintarget
at which to begin comparison. Default:0
. -
targetEnd
<integer>target
中结束比较(不包括)的偏移量。默认值:target.length
。¥
targetEnd
<integer> The offset withintarget
at which to end comparison (not inclusive). Default:target.length
. -
sourceStart
<integer>buf
内开始比较的偏移量。默认值:0
。¥
sourceStart
<integer> The offset withinbuf
at which to begin comparison. Default:0
. -
sourceEnd
<integer>buf
中结束比较(不包括)的偏移量。默认值:buf.length
。¥
sourceEnd
<integer> The offset withinbuf
at which to end comparison (not inclusive). Default:buf.length
. -
返回:<integer>
¥Returns: <integer>
将 buf
与 target
进行比较并返回数字,该数字指示 buf
在排序顺序中是在 target
之前、之后还是与 target
相同。比较基于每个 Buffer
中的实际字节序列。
¥Compares buf
with target
and returns a number indicating whether buf
comes before, after, or is the same as target
in sort order.
Comparison is based on the actual sequence of bytes in each Buffer
.
-
如果
target
与buf
相同,则返回0
¥
0
is returned iftarget
is the same asbuf
-
如果排序时
target
应该在buf
之前,则返回1
。¥
1
is returned iftarget
should come beforebuf
when sorted. -
如果排序时
target
应该在buf
之后,则返回-1
。¥
-1
is returned iftarget
should come afterbuf
when sorted.
import { Buffer } from 'node:buffer';
const buf1 = Buffer.from('ABC');
const buf2 = Buffer.from('BCD');
const buf3 = Buffer.from('ABCD');
console.log(buf1.compare(buf1));
// Prints: 0
console.log(buf1.compare(buf2));
// Prints: -1
console.log(buf1.compare(buf3));
// Prints: -1
console.log(buf2.compare(buf1));
// Prints: 1
console.log(buf2.compare(buf3));
// Prints: 1
console.log([buf1, buf2, buf3].sort(Buffer.compare));
// Prints: [ <Buffer 41 42 43>, <Buffer 41 42 43 44>, <Buffer 42 43 44> ]
// (This result is equal to: [buf1, buf3, buf2].)
const { Buffer } = require('node:buffer');
const buf1 = Buffer.from('ABC');
const buf2 = Buffer.from('BCD');
const buf3 = Buffer.from('ABCD');
console.log(buf1.compare(buf1));
// Prints: 0
console.log(buf1.compare(buf2));
// Prints: -1
console.log(buf1.compare(buf3));
// Prints: -1
console.log(buf2.compare(buf1));
// Prints: 1
console.log(buf2.compare(buf3));
// Prints: 1
console.log([buf1, buf2, buf3].sort(Buffer.compare));
// Prints: [ <Buffer 41 42 43>, <Buffer 41 42 43 44>, <Buffer 42 43 44> ]
// (This result is equal to: [buf1, buf3, buf2].)
可选的 targetStart
、targetEnd
、sourceStart
和 sourceEnd
参数可用于分别将比较限制在 target
和 buf
内的特定范围内。
¥The optional targetStart
, targetEnd
, sourceStart
, and sourceEnd
arguments can be used to limit the comparison to specific ranges within target
and buf
respectively.
import { Buffer } from 'node:buffer';
const buf1 = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8, 9]);
const buf2 = Buffer.from([5, 6, 7, 8, 9, 1, 2, 3, 4]);
console.log(buf1.compare(buf2, 5, 9, 0, 4));
// Prints: 0
console.log(buf1.compare(buf2, 0, 6, 4));
// Prints: -1
console.log(buf1.compare(buf2, 5, 6, 5));
// Prints: 1
const { Buffer } = require('node:buffer');
const buf1 = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8, 9]);
const buf2 = Buffer.from([5, 6, 7, 8, 9, 1, 2, 3, 4]);
console.log(buf1.compare(buf2, 5, 9, 0, 4));
// Prints: 0
console.log(buf1.compare(buf2, 0, 6, 4));
// Prints: -1
console.log(buf1.compare(buf2, 5, 6, 5));
// Prints: 1
如果 targetStart < 0
、sourceStart < 0
、targetEnd > target.byteLength
或 sourceEnd > source.byteLength
,则抛出 ERR_OUT_OF_RANGE
。
¥ERR_OUT_OF_RANGE
is thrown if targetStart < 0
, sourceStart < 0
,
targetEnd > target.byteLength
, or sourceEnd > source.byteLength
.