forked from electricessence/TypeScript.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringBuilder.ts
More file actions
132 lines (109 loc) · 2.82 KB
/
Copy pathStringBuilder.ts
File metadata and controls
132 lines (109 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
* @author electricessence / https://github.com/electricessence/
* .NET Reference: http://referencesource.microsoft.com/#mscorlib/system/text/StringBuilder.cs
* Licensing: MIT https://github.com/electricessence/TypeScript.NET/blob/master/LICENSE.md
*/
///<reference path="../Disposable/IDisposable.d.ts"/>
import Type from '../Types';
import LinkedList from '../Collections/LinkedList';
/*****************************
* IMPORTANT NOTES ABOUT PERFORMANCE:
* http://jsperf.com/string-concatenation-looped
* http://jsperf.com/adding-strings-to-an-array
* http://jsperf.com/string-concatenation-versus-array-operations-with-join
*
* It is clearly inefficient to use a StringBuilder or LinkedList to build a string when you have a small set of string portions.
* StringBuilder will really show it's benefit likely somewhere above 1000 items.
*****************************/
export default
class StringBuilder implements IDisposable
// Adding IDisposable allows for use with System.using();
// ... and since this may end up being a large array container, might be a good idea to allow for flexible cleanup.
{
//noinspection JSMismatchedCollectionQueryUpdate
private _partArray:any[];
private _latest:string; // AKA persistentString
constructor(...initial:any[])
{
var _ = this;
_._latest = null;
_._partArray = [];
_.appendThese(initial);
}
private appendSingle(item:any):void
{
if(item!==null && item!==undefined) {
var _ = this;
_._latest = null;
switch(typeof item) {
case Type.OBJECT:
case Type.FUNCTION:
item = item.toString();
break;
}
_._partArray.push(item); // Other primitive types can keep their format since a number or boolean is a smaller footprint than a string.
}
}
appendThese(items:any[]):StringBuilder
{
var _ = this;
items.forEach(s=> _.appendSingle(s));
return _;
}
append(...items:any[]):StringBuilder
{
this.appendThese(items);
return this;
}
appendLine(...items:any[]):StringBuilder
{
this.appendLines(items);
return this;
}
appendLines(items:any[]):StringBuilder
{
var _ = this;
items.forEach(
i=>
{
if(i!==null && i!==undefined) {
_.appendSingle(i);
_._partArray.push("\r\n");
}
}
);
return _;
}
/** /// These methods can only efficiently be added if not using a single array.
insert(index: number, value: string, count: number = 1): StringBuilder
{
}
remove(startIndex:number, length:number): StringBuilder
{
}
/**/
get isEmpty()
{
return this._partArray.length===0;
}
toString()
{
var latest = this._latest;
if(!latest===null)
this._latest = latest = this._partArray.join();
return latest;
}
join(delimiter:string):string
{
return this._partArray.join(delimiter);
}
clear():void
{
this._partArray.length = 0;
this._latest = null;
}
dispose():void
{
this.clear();
}
}