Skip to content

Commit

Permalink
Use better naming for regex split
Browse files Browse the repository at this point in the history
  • Loading branch information
lmg-anon committed Jun 1, 2024
1 parent 9468ded commit e171450
Showing 1 changed file with 20 additions and 32 deletions.
52 changes: 20 additions & 32 deletions mikupad.html
Original file line number Diff line number Diff line change
Expand Up @@ -3570,36 +3570,24 @@
);
}

/**
* Split a string with a RegExp separator an optionally limited number of times.
* (https://stackoverflow.com/a/64296576)
* @param {string} input
* @param {RegExp} separator
* @param {number} [limit] - If not included, splits the maximum times
* @returns {[string[], string[]]}
*/
function split(input, separator, limit) {
separator = new RegExp(separator, 'g');
limit = limit ?? -1;

const output = [];
const separators = [];
let finalIndex = 0;

while (limit--) {
const lastIndex = separator.lastIndex;
const search = separator.exec(input);
if (search === null) {
break;
}
finalIndex = separator.lastIndex;
output.push(input.slice(lastIndex, search.index));
separators.push(search[0]);
}

output.push(input.slice(finalIndex));

return [output, separators];
function regexSplitString(str, separator, limit) {
const result = [];
const separators = [];
let lastIndex = 0;
let match;
const regex = new RegExp(separator, 'g');

while ((match = regex.exec(str)) !== null) {
if (limit !== undefined && result.length >= limit) break;

result.push(str.slice(lastIndex, match.index));
separators.push(match[0]);
lastIndex = match.index + match[0].length;
}

result.push(str.slice(lastIndex)); // Add the remainder of the string

return [result, separators];
}

function useSessionState(sessionStorage, name, initialState) {
Expand Down Expand Up @@ -4097,7 +4085,7 @@

if (chunk.content.includes(fillPlaceholder) || chunk.content.includes(predictPlaceholder)) {
// split the chunk in 2
let [sides, separators] = split(chunk.content, placeholderRegex, 1);
let [sides, separators] = regexSplitString(chunk.content, placeholderRegex, 1);
foundFillPlaceholder = separators[0] == fillPlaceholder;
foundPredictPlaceholder = separators[0] == predictPlaceholder;

Expand Down Expand Up @@ -4135,7 +4123,7 @@
} else {
promptText = joinPrompt(leftPromptChunks);
}

const assembledWorldInfo = assembleWorldInfo(promptText);
const additionalContextPrompt = assembleAdditionalContext(assembledWorldInfo, promptText);
const finalPrompt = replacePlaceholders(additionalContextPrompt, templateReplacements);
Expand Down

0 comments on commit e171450

Please sign in to comment.