Skip to content

Commit b4d8828

Browse files
committed
Changeset: Turn stringIterator() into a real class
1 parent 06bab91 commit b4d8828

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

660660
/**
661661
* A custom made String Iterator
662-
*
663-
* @typedef {object} StringIterator
664-
* @property {Function} newlines -
665-
* @property {Function} peek -
666-
* @property {Function} remaining -
667-
* @property {Function} skip -
668-
* @property {Function} take -
669662
*/
663+
class StringIterator {
664+
constructor(str) {
665+
this._str = str;
666+
this._curIndex = 0;
667+
// this._newLines is the number of \n between this._curIndex and this._str.length
668+
this._newLines = this._str.split('\n').length - 1;
669+
}
670670

671-
/**
672-
* @param {string} str - String to iterate over
673-
* @returns {StringIterator}
674-
*/
675-
exports.stringIterator = (str) => {
676-
let curIndex = 0;
677-
// newLines is the number of \n between curIndex and str.length
678-
let newLines = str.split('\n').length - 1;
679-
const getnewLines = () => newLines;
671+
newlines() {
672+
return this._newLines;
673+
}
680674

681-
const assertRemaining = (n) => {
682-
assert(n <= remaining(), `!(${n} <= ${remaining()})`);
683-
};
675+
_assertRemaining(n) {
676+
assert(n <= this.remaining(), `!(${n} <= ${this.remaining()})`);
677+
}
684678

685-
const take = (n) => {
686-
assertRemaining(n);
687-
const s = str.substr(curIndex, n);
688-
newLines -= s.split('\n').length - 1;
689-
curIndex += n;
679+
take(n) {
680+
this._assertRemaining(n);
681+
const s = this._str.substr(this._curIndex, n);
682+
this._newLines -= s.split('\n').length - 1;
683+
this._curIndex += n;
690684
return s;
691-
};
685+
}
692686

693-
const peek = (n) => {
694-
assertRemaining(n);
695-
const s = str.substr(curIndex, n);
687+
peek(n) {
688+
this._assertRemaining(n);
689+
const s = this._str.substr(this._curIndex, n);
696690
return s;
697-
};
691+
}
698692

699-
const skip = (n) => {
700-
assertRemaining(n);
701-
curIndex += n;
702-
};
693+
skip(n) {
694+
this._assertRemaining(n);
695+
this._curIndex += n;
696+
}
703697

704-
const remaining = () => str.length - curIndex;
705-
return {
706-
take,
707-
skip,
708-
remaining,
709-
peek,
710-
newlines: getnewLines,
711-
};
712-
};
698+
remaining() {
699+
return this._str.length - this._curIndex;
700+
}
701+
}
702+
703+
/**
704+
* @param {string} str - String to iterate over
705+
* @returns {StringIterator}
706+
*/
707+
exports.stringIterator = (str) => new StringIterator(str);
713708

714709
/**
715710
* A custom made StringBuffer
@@ -1157,8 +1152,8 @@ exports.pack = (oldLen, newLen, opsStr, bank) => {
11571152
exports.applyToText = (cs, str) => {
11581153
const unpacked = exports.unpack(cs);
11591154
assert(str.length === unpacked.oldLen, `mismatched apply: ${str.length} / ${unpacked.oldLen}`);
1160-
const bankIter = exports.stringIterator(unpacked.charBank);
1161-
const strIter = exports.stringIterator(str);
1155+
const bankIter = new StringIterator(unpacked.charBank);
1156+
const strIter = new StringIterator(str);
11621157
let assem = '';
11631158
for (const op of exports.deserializeOps(unpacked.ops)) {
11641159
switch (op.opcode) {
@@ -1200,7 +1195,7 @@ exports.applyToText = (cs, str) => {
12001195
*/
12011196
exports.mutateTextLines = (cs, lines) => {
12021197
const unpacked = exports.unpack(cs);
1203-
const bankIter = exports.stringIterator(unpacked.charBank);
1198+
const bankIter = new StringIterator(unpacked.charBank);
12041199
const mut = new TextLinesMutator(lines);
12051200
for (const op of exports.deserializeOps(unpacked.ops)) {
12061201
switch (op.opcode) {
@@ -1476,8 +1471,8 @@ exports.compose = (cs1, cs2, pool) => {
14761471
const len2 = unpacked1.newLen;
14771472
assert(len2 === unpacked2.oldLen, 'mismatched composition of two changesets');
14781473
const len3 = unpacked2.newLen;
1479-
const bankIter1 = exports.stringIterator(unpacked1.charBank);
1480-
const bankIter2 = exports.stringIterator(unpacked2.charBank);
1474+
const bankIter1 = new StringIterator(unpacked1.charBank);
1475+
const bankIter2 = new StringIterator(unpacked2.charBank);
14811476
let bankAssem = '';
14821477

14831478
const newOps = applyZip(unpacked1.ops, unpacked2.ops, (op1, op2) => {
@@ -1568,7 +1563,7 @@ const toSplices = (cs) => {
15681563
const splices = [];
15691564

15701565
let oldPos = 0;
1571-
const charIter = exports.stringIterator(unpacked.charBank);
1566+
const charIter = new StringIterator(unpacked.charBank);
15721567
let inSplice = false;
15731568
for (const op of exports.deserializeOps(unpacked.ops)) {
15741569
if (op.opcode === '=') {
@@ -2253,8 +2248,8 @@ exports.follow = (cs1, cs2, reverseInsertOrder, pool) => {
22532248
const len1 = unpacked1.oldLen;
22542249
const len2 = unpacked2.oldLen;
22552250
assert(len1 === len2, 'mismatched follow - cannot transform cs1 on top of cs2');
2256-
const chars1 = exports.stringIterator(unpacked1.charBank);
2257-
const chars2 = exports.stringIterator(unpacked2.charBank);
2251+
const chars1 = new StringIterator(unpacked1.charBank);
2252+
const chars2 = new StringIterator(unpacked2.charBank);
22582253

22592254
const oldLen = unpacked1.newLen;
22602255
let oldPos = 0;

0 commit comments

Comments
 (0)