BytesThe bytes module provides helper functions to manipulate byte slices.
UsageThe following functions are exposed in mod.ts
.
indexOfFinds the first index of a binary pattern within a given binary array, or
returns -1 if the pattern is not present.
import { indexOf } from "https://deno.land/std@0.116.0/bytes/mod.ts" ;
indexOf (
new Uint8Array ( [ 1 , 2 , 0 , 1 , 2 , 0 , 1 , 2 , 0 , 1 , 3 ] ) ,
new Uint8Array ( [ 0 , 1 , 2 ] ) ,
) ;
indexOf (
new Uint8Array ( [ 1 , 2 , 0 , 1 , 2 , 0 , 1 , 2 , 0 , 1 , 3 ] ) ,
new Uint8Array ( [ 0 , 1 , 2 ] ) ,
3 ,
) ; lastIndexOfFinds the last index of a binary pattern within a given binary array, or returns
-1 if the pattern is not present.
import { lastIndexOf } from "https://deno.land/std@0.116.0/bytes/mod.ts" ;
lastIndexOf (
new Uint8Array ( [ 0 , 1 , 2 , 3 , 3 , 0 , 1 , 2 ] ) ,
new Uint8Array ( [ 0 , 1 , 2 ] ) ,
) ;
lastIndexOf (
new Uint8Array ( [ 0 , 1 , 2 , 3 , 3 , 0 , 1 , 2 ] ) ,
new Uint8Array ( [ 0 , 1 , 2 ] ) ,
3 ,
) ; equalsChecks whether two given binary arrays are equal to each other.
import { equals } from "https://deno.land/std@0.116.0/bytes/mod.ts" ;
equals ( new Uint8Array ( [ 0 , 1 , 2 , 3 ] ) , new Uint8Array ( [ 0 , 1 , 2 , 3 ] ) ) ;
equals ( new Uint8Array ( [ 0 , 1 , 2 , 3 ] ) , new Uint8Array ( [ 0 , 1 , 2 , 4 ] ) ) ; startsWithChecks whether a binary array starts with a binary array prefix.
import { startsWith } from "https://deno.land/std@0.116.0/bytes/mod.ts" ;
startsWith ( new Uint8Array ( [ 0 , 1 , 2 ] ) , new Uint8Array ( [ 0 , 1 ] ) ) ;
startsWith ( new Uint8Array ( [ 0 , 1 , 2 ] ) , new Uint8Array ( [ 1 , 2 ] ) ) ; endsWithChecks whether binary array ends with a binary array suffix.
import { endsWith } from "https://deno.land/std@0.116.0/bytes/mod.ts" ;
endsWith ( new Uint8Array ( [ 0 , 1 , 2 ] ) , new Uint8Array ( [ 0 , 1 ] ) ) ;
endsWith ( new Uint8Array ( [ 0 , 1 , 2 ] ) , new Uint8Array ( [ 1 , 2 ] ) ) ; repeatCreates a binary array consisting of multiple copies of a given binary array.
import { repeat } from "https://deno.land/std@0.116.0/bytes/mod.ts" ;
repeat ( new Uint8Array ( [ 1 ] ) , 3 ) ; concatConcatenates multiple binary arrays into a new one.
import { concat } from "https://deno.land/std@0.116.0/bytes/mod.ts" ;
concat ( new Uint8Array ( [ 1 , 2 ] ) , new Uint8Array ( [ 3 , 4 ] ) ) ;
concat (
new Uint8Array ( [ 1 , 2 ] ) ,
new Uint8Array ( [ 3 , 4 ] ) ,
new Uint8Array ( [ 5 , 6 ] ) ,
new Uint8Array ( [ 7 , 8 ] ) ,
) ; includesChecks that a given binary array includes a sequence corresponding to a pattern
array.
import { includes } from "https://deno.land/std@0.116.0/bytes/mod.ts" ;
includes (
new Uint8Array ( [ 1 , 2 , 0 , 1 , 2 , 0 , 2 , 1 , 3 ] ) ,
new Uint8Array ( [ 0 , 1 , 2 ] ) ,
) ;
includes (
new Uint8Array ( [ 1 , 2 , 0 , 1 , 2 , 0 , 2 , 1 , 3 ] ) ,
new Uint8Array ( [ 2 , 2 ] ) ,
) ; copyCopies bytes from one binary array to another one.
import { copy } from "https://deno.land/std@0.116.0/bytes/mod.ts" ;
const dest = new Uint8Array ( 4 ) ;
const src = Uint8Array . of ( 1 , 2 , 3 , 4 ) ;
const len = copy ( src, dest) ;