source: trip-planner-front/node_modules/@angular-devkit/schematics/src/utility/update-buffer.js

Last change on this file was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 8.4 KB
Line 
1"use strict";
2/**
3 * @license
4 * Copyright Google LLC All Rights Reserved.
5 *
6 * Use of this source code is governed by an MIT-style license that can be
7 * found in the LICENSE file at https://angular.io/license
8 */
9Object.defineProperty(exports, "__esModule", { value: true });
10exports.UpdateBuffer = exports.Chunk = exports.ContentCannotBeRemovedException = exports.IndexOutOfBoundException = void 0;
11const core_1 = require("@angular-devkit/core");
12const linked_list_1 = require("./linked-list");
13class IndexOutOfBoundException extends core_1.BaseException {
14 constructor(index, min, max = Infinity) {
15 super(`Index ${index} outside of range [${min}, ${max}].`);
16 }
17}
18exports.IndexOutOfBoundException = IndexOutOfBoundException;
19class ContentCannotBeRemovedException extends core_1.BaseException {
20 constructor() {
21 super(`User tried to remove content that was marked essential.`);
22 }
23}
24exports.ContentCannotBeRemovedException = ContentCannotBeRemovedException;
25/**
26 * A Chunk description, including left/right content that has been inserted.
27 * If _left/_right is null, this means that content was deleted. If the _content is null,
28 * it means the content itself was deleted.
29 *
30 * @see UpdateBuffer
31 */
32class Chunk {
33 constructor(start, end, originalContent) {
34 this.start = start;
35 this.end = end;
36 this.originalContent = originalContent;
37 this._left = Buffer.alloc(0);
38 this._right = Buffer.alloc(0);
39 this._assertLeft = false;
40 this._assertRight = false;
41 this.next = null;
42 this._content = originalContent.slice(start, end);
43 }
44 get length() {
45 return ((this._left ? this._left.length : 0) +
46 (this._content ? this._content.length : 0) +
47 (this._right ? this._right.length : 0));
48 }
49 toString(encoding = 'utf-8') {
50 return ((this._left ? this._left.toString(encoding) : '') +
51 (this._content ? this._content.toString(encoding) : '') +
52 (this._right ? this._right.toString(encoding) : ''));
53 }
54 slice(start) {
55 if (start < this.start || start > this.end) {
56 throw new IndexOutOfBoundException(start, this.start, this.end);
57 }
58 // Update _content to the new indices.
59 const newChunk = new Chunk(start, this.end, this.originalContent);
60 // If this chunk has _content, reslice the original _content. We move the _right so we are not
61 // losing any data here. If this chunk has been deleted, the next chunk should also be deleted.
62 if (this._content) {
63 this._content = this.originalContent.slice(this.start, start);
64 }
65 else {
66 newChunk._content = this._content;
67 if (this._right === null) {
68 newChunk._left = null;
69 }
70 }
71 this.end = start;
72 // Move _right to the new chunk.
73 newChunk._right = this._right;
74 this._right = this._right && Buffer.alloc(0);
75 // Update essentials.
76 if (this._assertRight) {
77 newChunk._assertRight = true;
78 this._assertRight = false;
79 }
80 // Update the linked list.
81 newChunk.next = this.next;
82 this.next = newChunk;
83 return newChunk;
84 }
85 append(buffer, essential) {
86 if (!this._right) {
87 if (essential) {
88 throw new ContentCannotBeRemovedException();
89 }
90 return;
91 }
92 const outro = this._right;
93 this._right = Buffer.alloc(outro.length + buffer.length);
94 outro.copy(this._right, 0);
95 buffer.copy(this._right, outro.length);
96 if (essential) {
97 this._assertRight = true;
98 }
99 }
100 prepend(buffer, essential) {
101 if (!this._left) {
102 if (essential) {
103 throw new ContentCannotBeRemovedException();
104 }
105 return;
106 }
107 const intro = this._left;
108 this._left = Buffer.alloc(intro.length + buffer.length);
109 intro.copy(this._left, 0);
110 buffer.copy(this._left, intro.length);
111 if (essential) {
112 this._assertLeft = true;
113 }
114 }
115 assert(left, _content, right) {
116 if (left && this._assertLeft) {
117 throw new ContentCannotBeRemovedException();
118 }
119 if (right && this._assertRight) {
120 throw new ContentCannotBeRemovedException();
121 }
122 }
123 remove(left, content, right) {
124 if (left) {
125 if (this._assertLeft) {
126 throw new ContentCannotBeRemovedException();
127 }
128 this._left = null;
129 }
130 if (content) {
131 this._content = null;
132 }
133 if (right) {
134 if (this._assertRight) {
135 throw new ContentCannotBeRemovedException();
136 }
137 this._right = null;
138 }
139 }
140 copy(target, start) {
141 if (this._left) {
142 this._left.copy(target, start);
143 start += this._left.length;
144 }
145 if (this._content) {
146 this._content.copy(target, start);
147 start += this._content.length;
148 }
149 if (this._right) {
150 this._right.copy(target, start);
151 start += this._right.length;
152 }
153 return start;
154 }
155}
156exports.Chunk = Chunk;
157/**
158 * An utility class that allows buffers to be inserted to the _right or _left, or deleted, while
159 * keeping indices to the original buffer.
160 *
161 * The constructor takes an original buffer, and keeps it into a linked list of chunks, smaller
162 * buffers that keep track of _content inserted to the _right or _left of it.
163 *
164 * Since the Node Buffer structure is non-destructive when slicing, we try to use slicing to create
165 * new chunks, and always keep chunks pointing to the original content.
166 */
167class UpdateBuffer {
168 constructor(_originalContent) {
169 this._originalContent = _originalContent;
170 this._linkedList = new linked_list_1.LinkedList(new Chunk(0, _originalContent.length, _originalContent));
171 }
172 _assertIndex(index) {
173 if (index < 0 || index > this._originalContent.length) {
174 throw new IndexOutOfBoundException(index, 0, this._originalContent.length);
175 }
176 }
177 _slice(start) {
178 // If start is longer than the content, use start, otherwise determine exact position in string.
179 const index = start >= this._originalContent.length ? start : this._getTextPosition(start);
180 this._assertIndex(index);
181 // Find the chunk by going through the list.
182 const h = this._linkedList.find((chunk) => index <= chunk.end);
183 if (!h) {
184 throw Error('Chunk cannot be found.');
185 }
186 if (index == h.end && h.next !== null) {
187 return [h, h.next];
188 }
189 return [h, h.slice(index)];
190 }
191 /**
192 * Gets the position in the content based on the position in the string.
193 * Some characters might be wider than one byte, thus we have to determine the position using
194 * string functions.
195 */
196 _getTextPosition(index) {
197 return Buffer.from(this._originalContent.toString().substring(0, index)).length;
198 }
199 get length() {
200 return this._linkedList.reduce((acc, chunk) => acc + chunk.length, 0);
201 }
202 get original() {
203 return this._originalContent;
204 }
205 toString(encoding = 'utf-8') {
206 return this._linkedList.reduce((acc, chunk) => acc + chunk.toString(encoding), '');
207 }
208 generate() {
209 const result = Buffer.allocUnsafe(this.length);
210 let i = 0;
211 this._linkedList.forEach((chunk) => {
212 chunk.copy(result, i);
213 i += chunk.length;
214 });
215 return result;
216 }
217 insertLeft(index, content, assert = false) {
218 this._slice(index)[0].append(content, assert);
219 }
220 insertRight(index, content, assert = false) {
221 this._slice(index)[1].prepend(content, assert);
222 }
223 remove(index, length) {
224 const end = index + length;
225 const first = this._slice(index)[1];
226 const last = this._slice(end)[1];
227 let curr;
228 for (curr = first; curr && curr !== last; curr = curr.next) {
229 curr.assert(curr !== first, curr !== last, curr === first);
230 }
231 for (curr = first; curr && curr !== last; curr = curr.next) {
232 curr.remove(curr !== first, curr !== last, curr === first);
233 }
234 if (curr) {
235 curr.remove(true, false, false);
236 }
237 }
238}
239exports.UpdateBuffer = UpdateBuffer;
Note: See TracBrowser for help on using the repository browser.