-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
184 additions
and
47 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,185 @@ | ||
import { Sink } from "../src/sink"; | ||
|
||
@inline export function serializeInteger<T extends number>(data: T, out: Sink | null = null): Sink { | ||
if (!out) out = Sink.withCapacity(0); | ||
return out.writeNumber(data); | ||
if (!out) out = Sink.withCapacity(10); | ||
return utoa32(data, out); | ||
} | ||
|
||
@inline export function utoa16(data: u32): string { | ||
const str = changetype<string>(__new(10, idof<string>())); | ||
const buf = changetype<usize>(str); | ||
@inline export function utoa32<T extends number>(data: T, out: Sink): Sink { | ||
let buf = changetype<usize>(out.buffer) + out.offset; | ||
const fix4_28 = (1 << 28) / 10000; | ||
let tmp = data * (fix4_28 + 1) - (data / 4); | ||
|
||
store<u16>(buf, 48 + (tmp >> 28)); | ||
tmp = (tmp & 0x0fffffff) * 10; | ||
|
||
store<u16>(buf, 48 + (tmp >> 28), 2); | ||
tmp = (tmp & 0x0fffffff) * 10; | ||
|
||
store<u16>(buf, 48 + (tmp >> 28), 4); | ||
tmp = (tmp & 0x0fffffff) * 10; | ||
|
||
store<u16>(buf, 48 + (tmp >> 28), 6); | ||
tmp = (tmp & 0x0fffffff) * 10; | ||
|
||
store<u16>(buf, 48 + (tmp >> 28), 8); | ||
return str | ||
} | ||
|
||
@inline function utoa32(buf: usize, data: u32): void { | ||
const low = data % 10000; | ||
const high = data / 10000; | ||
const fix4_28 = (1 << 28) / 10000; | ||
let tmp = data * (fix4_28 + 1) - (data / 4); | ||
|
||
store<u16>(buf, 48 + (tmp >> 28)); | ||
tmp = (tmp & 0x0fffffff) * 10; | ||
|
||
store<u16>(buf, 48 + (tmp >> 28), 2); | ||
tmp = (tmp & 0x0fffffff) * 10; | ||
|
||
store<u16>(buf, 48 + (tmp >> 28), 4); | ||
tmp = (tmp & 0x0fffffff) * 10; | ||
let tmpLow = low * (fix4_28 + 1) - (low >> 2); | ||
let tmpHigh = high * (fix4_28 + 1) - (high >> 2); | ||
|
||
store<u16>(buf, 48 + (tmp >> 28), 6); | ||
tmp = (tmp & 0x0fffffff) * 10; | ||
let fnd: bool = false; | ||
|
||
store<u16>(buf, 48 + (tmp >> 28), 8); | ||
{ | ||
const v = tmpHigh >> 28; | ||
if (!fnd) { | ||
if (v == 0) { | ||
buf -= 2; | ||
tmpHigh = (tmpHigh & 0x0fffffff) * 10; | ||
} else { | ||
fnd = true; | ||
store<u16>(buf, 48 + v); | ||
tmpHigh = (tmpHigh & 0x0fffffff) * 10; | ||
out.offset += 2; | ||
} | ||
} else { | ||
store<u16>(buf, 48 + v); | ||
tmpHigh = (tmpHigh & 0x0fffffff) * 10; | ||
out.offset += 2; | ||
} | ||
} | ||
{ | ||
const v = tmpHigh >> 28; | ||
if (!fnd) { | ||
if (v == 0) { | ||
buf -= 2; | ||
tmpHigh = (tmpHigh & 0x0fffffff) * 10; | ||
} else { | ||
fnd = true; | ||
store<u16>(buf, 48 + v, 2); | ||
tmpHigh = (tmpHigh & 0x0fffffff) * 10; | ||
out.offset += 2; | ||
} | ||
} else { | ||
store<u16>(buf, 48 + v, 2); | ||
tmpHigh = (tmpHigh & 0x0fffffff) * 10; | ||
out.offset += 2; | ||
} | ||
} | ||
{ | ||
const v = tmpHigh >> 28; | ||
if (!fnd) { | ||
if (v == 0) { | ||
buf -= 2; | ||
tmpHigh = (tmpHigh & 0x0fffffff) * 10; | ||
} else { | ||
fnd = true; | ||
store<u16>(buf, 48 + v, 4); | ||
tmpHigh = (tmpHigh & 0x0fffffff) * 10; | ||
out.offset += 2; | ||
} | ||
} else { | ||
store<u16>(buf, 48 + v, 4); | ||
tmpHigh = (tmpHigh & 0x0fffffff) * 10; | ||
out.offset += 2; | ||
} | ||
} | ||
{ | ||
const v = tmpHigh >> 28; | ||
if (!fnd) { | ||
if (v == 0) { | ||
buf -= 2; | ||
tmpHigh = (tmpHigh & 0x0fffffff) * 10; | ||
} else { | ||
fnd = true; | ||
store<u16>(buf, 48 + v, 6); | ||
tmpHigh = (tmpHigh & 0x0fffffff) * 10; | ||
out.offset += 2; | ||
} | ||
} else { | ||
store<u16>(buf, 48 + v, 6); | ||
tmpHigh = (tmpHigh & 0x0fffffff) * 10; | ||
out.offset += 2; | ||
} | ||
} | ||
{ | ||
const v = tmpHigh >> 28; | ||
if (!fnd) { | ||
if (v == 0) { | ||
buf -= 2; | ||
tmpHigh = (tmpHigh & 0x0fffffff) * 10; | ||
} else { | ||
fnd = true; | ||
store<u16>(buf, 48 + v, 8); | ||
tmpHigh = (tmpHigh & 0x0fffffff) * 10; | ||
out.offset += 2; | ||
} | ||
} else { | ||
store<u16>(buf, 48 + v, 8); | ||
tmpHigh = (tmpHigh & 0x0fffffff) * 10; | ||
out.offset += 2; | ||
} | ||
} | ||
{ | ||
const v = tmpLow >> 28; | ||
if (!fnd) { | ||
if (v == 0) { | ||
buf -= 2; | ||
tmpLow = (tmpLow & 0x0fffffff) * 10; | ||
} else { | ||
fnd = true; | ||
store<u16>(buf, 48 + v, 10); | ||
tmpLow = (tmpLow & 0x0fffffff) * 10; | ||
out.offset += 2; | ||
} | ||
} else { | ||
store<u16>(buf, 48 + v, 10); | ||
tmpLow = (tmpLow & 0x0fffffff) * 10; | ||
out.offset += 2; | ||
} | ||
} | ||
{ | ||
const v = tmpLow >> 28; | ||
if (!fnd) { | ||
if (v == 0) { | ||
buf -= 2; | ||
tmpLow = (tmpLow & 0x0fffffff) * 10; | ||
} else { | ||
fnd = true; | ||
store<u16>(buf, 48 + v, 12); | ||
tmpLow = (tmpLow & 0x0fffffff) * 10; | ||
out.offset += 2; | ||
} | ||
} else { | ||
store<u16>(buf, 48 + v, 12); | ||
tmpLow = (tmpLow & 0x0fffffff) * 10; | ||
out.offset += 2; | ||
} | ||
} | ||
{ | ||
const v = tmpLow >> 28; | ||
if (!fnd) { | ||
if (v == 0) { | ||
buf -= 2; | ||
tmpLow = (tmpLow & 0x0fffffff) * 10; | ||
} else { | ||
fnd = true; | ||
store<u16>(buf, 48 + v, 14); | ||
tmpLow = (tmpLow & 0x0fffffff) * 10; | ||
out.offset += 2; | ||
} | ||
} else { | ||
store<u16>(buf, 48 + v, 14); | ||
tmpLow = (tmpLow & 0x0fffffff) * 10; | ||
out.offset += 2; | ||
} | ||
} | ||
{ | ||
const v = tmpLow >> 28; | ||
if (!fnd) { | ||
if (v == 0) { | ||
buf -= 2; | ||
tmpLow = (tmpLow & 0x0fffffff) * 10; | ||
} else { | ||
fnd = true; | ||
store<u16>(buf, 48 + v, 16); | ||
tmpLow = (tmpLow & 0x0fffffff) * 10; | ||
out.offset += 2; | ||
} | ||
} else { | ||
store<u16>(buf, 48 + v, 16); | ||
tmpLow = (tmpLow & 0x0fffffff) * 10; | ||
out.offset += 2; | ||
} | ||
} | ||
store<u16>(buf, 48 + (tmpLow >> 28), 18); | ||
out.offset += 2; | ||
return out; | ||
} |
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