Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Factor out use of indexOf (528) #4

Merged
merged 2 commits into from Feb 18, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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