1 | /*
|
---|
2 | * This is a AssemblyScript port of the original Java version, which was written by
|
---|
3 | * Gil Tene as described in
|
---|
4 | * https://github.com/HdrHistogram/HdrHistogram
|
---|
5 | * and released to the public domain, as explained at
|
---|
6 | * http://creativecommons.org/publicdomain/zero/1.0/
|
---|
7 | */
|
---|
8 |
|
---|
9 | const leftPadding = (size: i32, input: string): string => {
|
---|
10 | if (input.length < size) {
|
---|
11 | input.padStart(size - input.length);
|
---|
12 | return " ".repeat(size - input.length) + input;
|
---|
13 | }
|
---|
14 | return input;
|
---|
15 | };
|
---|
16 |
|
---|
17 | export const integerFormatter = (size: i32, integer: u64): string => {
|
---|
18 | return leftPadding(size, integer.toString());
|
---|
19 | };
|
---|
20 |
|
---|
21 | export class IntegerFormatter {
|
---|
22 | constructor(private size: i32) {}
|
---|
23 |
|
---|
24 | format(integer: u64): string {
|
---|
25 | return leftPadding(this.size, integer.toString());
|
---|
26 | }
|
---|
27 | }
|
---|
28 |
|
---|
29 | export class FloatFormatter {
|
---|
30 | constructor(private size: i32, private fractionDigits: i32) {}
|
---|
31 |
|
---|
32 | format(float: f64): string {
|
---|
33 | const intergerPart = <u64>Math.floor(float);
|
---|
34 | const digits = Math.pow(10, this.fractionDigits);
|
---|
35 | const floatPart = <u64>(
|
---|
36 | Math.round(float * digits - <f64>intergerPart * digits)
|
---|
37 | );
|
---|
38 | let floatPartString = floatPart.toString();
|
---|
39 | if (floatPartString.length < this.fractionDigits) {
|
---|
40 | floatPartString += "0".repeat(
|
---|
41 | this.fractionDigits - floatPartString.length
|
---|
42 | );
|
---|
43 | }
|
---|
44 |
|
---|
45 | let result = intergerPart.toString() + "." + floatPartString;
|
---|
46 | if (result.length < this.size) {
|
---|
47 | result = " ".repeat(this.size - result.length) + result;
|
---|
48 | }
|
---|
49 | return result;
|
---|
50 | }
|
---|
51 | }
|
---|