13
edits
m (→replacegreedy() / replacegreedyi(): slightly more efficient) |
(→replacegreedy() / replacegreedyi(): optimizations; fixed for length(find)=0 to 1; documentation++) |
||
Line 45: | Line 45: | ||
// rolling window approach that results in the replacement of occurrences | // rolling window approach that results in the replacement of occurrences | ||
// that existed in the original text (like the built-in replace() function) | // that existed in the original text (like the built-in replace() function) | ||
// as well as any new occurrences that emerge as a result of the | // as well as (when length of find >= length of replace) any new occurrences | ||
// replacements | // that emerge as a result of the replacements made (the length limitation | ||
// prevents an infinite number of replacements being called for). | |||
// (Substitutes for built-in replacerepeat(), which is not working.) | |||
function('replacegreedy', _ | function('replacegreedy', _ | ||
set('replacegreedy_casesensitive', True) + _ | set('replacegreedy_casesensitive', True) + _ | ||
Line 63: | Line 64: | ||
// -- Do not call directly; called from replacegreedy() / replacegreedyi() | // -- Do not call directly; called from replacegreedy() / replacegreedyi() | ||
function('replacegreedy_internal', _ | function('replacegreedy_internal', _ | ||
set('replacegreedy_find_len', length(param2)) + _ | set('replacegreedy_find_len', length(param2)) + _ | ||
if( | if(replacegreedy_find_len = 0, _ | ||
param1, _ | param1, _ | ||
if(replacegreedy_find_len < length(param3), _ | |||
// optimize cases where standard replace()/replacei() will give the same result. | |||
if(replacegreedy_casesensitive, _ | |||
replace(param1, param2, param3), _ | |||
replacei(param1, param2, param3) _ | |||
) | ), _ | ||
set(' | // begin greedy algorithm | ||
set('replacegreedy_text_len', length(param1)) + _ | |||
if((replacegreedy_text_len < replacegreedy_find_len) OR (replacegreedy_text_len = 0), _ | |||
param1, _ | |||
set('replacegreedy_new_text', _ | set('replacegreedy_new_text', _ | ||
if(replacegreedy_new_text_len <> replacegreedy_find_len, _ | if(replacegreedy_find_len = 1, '', limit(param1, replacegreedy_find_len - 1)) _ | ||
) + _ | |||
for('replacegreedy_text_pos', replacegreedy_find_len, replacegreedy_text_len, _ | |||
set('replacegreedy_new_text', _ | |||
replacegreedy_new_text + _ | |||
copy(param1, replacegreedy_text_pos, 1) _ | |||
) + _ | |||
set('replacegreedy_new_text_len', length(replacegreedy_new_text)) + _ | |||
set('replacegreedy_text_found', _ | |||
copy(replacegreedy_new_text, _ | |||
(replacegreedy_new_text_len - replacegreedy_find_len) + 1, _ | |||
replacegreedy_find_len _ | |||
) _ | |||
) + _ | |||
if(if(replacegreedy_casesensitive, _ | |||
start(replacegreedy_text_found, param2), _ | |||
starti(replacegreedy_text_found, param2) _ | |||
), _ | |||
set('replacegreedy_new_text', _ | |||
if(replacegreedy_new_text_len <> replacegreedy_find_len, _ | |||
limit(replacegreedy_new_text, _ | |||
replacegreedy_new_text_len - replacegreedy_find_len _ | |||
) + param3, _ | |||
replacegreedy_text_found _ | |||
) _ | |||
) _ | |||
) _ | ) _ | ||
) _ | ) + _ | ||
replacegreedy_new_text _ | |||
) _ | ) _ | ||
) | ) _ | ||
) _ | ) _ | ||
) | ) |
edits