Skip to content

Implement different bases (radix) for int#toString #1284

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 28 commits into from
May 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
9cc521e
initial
MaxGraey May 17, 2020
22f52d7
remove generic itoa
MaxGraey May 18, 2020
9f700ce
implement special cases for radix=16
MaxGraey May 18, 2020
ab8615b
move sign case appendix to the end
MaxGraey May 18, 2020
5e11f0b
don't care about special case for radix=2
MaxGraey May 18, 2020
24d426a
rename _core/_lut for decimals ro _code_dec_/_dec_lut for consistency
MaxGraey May 18, 2020
6099b35
fixes and first build
MaxGraey May 18, 2020
44e516a
implement general (any) radix itoa kernel
MaxGraey May 18, 2020
0643574
improve ulogBase a little
MaxGraey May 19, 2020
0b2382f
add fast path for ulogBase
MaxGraey May 19, 2020
4e8e897
add tests for 32-bit radix=16
MaxGraey May 19, 2020
689c834
add tests for 64-bit radix=16
MaxGraey May 19, 2020
dc5ca6f
add tests for 32/64-bit radix=2
MaxGraey May 19, 2020
4c4870f
use different approach for ulogBase
MaxGraey May 19, 2020
220f68e
update fixtures
MaxGraey May 19, 2020
80b1790
speedup ulogBase when base is POT
MaxGraey May 19, 2020
dc82942
use radix=0 defaults when it's unnecessary
MaxGraey May 19, 2020
14d761e
add test tests for random radix
MaxGraey May 19, 2020
a7a4b70
rebuild
MaxGraey May 19, 2020
a52100d
speedup ulogBase even more
MaxGraey May 20, 2020
30c0652
more
MaxGraey May 20, 2020
1d4d7a0
cleanups
MaxGraey May 20, 2020
60de09f
remove early return as to too rarely special case
MaxGraey May 20, 2020
184be02
fix zero extending for i32 -> u64
MaxGraey May 20, 2020
fafc692
update rest fixtures
MaxGraey May 20, 2020
e645b8c
refactor
MaxGraey May 21, 2020
4a51376
refactor according review
MaxGraey May 21, 2020
e348d79
remove unnecessary changetype
MaxGraey May 21, 2020
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
67 changes: 31 additions & 36 deletions std/assembly/number.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { itoa, dtoa } from "./util/number";
import { itoa32, utoa32, itoa64, utoa64, dtoa } from "./util/number";
import { strtol } from "./util/string";

// @ts-ignore: decorator
Expand Down Expand Up @@ -32,9 +32,8 @@ export abstract class I8 {
return <i8>strtol<i32>(value, radix);
}

toString(this: i8): String {
// TODO: radix
return itoa(this);
toString(this: i8, radix: i32 = 10): String {
return itoa32(this, radix);
}
}

Expand All @@ -53,9 +52,8 @@ export abstract class I16 {
return <i16>strtol<i32>(value, radix);
}

toString(this: i16): String {
// TODO: radix
return itoa(this);
toString(this: i16, radix: i32 = 10): String {
return itoa32(this, radix);
}
}

Expand All @@ -74,9 +72,8 @@ export abstract class I32 {
return <i32>strtol<i32>(value, radix);
}

toString(this: i32): String {
// TODO: radix
return itoa(this);
toString(this: i32, radix: i32 = 10): String {
return itoa32(this, radix);
}
}

Expand All @@ -95,9 +92,8 @@ export abstract class I64 {
return strtol<i64>(value, radix);
}

toString(this: i64): String {
// TODO: radix
return itoa(this);
toString(this: i64, radix: i32 = 10): String {
return itoa64(this, radix);
}
}

Expand All @@ -116,9 +112,12 @@ export abstract class Isize {
return <isize>strtol<i64>(value, radix);
}

toString(this: isize): String {
// TODO: radix
return itoa(this);
toString(this: isize, radix: i32 = 10): String {
if (sizeof<isize>() == 4) {
return itoa32(this, radix);
} else {
return itoa64(this, radix);
}
}
}

Expand All @@ -137,9 +136,8 @@ export abstract class U8 {
return <u8>strtol<i32>(value, radix);
}

toString(this: u8): String {
// TODO: radix
return itoa(this);
toString(this: u8, radix: i32 = 10): String {
return utoa32(this, radix);
}
}

Expand All @@ -158,9 +156,8 @@ export abstract class U16 {
return <u16>strtol<i32>(value, radix);
}

toString(this: u16): String {
// TODO: radix
return itoa(this);
toString(this: u16, radix: i32 = 10): String {
return utoa32(this, radix);
}
}

Expand All @@ -179,9 +176,8 @@ export abstract class U32 {
return <u32>strtol<i32>(value, radix);
}

toString(this: u32): String {
// TODO: radix
return itoa(this);
toString(this: u32, radix: i32 = 10): String {
return utoa32(this, radix);
}
}

Expand All @@ -200,9 +196,8 @@ export abstract class U64 {
return <u64>strtol<i64>(value, radix);
}

toString(this: u64): String {
// TODO: radix
return itoa(this);
toString(this: u64, radix: i32 = 10): String {
return utoa64(this, radix);
}
}

Expand All @@ -221,9 +216,12 @@ export abstract class Usize {
return <usize>strtol<i64>(value, radix);
}

toString(this: usize): String {
// TODO: radix
return itoa(this);
toString(this: usize, radix: i32 = 10): String {
if (sizeof<isize>() == 4) {
return utoa32(this, radix);
} else {
return utoa64(this, radix);
}
}
}

Expand All @@ -238,8 +236,7 @@ export abstract class Bool {
@lazy
static readonly MAX_VALUE: bool = bool.MAX_VALUE;

toString(this: bool): String {
// TODO: radix?
toString(this: bool, radix: i32 = 0): String {
return this ? "true" : "false";
}
}
Expand Down Expand Up @@ -305,8 +302,7 @@ export abstract class F32 {
return <f32>parseFloat(value);
}

toString(this: f32): String {
// TODO: radix
toString(this: f32, radix: i32 = 0): String {
return dtoa(this);
}
}
Expand Down Expand Up @@ -371,7 +367,6 @@ export abstract class F64 {
}

toString(this: f64, radix: i32 = 0): String {
// TODO: radix
return dtoa(this);
}
}
Expand Down
Loading