Skip to content

Commit 0457b11

Browse files
authored
Implement Number#toString radix argument (#1284)
1 parent 7702f5d commit 0457b11

30 files changed

+17607
-9081
lines changed

std/assembly/number.ts

Lines changed: 31 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { itoa, dtoa } from "./util/number";
1+
import { itoa32, utoa32, itoa64, utoa64, dtoa } from "./util/number";
22
import { strtol } from "./util/string";
33

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

35-
toString(this: i8): String {
36-
// TODO: radix
37-
return itoa(this);
35+
toString(this: i8, radix: i32 = 10): String {
36+
return itoa32(this, radix);
3837
}
3938
}
4039

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

56-
toString(this: i16): String {
57-
// TODO: radix
58-
return itoa(this);
55+
toString(this: i16, radix: i32 = 10): String {
56+
return itoa32(this, radix);
5957
}
6058
}
6159

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

77-
toString(this: i32): String {
78-
// TODO: radix
79-
return itoa(this);
75+
toString(this: i32, radix: i32 = 10): String {
76+
return itoa32(this, radix);
8077
}
8178
}
8279

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

98-
toString(this: i64): String {
99-
// TODO: radix
100-
return itoa(this);
95+
toString(this: i64, radix: i32 = 10): String {
96+
return itoa64(this, radix);
10197
}
10298
}
10399

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

119-
toString(this: isize): String {
120-
// TODO: radix
121-
return itoa(this);
115+
toString(this: isize, radix: i32 = 10): String {
116+
if (sizeof<isize>() == 4) {
117+
return itoa32(this, radix);
118+
} else {
119+
return itoa64(this, radix);
120+
}
122121
}
123122
}
124123

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

140-
toString(this: u8): String {
141-
// TODO: radix
142-
return itoa(this);
139+
toString(this: u8, radix: i32 = 10): String {
140+
return utoa32(this, radix);
143141
}
144142
}
145143

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

161-
toString(this: u16): String {
162-
// TODO: radix
163-
return itoa(this);
159+
toString(this: u16, radix: i32 = 10): String {
160+
return utoa32(this, radix);
164161
}
165162
}
166163

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

182-
toString(this: u32): String {
183-
// TODO: radix
184-
return itoa(this);
179+
toString(this: u32, radix: i32 = 10): String {
180+
return utoa32(this, radix);
185181
}
186182
}
187183

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

203-
toString(this: u64): String {
204-
// TODO: radix
205-
return itoa(this);
199+
toString(this: u64, radix: i32 = 10): String {
200+
return utoa64(this, radix);
206201
}
207202
}
208203

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

224-
toString(this: usize): String {
225-
// TODO: radix
226-
return itoa(this);
219+
toString(this: usize, radix: i32 = 10): String {
220+
if (sizeof<isize>() == 4) {
221+
return utoa32(this, radix);
222+
} else {
223+
return utoa64(this, radix);
224+
}
227225
}
228226
}
229227

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

241-
toString(this: bool): String {
242-
// TODO: radix?
239+
toString(this: bool, radix: i32 = 0): String {
243240
return this ? "true" : "false";
244241
}
245242
}
@@ -305,8 +302,7 @@ export abstract class F32 {
305302
return <f32>parseFloat(value);
306303
}
307304

308-
toString(this: f32): String {
309-
// TODO: radix
305+
toString(this: f32, radix: i32 = 0): String {
310306
return dtoa(this);
311307
}
312308
}
@@ -371,7 +367,6 @@ export abstract class F64 {
371367
}
372368

373369
toString(this: f64, radix: i32 = 0): String {
374-
// TODO: radix
375370
return dtoa(this);
376371
}
377372
}

0 commit comments

Comments
 (0)