1 | ;
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.StringBuilder = void 0;
|
4 | /**
|
5 | * This class allows a large text string to be constructed incrementally by appending small chunks. The final
|
6 | * string can be obtained by calling StringBuilder.toString().
|
7 | *
|
8 | * @remarks
|
9 | * A naive approach might use the `+=` operator to append strings: This would have the downside of copying
|
10 | * the entire string each time a chunk is appended, resulting in `O(n^2)` bytes of memory being allocated
|
11 | * (and later freed by the garbage collector), and many of the allocations could be very large objects.
|
12 | * StringBuilder avoids this overhead by accumulating the chunks in an array, and efficiently joining them
|
13 | * when `getText()` is finally called.
|
14 | */
|
15 | var StringBuilder = /** @class */ (function () {
|
16 | function StringBuilder() {
|
17 | this._chunks = [];
|
18 | }
|
19 | /** {@inheritdoc IStringBuilder.append} */
|
20 | StringBuilder.prototype.append = function (text) {
|
21 | this._chunks.push(text);
|
22 | };
|
23 | /** {@inheritdoc IStringBuilder.toString} */
|
24 | StringBuilder.prototype.toString = function () {
|
25 | if (this._chunks.length === 0) {
|
26 | return '';
|
27 | }
|
28 | if (this._chunks.length > 1) {
|
29 | var joined = this._chunks.join('');
|
30 | this._chunks.length = 1;
|
31 | this._chunks[0] = joined;
|
32 | }
|
33 | return this._chunks[0];
|
34 | };
|
35 | return StringBuilder;
|
36 | }());
|
37 | exports.StringBuilder = StringBuilder;
|
38 | //# sourceMappingURL=StringBuilder.js.map |
\ | No newline at end of file |