@@ -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
@@ -1143,8 +1138,8 @@ exports.pack = (oldLen, newLen, opsStr, bank) => {
11431138exports . applyToText = ( cs , str ) => {
11441139 const unpacked = exports . unpack ( cs ) ;
11451140 assert ( str . length === unpacked . oldLen , 'mismatched apply: ' , str . length , ' / ' , unpacked . oldLen ) ;
1146- const bankIter = exports . stringIterator ( unpacked . charBank ) ;
1147- const strIter = exports . stringIterator ( str ) ;
1141+ const bankIter = new StringIterator ( unpacked . charBank ) ;
1142+ const strIter = new StringIterator ( str ) ;
11481143 let assem = '' ;
11491144 for ( const op of new exports . OpIter ( unpacked . ops ) ) {
11501145 switch ( op . opcode ) {
@@ -1186,7 +1181,7 @@ exports.applyToText = (cs, str) => {
11861181 */
11871182exports . mutateTextLines = ( cs , lines ) => {
11881183 const unpacked = exports . unpack ( cs ) ;
1189- const bankIter = exports . stringIterator ( unpacked . charBank ) ;
1184+ const bankIter = new StringIterator ( unpacked . charBank ) ;
11901185 const mut = new TextLinesMutator ( lines ) ;
11911186 for ( const op of new exports . OpIter ( unpacked . ops ) ) {
11921187 switch ( op . opcode ) {
@@ -1468,8 +1463,8 @@ exports.compose = (cs1, cs2, pool) => {
14681463 const len2 = unpacked1 . newLen ;
14691464 assert ( len2 === unpacked2 . oldLen , 'mismatched composition of two changesets' ) ;
14701465 const len3 = unpacked2 . newLen ;
1471- const bankIter1 = exports . stringIterator ( unpacked1 . charBank ) ;
1472- const bankIter2 = exports . stringIterator ( unpacked2 . charBank ) ;
1466+ const bankIter1 = new StringIterator ( unpacked1 . charBank ) ;
1467+ const bankIter2 = new StringIterator ( unpacked2 . charBank ) ;
14731468 let bankAssem = '' ;
14741469
14751470 const newOps = applyZip ( unpacked1 . ops , unpacked2 . ops , ( op1 , op2 ) => {
@@ -1559,7 +1554,7 @@ const toSplices = (cs) => {
15591554 const splices = [ ] ;
15601555
15611556 let oldPos = 0 ;
1562- const charIter = exports . stringIterator ( unpacked . charBank ) ;
1557+ const charIter = new StringIterator ( unpacked . charBank ) ;
15631558 let inSplice = false ;
15641559 for ( const op of new exports . OpIter ( unpacked . ops ) ) {
15651560 if ( op . opcode === '=' ) {
@@ -2171,8 +2166,8 @@ exports.follow = (cs1, cs2, reverseInsertOrder, pool) => {
21712166 const len1 = unpacked1 . oldLen ;
21722167 const len2 = unpacked2 . oldLen ;
21732168 assert ( len1 === len2 , 'mismatched follow - cannot transform cs1 on top of cs2' ) ;
2174- const chars1 = exports . stringIterator ( unpacked1 . charBank ) ;
2175- const chars2 = exports . stringIterator ( unpacked2 . charBank ) ;
2169+ const chars1 = new StringIterator ( unpacked1 . charBank ) ;
2170+ const chars2 = new StringIterator ( unpacked2 . charBank ) ;
21762171
21772172 const oldLen = unpacked1 . newLen ;
21782173 let oldPos = 0 ;
0 commit comments