-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Changed size estimation api, added the ability to skip a number of by…
…tes in IO.
- Loading branch information
Showing
18 changed files
with
264 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export { ISerialInput, ISerialOutput } from './types'; | ||
export { ISerialInput, ISerialOutput, IMeasurer } from './types'; | ||
export { BufferWriter } from './bufferWriter'; | ||
export { BufferReader } from './bufferReader'; | ||
export { isBigEndian } from '../util'; | ||
export { Measurer } from './measurer'; | ||
export { isBigEndian } from '../util'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { IMeasurer } from './types'; | ||
|
||
class UnboundedMeasurer implements IMeasurer { | ||
size = NaN; | ||
unbounded: IMeasurer = this; | ||
isUnbounded = true; | ||
|
||
add(): IMeasurer { | ||
return this; | ||
} | ||
|
||
fork(): IMeasurer { | ||
return this; | ||
} | ||
} | ||
|
||
const unboundedMeasurer = new UnboundedMeasurer(); | ||
|
||
export class Measurer implements IMeasurer { | ||
size = 0; | ||
unbounded: IMeasurer = unboundedMeasurer; | ||
isUnbounded = false; | ||
|
||
add(bytes: number): IMeasurer { | ||
this.size += bytes; | ||
return this; | ||
} | ||
|
||
fork() { | ||
const forked = new Measurer(); | ||
forked.size = this.size; | ||
return forked; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,39 @@ | ||
import type { ISerialInput, ISerialOutput } from '../io'; | ||
import type { IMeasurer, ISerialInput, ISerialOutput } from '../io'; | ||
import { TypedBinaryError } from '../error'; | ||
import { Schema } from './types'; | ||
|
||
export class CharsSchema extends Schema<string> { | ||
constructor(public readonly length: number) { | ||
super(); | ||
} | ||
constructor(public readonly length: number) { | ||
super(); | ||
} | ||
|
||
resolve(): void { /* Nothing to resolve */ } | ||
resolve(): void { | ||
/* Nothing to resolve */ | ||
} | ||
|
||
write(output: ISerialOutput, value: string): void { | ||
if (value.length !== this.length) { | ||
throw new TypedBinaryError(`Expected char-string of length ${this.length}, got ${value.length}`); | ||
} | ||
|
||
for (let i = 0; i < value.length; ++i) { | ||
output.writeByte(value.charCodeAt(i)); | ||
} | ||
write(output: ISerialOutput, value: string): void { | ||
if (value.length !== this.length) { | ||
throw new TypedBinaryError( | ||
`Expected char-string of length ${this.length}, got ${value.length}`, | ||
); | ||
} | ||
|
||
read(input: ISerialInput): string { | ||
let content = ''; | ||
|
||
for (let i = 0; i < this.length; ++i) { | ||
content += String.fromCharCode(input.readByte()); | ||
} | ||
|
||
return content; | ||
for (let i = 0; i < value.length; ++i) { | ||
output.writeByte(value.charCodeAt(i)); | ||
} | ||
} | ||
|
||
read(input: ISerialInput): string { | ||
let content = ''; | ||
|
||
sizeOf(): number { | ||
return this.length; | ||
for (let i = 0; i < this.length; ++i) { | ||
content += String.fromCharCode(input.readByte()); | ||
} | ||
|
||
return content; | ||
} | ||
|
||
measure(_: string, measurer: IMeasurer): IMeasurer { | ||
return measurer.add(this.length); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.