Skip to content

Commit e171450

Browse files
committed
Use better naming for regex split
1 parent 9468ded commit e171450

File tree

1 file changed

+20
-32
lines changed

1 file changed

+20
-32
lines changed

mikupad.html

+20-32
Original file line numberDiff line numberDiff line change
@@ -3570,36 +3570,24 @@
35703570
);
35713571
}
35723572

3573-
/**
3574-
* Split a string with a RegExp separator an optionally limited number of times.
3575-
* (https://stackoverflow.com/a/64296576)
3576-
* @param {string} input
3577-
* @param {RegExp} separator
3578-
* @param {number} [limit] - If not included, splits the maximum times
3579-
* @returns {[string[], string[]]}
3580-
*/
3581-
function split(input, separator, limit) {
3582-
separator = new RegExp(separator, 'g');
3583-
limit = limit ?? -1;
3584-
3585-
const output = [];
3586-
const separators = [];
3587-
let finalIndex = 0;
3588-
3589-
while (limit--) {
3590-
const lastIndex = separator.lastIndex;
3591-
const search = separator.exec(input);
3592-
if (search === null) {
3593-
break;
3594-
}
3595-
finalIndex = separator.lastIndex;
3596-
output.push(input.slice(lastIndex, search.index));
3597-
separators.push(search[0]);
3598-
}
3599-
3600-
output.push(input.slice(finalIndex));
3601-
3602-
return [output, separators];
3573+
function regexSplitString(str, separator, limit) {
3574+
const result = [];
3575+
const separators = [];
3576+
let lastIndex = 0;
3577+
let match;
3578+
const regex = new RegExp(separator, 'g');
3579+
3580+
while ((match = regex.exec(str)) !== null) {
3581+
if (limit !== undefined && result.length >= limit) break;
3582+
3583+
result.push(str.slice(lastIndex, match.index));
3584+
separators.push(match[0]);
3585+
lastIndex = match.index + match[0].length;
3586+
}
3587+
3588+
result.push(str.slice(lastIndex)); // Add the remainder of the string
3589+
3590+
return [result, separators];
36033591
}
36043592

36053593
function useSessionState(sessionStorage, name, initialState) {
@@ -4097,7 +4085,7 @@
40974085

40984086
if (chunk.content.includes(fillPlaceholder) || chunk.content.includes(predictPlaceholder)) {
40994087
// split the chunk in 2
4100-
let [sides, separators] = split(chunk.content, placeholderRegex, 1);
4088+
let [sides, separators] = regexSplitString(chunk.content, placeholderRegex, 1);
41014089
foundFillPlaceholder = separators[0] == fillPlaceholder;
41024090
foundPredictPlaceholder = separators[0] == predictPlaceholder;
41034091

@@ -4135,7 +4123,7 @@
41354123
} else {
41364124
promptText = joinPrompt(leftPromptChunks);
41374125
}
4138-
4126+
41394127
const assembledWorldInfo = assembleWorldInfo(promptText);
41404128
const additionalContextPrompt = assembleAdditionalContext(assembledWorldInfo, promptText);
41414129
const finalPrompt = replacePlaceholders(additionalContextPrompt, templateReplacements);

0 commit comments

Comments
 (0)