Skip to content

Commit

Permalink
Changeset: Move stringIterator() logic to a new class
Browse files Browse the repository at this point in the history
  • Loading branch information
rhansen committed Oct 17, 2021
1 parent 2ffa9df commit 3e7389a
Showing 1 changed file with 47 additions and 44 deletions.
91 changes: 47 additions & 44 deletions src/static/js/Changeset.js
Original file line number Diff line number Diff line change
Expand Up @@ -631,48 +631,51 @@ exports.opAssembler = () => {
return new OpAssembler();
};

/**
* A custom made String Iterator
* @param {string} str - String to be iterated over
*/
exports.stringIterator = (str) => {
let curIndex = 0;
// newLines is the number of \n between curIndex and str.length
let newLines = str.split('\n').length - 1;
const getnewLines = () => newLines;

const assertRemaining = (n) => {
assert(n <= remaining(), '!(', n, ' <= ', remaining(), ')');
};
class StringIterator {
constructor(str) {
this._str = str;
this._curIndex = 0;
// this._newLines is the number of \n between this._curIndex and this._str.length
this._newLines = this._str.split('\n').length - 1;
}

const take = (n) => {
assertRemaining(n);
const s = str.substr(curIndex, n);
newLines -= s.split('\n').length - 1;
curIndex += n;
newlines() {
return this._newLines;
}

_assertRemaining(n) {
assert(n <= this.remaining(), '!(', n, ' <= ', this.remaining(), ')');
}

take(n) {
this._assertRemaining(n);
const s = this._str.substr(this._curIndex, n);
this._newLines -= s.split('\n').length - 1;
this._curIndex += n;
return s;
};
}

const peek = (n) => {
assertRemaining(n);
const s = str.substr(curIndex, n);
peek(n) {
this._assertRemaining(n);
const s = this._str.substr(this._curIndex, n);
return s;
};
}

const skip = (n) => {
assertRemaining(n);
curIndex += n;
};
skip(n) {
this._assertRemaining(n);
this._curIndex += n;
}

const remaining = () => str.length - curIndex;
return {
take,
skip,
remaining,
peek,
newlines: getnewLines,
};
};
remaining() {
return this._str.length - this._curIndex;
}
}

/**
* A custom made String Iterator
* @param {string} str - String to be iterated over
*/
exports.stringIterator = (str) => new StringIterator(str);

class StringAssembler {
constructor() { this._str = ''; }
Expand Down Expand Up @@ -1081,8 +1084,8 @@ exports.pack = (oldLen, newLen, opsStr, bank) => {
exports.applyToText = (cs, str) => {
const unpacked = exports.unpack(cs);
assert(str.length === unpacked.oldLen, 'mismatched apply: ', str.length, ' / ', unpacked.oldLen);
const bankIter = exports.stringIterator(unpacked.charBank);
const strIter = exports.stringIterator(str);
const bankIter = new StringIterator(unpacked.charBank);
const strIter = new StringIterator(str);
let assem = '';
for (const op of new exports.OpIter(unpacked.ops)) {
switch (op.opcode) {
Expand Down Expand Up @@ -1123,7 +1126,7 @@ exports.applyToText = (cs, str) => {
*/
exports.mutateTextLines = (cs, lines) => {
const unpacked = exports.unpack(cs);
const bankIter = exports.stringIterator(unpacked.charBank);
const bankIter = new StringIterator(unpacked.charBank);
const mut = new TextLinesMutator(lines);
for (const op of new exports.OpIter(unpacked.ops)) {
switch (op.opcode) {
Expand Down Expand Up @@ -1395,8 +1398,8 @@ exports.compose = (cs1, cs2, pool) => {
const len2 = unpacked1.newLen;
assert(len2 === unpacked2.oldLen, 'mismatched composition of two changesets');
const len3 = unpacked2.newLen;
const bankIter1 = exports.stringIterator(unpacked1.charBank);
const bankIter2 = exports.stringIterator(unpacked2.charBank);
const bankIter1 = new StringIterator(unpacked1.charBank);
const bankIter2 = new StringIterator(unpacked2.charBank);
let bankAssem = '';

const newOps = applyZip(unpacked1.ops, unpacked2.ops, (op1, op2) => {
Expand Down Expand Up @@ -1478,7 +1481,7 @@ const toSplices = (cs) => {
const splices = [];

let oldPos = 0;
const charIter = exports.stringIterator(unpacked.charBank);
const charIter = new StringIterator(unpacked.charBank);
let inSplice = false;
for (const op of new exports.OpIter(unpacked.ops)) {
if (op.opcode === '=') {
Expand Down Expand Up @@ -2054,8 +2057,8 @@ exports.follow = (cs1, cs2, reverseInsertOrder, pool) => {
const len1 = unpacked1.oldLen;
const len2 = unpacked2.oldLen;
assert(len1 === len2, 'mismatched follow - cannot transform cs1 on top of cs2');
const chars1 = exports.stringIterator(unpacked1.charBank);
const chars2 = exports.stringIterator(unpacked2.charBank);
const chars1 = new StringIterator(unpacked1.charBank);
const chars2 = new StringIterator(unpacked2.charBank);

const oldLen = unpacked1.newLen;
let oldPos = 0;
Expand Down

0 comments on commit 3e7389a

Please sign in to comment.