Skip to content

Commit

Permalink
Merge pull request #4 from mcpower/tradeoffs
Browse files Browse the repository at this point in the history
Factor out use of indexOf (528)
  • Loading branch information
samthor committed Feb 18, 2022
2 parents c1d2e6a + ed5cbba commit 0289d93
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions index.js
Expand Up @@ -27,9 +27,13 @@ export function *iter(source) {

/** @type {number} */
let temp;
/** @type {number} */
let temp2;

let sourceCharCodeAt = () => source.charCodeAt(i);
let substringIToTemp = () => source.substring(i, temp);
let nextIndex = (/** @type {string} */ c) =>
(temp2 = source.indexOf(c, i)) < 0 ? length : temp2;

for (;;) {
// we consume at most one col per outer loop
Expand All @@ -43,21 +47,15 @@ export function *iter(source) {
}

if (i > newline) {
newline = source.indexOf('\n', i);
if (newline < 0) {
newline = length;
}
newline = nextIndex('\n');
}

if (sourceCharCodeAt() == C_QUOTE) {
s = '';
// consume many parts of quoted string
for (; ;) {
++i;
temp = source.indexOf('"', i);
if (temp < 0) {
temp = length;
}
temp = nextIndex('"');
s += substringIToTemp();

i = temp + 1;
Expand All @@ -73,8 +71,8 @@ export function *iter(source) {
} else {
// this is a "normal" value, ends with a comma or newline
// look for comma first (educated guess)
temp = source.indexOf(',', i);
if (temp < 0 || newline < temp) {
temp = nextIndex(',');
if (newline < temp) {
temp = newline;
}

Expand Down

0 comments on commit 0289d93

Please sign in to comment.