Skip to content

Commit 236b7f1

Browse files
committed
Changeset: Turn stringIterator() into a real class
1 parent 7b76323 commit 236b7f1

File tree

1 file changed

+45
-50
lines changed

1 file changed

+45
-50
lines changed

src/static/js/Changeset.js

Lines changed: 45 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -671,57 +671,52 @@ exports.opAssembler = () => {
671671

672672
/**
673673
* A custom made String Iterator
674-
*
675-
* @typedef {object} StringIterator
676-
* @property {Function} newlines -
677-
* @property {Function} peek -
678-
* @property {Function} remaining -
679-
* @property {Function} skip -
680-
* @property {Function} take -
681674
*/
675+
class StringIterator {
676+
constructor(str) {
677+
this._str = str;
678+
this._curIndex = 0;
679+
// this._newLines is the number of \n between this._curIndex and this._str.length
680+
this._newLines = this._str.split('\n').length - 1;
681+
}
682682

683-
/**
684-
* @param {string} str - String to iterate over
685-
* @returns {StringIterator}
686-
*/
687-
exports.stringIterator = (str) => {
688-
let curIndex = 0;
689-
// newLines is the number of \n between curIndex and str.length
690-
let newLines = str.split('\n').length - 1;
691-
const getnewLines = () => newLines;
683+
newlines() {
684+
return this._newLines;
685+
}
692686

693-
const assertRemaining = (n) => {
694-
assert(n <= remaining(), `!( ${n} <= ${remaining()} )`);
695-
};
687+
_assertRemaining(n) {
688+
assert(n <= this.remaining(), `!( ${n} <= ${this.remaining()} )`);
689+
}
696690

697-
const take = (n) => {
698-
assertRemaining(n);
699-
const s = str.substr(curIndex, n);
700-
newLines -= s.split('\n').length - 1;
701-
curIndex += n;
691+
take(n) {
692+
this._assertRemaining(n);
693+
const s = this._str.substr(this._curIndex, n);
694+
this._newLines -= s.split('\n').length - 1;
695+
this._curIndex += n;
702696
return s;
703-
};
697+
}
704698

705-
const peek = (n) => {
706-
assertRemaining(n);
707-
const s = str.substr(curIndex, n);
699+
peek(n) {
700+
this._assertRemaining(n);
701+
const s = this._str.substr(this._curIndex, n);
708702
return s;
709-
};
703+
}
710704

711-
const skip = (n) => {
712-
assertRemaining(n);
713-
curIndex += n;
714-
};
705+
skip(n) {
706+
this._assertRemaining(n);
707+
this._curIndex += n;
708+
}
715709

716-
const remaining = () => str.length - curIndex;
717-
return {
718-
take,
719-
skip,
720-
remaining,
721-
peek,
722-
newlines: getnewLines,
723-
};
724-
};
710+
remaining() {
711+
return this._str.length - this._curIndex;
712+
}
713+
}
714+
715+
/**
716+
* @param {string} str - String to iterate over
717+
* @returns {StringIterator}
718+
*/
719+
exports.stringIterator = (str) => new StringIterator(str);
725720

726721
/**
727722
* A custom made StringBuffer
@@ -1168,8 +1163,8 @@ exports.pack = (oldLen, newLen, opsStr, bank) => {
11681163
exports.applyToText = (cs, str) => {
11691164
const unpacked = exports.unpack(cs);
11701165
assert(str.length === unpacked.oldLen, `mismatched apply: ${str.length} / ${unpacked.oldLen}`);
1171-
const bankIter = exports.stringIterator(unpacked.charBank);
1172-
const strIter = exports.stringIterator(str);
1166+
const bankIter = new StringIterator(unpacked.charBank);
1167+
const strIter = new StringIterator(str);
11731168
let assem = '';
11741169
for (const op of exports.deserializeOps(unpacked.ops)) {
11751170
switch (op.opcode) {
@@ -1211,7 +1206,7 @@ exports.applyToText = (cs, str) => {
12111206
*/
12121207
exports.mutateTextLines = (cs, lines) => {
12131208
const unpacked = exports.unpack(cs);
1214-
const bankIter = exports.stringIterator(unpacked.charBank);
1209+
const bankIter = new StringIterator(unpacked.charBank);
12151210
const mut = new TextLinesMutator(lines);
12161211
for (const op of exports.deserializeOps(unpacked.ops)) {
12171212
switch (op.opcode) {
@@ -1506,8 +1501,8 @@ exports.compose = (cs1, cs2, pool) => {
15061501
const len2 = unpacked1.newLen;
15071502
assert(len2 === unpacked2.oldLen, 'mismatched composition of two changesets');
15081503
const len3 = unpacked2.newLen;
1509-
const bankIter1 = exports.stringIterator(unpacked1.charBank);
1510-
const bankIter2 = exports.stringIterator(unpacked2.charBank);
1504+
const bankIter1 = new StringIterator(unpacked1.charBank);
1505+
const bankIter2 = new StringIterator(unpacked2.charBank);
15111506
let bankAssem = '';
15121507

15131508
const newOps = applyZip(unpacked1.ops, unpacked2.ops, (op1, op2) => {
@@ -1598,7 +1593,7 @@ const toSplices = (cs) => {
15981593
const splices = [];
15991594

16001595
let oldPos = 0;
1601-
const charIter = exports.stringIterator(unpacked.charBank);
1596+
const charIter = new StringIterator(unpacked.charBank);
16021597
let inSplice = false;
16031598
for (const op of exports.deserializeOps(unpacked.ops)) {
16041599
if (op.opcode === '=') {
@@ -2249,8 +2244,8 @@ exports.follow = (cs1, cs2, reverseInsertOrder, pool) => {
22492244
const len1 = unpacked1.oldLen;
22502245
const len2 = unpacked2.oldLen;
22512246
assert(len1 === len2, 'mismatched follow - cannot transform cs1 on top of cs2');
2252-
const chars1 = exports.stringIterator(unpacked1.charBank);
2253-
const chars2 = exports.stringIterator(unpacked2.charBank);
2247+
const chars1 = new StringIterator(unpacked1.charBank);
2248+
const chars2 = new StringIterator(unpacked2.charBank);
22542249

22552250
const oldLen = unpacked1.newLen;
22562251
let oldPos = 0;

0 commit comments

Comments
 (0)