Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/bytes-iter/bytes-iter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,11 @@ describe('BytesIter', () => {
expect(iter.nextByte()).toEqual('0xad')
expect(iter.nextBytes(2)).toEqual('0xbeef')
})

it('should iterate in reverse ', () => {
const iter = BytesIter.String('0xdeadbeef')
expect(iter.nextByte(BytesIter.SIDE.Back)).toEqual('0xef')
expect(iter.nextByte(BytesIter.SIDE.Back)).toEqual('0xbe')
expect(iter.nextBytes(2, BytesIter.SIDE.Back)).toEqual('0xdead')
})
})
49 changes: 30 additions & 19 deletions src/bytes-iter/bytes-iter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import assert from 'assert'
import {isHexBytes} from '../validations'
import {add0x} from '../utils'

enum Side {
Front,
Back
}

/**
* Class to iterate through bytes string by parsing individual bytes
*
Expand All @@ -12,6 +17,8 @@ import {add0x} from '../utils'
* iter.nextBytes(2) == BigInt(0xbeef)
*/
export class BytesIter<T> {
public static SIDE = Side

private bytes: string

private constructor(
Expand Down Expand Up @@ -42,11 +49,11 @@ export class BytesIter<T> {
return this.bytes.length === 0
}

public nextByte(): T {
return this.nextBytes(1)
public nextByte(side = Side.Front): T {
return this.nextBytes(1, side)
}

public nextBytes(n: number): T {
public nextBytes(n: number, side = Side.Front): T {
const cnt = n * 2

if (this.bytes.length < cnt) {
Expand All @@ -55,38 +62,42 @@ export class BytesIter<T> {
)
}

const bytes = this.bytes.slice(0, cnt)
const isFront = side === Side.Front

const bytes = isFront
? this.bytes.slice(0, cnt)
: this.bytes.slice(-cnt)

this.bytes = this.bytes.slice(cnt)
this.bytes = isFront ? this.bytes.slice(cnt) : this.bytes.slice(0, -cnt)

return this.ResultType(add0x(bytes))
}

public nextUint8(): T {
return this.nextByte()
public nextUint8(side = Side.Front): T {
return this.nextByte(side)
}

public nextUint16(): T {
return this.nextBytes(2)
public nextUint16(side = Side.Front): T {
return this.nextBytes(2, side)
}

public nextUint24(): T {
return this.nextBytes(3)
public nextUint24(side = Side.Front): T {
return this.nextBytes(3, side)
}

public nextUint32(): T {
return this.nextBytes(4)
public nextUint32(side = Side.Front): T {
return this.nextBytes(4, side)
}

public nextUint128(): T {
return this.nextBytes(16)
public nextUint128(side = Side.Front): T {
return this.nextBytes(16, side)
}

public nextUint160(): T {
return this.nextBytes(20)
public nextUint160(side = Side.Front): T {
return this.nextBytes(20, side)
}

public nextUint256(): T {
return this.nextBytes(32)
public nextUint256(side = Side.Front): T {
return this.nextBytes(32, side)
}
}
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"ES2021"
],
"target": "ES2021",
"removeComments": false
},
"include": ["./src"]
}