Skip to content

Commit 2674623

Browse files
committed
Changeset: Turn stringIterator() into a real class
1 parent 3a2f618 commit 2674623

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

664664
/**
665665
* A custom made String Iterator
666-
*
667-
* @typedef {object} StringIterator
668-
* @property {Function} newlines -
669-
* @property {Function} peek -
670-
* @property {Function} remaining -
671-
* @property {Function} skip -
672-
* @property {Function} take -
673666
*/
667+
class StringIterator {
668+
constructor(str) {
669+
this._str = str;
670+
this._curIndex = 0;
671+
// this._newLines is the number of \n between this._curIndex and this._str.length
672+
this._newLines = this._str.split('\n').length - 1;
673+
}
674674

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

685-
const assertRemaining = (n) => {
686-
assert(n <= remaining(), `!(${n} <= ${remaining()})`);
687-
};
679+
_assertRemaining(n) {
680+
assert(n <= this.remaining(), `!(${n} <= ${this.remaining()})`);
681+
}
688682

689-
const take = (n) => {
690-
assertRemaining(n);
691-
const s = str.substr(curIndex, n);
692-
newLines -= s.split('\n').length - 1;
693-
curIndex += n;
683+
take(n) {
684+
this._assertRemaining(n);
685+
const s = this._str.substr(this._curIndex, n);
686+
this._newLines -= s.split('\n').length - 1;
687+
this._curIndex += n;
694688
return s;
695-
};
689+
}
696690

697-
const peek = (n) => {
698-
assertRemaining(n);
699-
const s = str.substr(curIndex, n);
691+
peek(n) {
692+
this._assertRemaining(n);
693+
const s = this._str.substr(this._curIndex, n);
700694
return s;
701-
};
695+
}
702696

703-
const skip = (n) => {
704-
assertRemaining(n);
705-
curIndex += n;
706-
};
697+
skip(n) {
698+
this._assertRemaining(n);
699+
this._curIndex += n;
700+
}
707701

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

718713
/**
719714
* A custom made StringBuffer
@@ -1160,8 +1155,8 @@ exports.pack = (oldLen, newLen, opsStr, bank) => {
11601155
exports.applyToText = (cs, str) => {
11611156
const unpacked = exports.unpack(cs);
11621157
assert(str.length === unpacked.oldLen, `mismatched apply: ${str.length} / ${unpacked.oldLen}`);
1163-
const bankIter = exports.stringIterator(unpacked.charBank);
1164-
const strIter = exports.stringIterator(str);
1158+
const bankIter = new StringIterator(unpacked.charBank);
1159+
const strIter = new StringIterator(str);
11651160
let assem = '';
11661161
for (const op of exports.deserializeOps(unpacked.ops)) {
11671162
switch (op.opcode) {
@@ -1203,7 +1198,7 @@ exports.applyToText = (cs, str) => {
12031198
*/
12041199
exports.mutateTextLines = (cs, lines) => {
12051200
const unpacked = exports.unpack(cs);
1206-
const bankIter = exports.stringIterator(unpacked.charBank);
1201+
const bankIter = new StringIterator(unpacked.charBank);
12071202
const mut = new TextLinesMutator(lines);
12081203
for (const op of exports.deserializeOps(unpacked.ops)) {
12091204
switch (op.opcode) {
@@ -1479,8 +1474,8 @@ exports.compose = (cs1, cs2, pool) => {
14791474
const len2 = unpacked1.newLen;
14801475
assert(len2 === unpacked2.oldLen, 'mismatched composition of two changesets');
14811476
const len3 = unpacked2.newLen;
1482-
const bankIter1 = exports.stringIterator(unpacked1.charBank);
1483-
const bankIter2 = exports.stringIterator(unpacked2.charBank);
1477+
const bankIter1 = new StringIterator(unpacked1.charBank);
1478+
const bankIter2 = new StringIterator(unpacked2.charBank);
14841479
let bankAssem = '';
14851480

14861481
const newOps = applyZip(unpacked1.ops, unpacked2.ops, (op1, op2) => {
@@ -1571,7 +1566,7 @@ const toSplices = (cs) => {
15711566
const splices = [];
15721567

15731568
let oldPos = 0;
1574-
const charIter = exports.stringIterator(unpacked.charBank);
1569+
const charIter = new StringIterator(unpacked.charBank);
15751570
let inSplice = false;
15761571
for (const op of exports.deserializeOps(unpacked.ops)) {
15771572
if (op.opcode === '=') {
@@ -2256,8 +2251,8 @@ exports.follow = (cs1, cs2, reverseInsertOrder, pool) => {
22562251
const len1 = unpacked1.oldLen;
22572252
const len2 = unpacked2.oldLen;
22582253
assert(len1 === len2, 'mismatched follow - cannot transform cs1 on top of cs2');
2259-
const chars1 = exports.stringIterator(unpacked1.charBank);
2260-
const chars2 = exports.stringIterator(unpacked2.charBank);
2254+
const chars1 = new StringIterator(unpacked1.charBank);
2255+
const chars2 = new StringIterator(unpacked2.charBank);
22612256

22622257
const oldLen = unpacked1.newLen;
22632258
let oldPos = 0;

0 commit comments

Comments
 (0)