Skip to content

Commit a5ba53f

Browse files
committed
Changeset: Turn stringIterator() into a real class
1 parent 849f2aa commit a5ba53f

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
@@ -676,57 +676,52 @@ exports.opAssembler = () => {
676676

677677
/**
678678
* A custom made String Iterator
679-
*
680-
* @typedef {object} StringIterator
681-
* @property {Function} newlines -
682-
* @property {Function} peek -
683-
* @property {Function} remaining -
684-
* @property {Function} skip -
685-
* @property {Function} take -
686679
*/
680+
class StringIterator {
681+
constructor(str) {
682+
this._str = str;
683+
this._curIndex = 0;
684+
// this._newLines is the number of \n between this._curIndex and this._str.length
685+
this._newLines = this._str.split('\n').length - 1;
686+
}
687687

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

698-
const assertRemaining = (n) => {
699-
assert(n <= remaining(), `!(${n} <= ${remaining()})`);
700-
};
692+
_assertRemaining(n) {
693+
assert(n <= this.remaining(), `!(${n} <= ${this.remaining()})`);
694+
}
701695

702-
const take = (n) => {
703-
assertRemaining(n);
704-
const s = str.substr(curIndex, n);
705-
newLines -= s.split('\n').length - 1;
706-
curIndex += n;
696+
take(n) {
697+
this._assertRemaining(n);
698+
const s = this._str.substr(this._curIndex, n);
699+
this._newLines -= s.split('\n').length - 1;
700+
this._curIndex += n;
707701
return s;
708-
};
702+
}
709703

710-
const peek = (n) => {
711-
assertRemaining(n);
712-
const s = str.substr(curIndex, n);
704+
peek(n) {
705+
this._assertRemaining(n);
706+
const s = this._str.substr(this._curIndex, n);
713707
return s;
714-
};
708+
}
715709

716-
const skip = (n) => {
717-
assertRemaining(n);
718-
curIndex += n;
719-
};
710+
skip(n) {
711+
this._assertRemaining(n);
712+
this._curIndex += n;
713+
}
720714

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

731726
/**
732727
* A custom made StringBuffer
@@ -1174,8 +1169,8 @@ exports.pack = (oldLen, newLen, opsStr, bank) => {
11741169
exports.applyToText = (cs, str) => {
11751170
const unpacked = exports.unpack(cs);
11761171
assert(str.length === unpacked.oldLen, `mismatched apply: ${str.length} / ${unpacked.oldLen}`);
1177-
const bankIter = exports.stringIterator(unpacked.charBank);
1178-
const strIter = exports.stringIterator(str);
1172+
const bankIter = new StringIterator(unpacked.charBank);
1173+
const strIter = new StringIterator(str);
11791174
let assem = '';
11801175
for (const op of exports.deserializeOps(unpacked.ops)) {
11811176
switch (op.opcode) {
@@ -1217,7 +1212,7 @@ exports.applyToText = (cs, str) => {
12171212
*/
12181213
exports.mutateTextLines = (cs, lines) => {
12191214
const unpacked = exports.unpack(cs);
1220-
const bankIter = exports.stringIterator(unpacked.charBank);
1215+
const bankIter = new StringIterator(unpacked.charBank);
12211216
const mut = new TextLinesMutator(lines);
12221217
for (const op of exports.deserializeOps(unpacked.ops)) {
12231218
switch (op.opcode) {
@@ -1493,8 +1488,8 @@ exports.compose = (cs1, cs2, pool) => {
14931488
const len2 = unpacked1.newLen;
14941489
assert(len2 === unpacked2.oldLen, 'mismatched composition of two changesets');
14951490
const len3 = unpacked2.newLen;
1496-
const bankIter1 = exports.stringIterator(unpacked1.charBank);
1497-
const bankIter2 = exports.stringIterator(unpacked2.charBank);
1491+
const bankIter1 = new StringIterator(unpacked1.charBank);
1492+
const bankIter2 = new StringIterator(unpacked2.charBank);
14981493
let bankAssem = '';
14991494

15001495
const newOps = applyZip(unpacked1.ops, unpacked2.ops, (op1, op2) => {
@@ -1585,7 +1580,7 @@ const toSplices = (cs) => {
15851580
const splices = [];
15861581

15871582
let oldPos = 0;
1588-
const charIter = exports.stringIterator(unpacked.charBank);
1583+
const charIter = new StringIterator(unpacked.charBank);
15891584
let inSplice = false;
15901585
for (const op of exports.deserializeOps(unpacked.ops)) {
15911586
if (op.opcode === '=') {
@@ -2270,8 +2265,8 @@ exports.follow = (cs1, cs2, reverseInsertOrder, pool) => {
22702265
const len1 = unpacked1.oldLen;
22712266
const len2 = unpacked2.oldLen;
22722267
assert(len1 === len2, 'mismatched follow - cannot transform cs1 on top of cs2');
2273-
const chars1 = exports.stringIterator(unpacked1.charBank);
2274-
const chars2 = exports.stringIterator(unpacked2.charBank);
2268+
const chars1 = new StringIterator(unpacked1.charBank);
2269+
const chars2 = new StringIterator(unpacked2.charBank);
22752270

22762271
const oldLen = unpacked1.newLen;
22772272
let oldPos = 0;

0 commit comments

Comments
 (0)