-
-
Notifications
You must be signed in to change notification settings - Fork 677
Add Array#join and Array#toString + dtoa #275
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
Changes from 52 commits
2a4e550
9e6c7de
e269a0e
96125e2
1b46367
527a57d
ca2bdeb
9711df9
b918779
e9baecc
1488301
dbefc9b
a493326
225f55d
4643507
eb00cba
f423094
3620242
f86a863
adaf0d3
1ac0920
4c00ac8
dba7761
7a5ec23
ba78526
c68f26e
4dbae43
9086bc0
810a89a
2615084
68b2d55
1e927bd
8d56b54
3d42f0e
72a86e9
c4f1ff8
678de64
2c5d49b
749c34a
430d68d
4190233
013bc0d
b8dfcb2
65cb8a9
64187fb
6939844
a9213e3
1ddbd1e
99843d2
9a6b6f2
65a45b8
d2a6a11
36397d9
d487877
ca9fa43
2bb9514
0fbd6be
293f9a4
d24dfdc
4bb3da7
3882492
c0aaf1b
c74c477
8c41cde
f78ded0
e59d192
c545609
3d95859
4acb8cc
46957b1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,9 @@ import { | |
weakHeapSort | ||
} from "./internal/array"; | ||
|
||
import { itoa } from "./internal/itoa"; | ||
import { dtoa } from "./internal/dtoa"; | ||
|
||
export class Array<T> { | ||
|
||
/* @internal */ buffer_: ArrayBuffer; | ||
|
@@ -356,6 +359,66 @@ export class Array<T> { | |
} | ||
} | ||
|
||
join(separator: string = ","): string { | ||
var lastIndex = this.length_ - 1; | ||
if (lastIndex < 0) return ""; | ||
var result = ""; | ||
var value: T; | ||
var buffer = this.buffer_; | ||
var hasSeparator = separator.length != 0; | ||
if (value instanceof bool) { | ||
for (let i = 0; i < lastIndex; ++i) { | ||
result += loadUnsafe<T,T>(buffer, i) ? "true" : "false"; | ||
|
||
if (hasSeparator) result += separator; | ||
} | ||
result += loadUnsafe<T,T>(buffer, lastIndex) ? "true" : "false"; | ||
} else if (isInteger<T>()) { | ||
for (let i = 0; i < lastIndex; ++i) { | ||
result += itoa<T>(loadUnsafe<T,T>(buffer, i)); | ||
if (hasSeparator) result += separator; | ||
} | ||
result += itoa<T>(loadUnsafe<T,T>(buffer, lastIndex)); | ||
} else if (isFloat<T>()) { | ||
for (let i = 0; i < lastIndex; ++i) { | ||
result += dtoa(loadUnsafe<T,f64>(buffer, i)); | ||
if (hasSeparator) result += separator; | ||
} | ||
result += dtoa(loadUnsafe<T,f64>(buffer, lastIndex)); | ||
} else if (isString<T>()) { | ||
for (let i = 0; i < lastIndex; ++i) { | ||
value = loadUnsafe<T,T>(buffer, i); | ||
if (value) result += value; | ||
if (hasSeparator) result += separator; | ||
} | ||
value = loadUnsafe<T,T>(buffer, lastIndex); | ||
if (value) result += value; | ||
} else if (isArray<T>()) { | ||
for (let i = 0; i < lastIndex; ++i) { | ||
value = loadUnsafe<T,T>(buffer, i); | ||
if (value) result += value.join(separator); // tslint:disable-line:no-unsafe-any | ||
if (hasSeparator) result += separator; | ||
} | ||
value = loadUnsafe<T,T>(buffer, lastIndex); | ||
if (value) result += value.join(separator); // tslint:disable-line:no-unsafe-any | ||
} else if (isReference<T>()) { // References | ||
for (let i = 0; i < lastIndex; ++i) { | ||
value = loadUnsafe<T,T>(buffer, i); | ||
if (value) result += "[object Object]"; | ||
if (hasSeparator) result += separator; | ||
} | ||
value = loadUnsafe<T,T>(buffer, lastIndex); | ||
if (value) result += "[object Object]"; | ||
} else { | ||
assert(false); // Unsupported generic typename | ||
} | ||
return result; | ||
} | ||
|
||
@inline | ||
toString(): string { | ||
return this.join(); | ||
} | ||
|
||
private __gc(): void { | ||
var buffer = this.buffer_; | ||
__gc_mark(changetype<usize>(buffer)); // tslint:disable-line | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could maybe make this
internal/number
holding all the number compatibility codeThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, make sense
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think
parse<T>
should be also move tointernal/number
. WDYT?