User:Belove/AOM Sample Rename Patterns: Difference between revisions

From AniDB
Jump to navigation Jump to search
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 made. (Substitutes for built-in replacerepeat(), which is
//    that emerge as a result of the replacements made (the length limitation
//    not working.)
//    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_text_len', length(param1)) + _
set('replacegreedy_find_len', length(param2)) + _
set('replacegreedy_find_len', length(param2)) + _
if((replacegreedy_text_len < replacegreedy_find_len) OR (replacegreedy_text_len = 0), _
if(replacegreedy_find_len = 0, _
param1, _
param1, _
set('replacegreedy_new_text', limit(param1, replacegreedy_find_len - 1)) + _
if(replacegreedy_find_len < length(param3), _
for('replacegreedy_text_pos', replacegreedy_find_len, replacegreedy_text_len, _
// optimize cases where standard replace()/replacei() will give the same result.
set('replacegreedy_new_text', _
if(replacegreedy_casesensitive, _
replacegreedy_new_text + _
replace(param1, param2, param3), _
copy(param1, replacegreedy_text_pos, 1) _
replacei(param1, param2, param3) _
) + _
), _
set('replacegreedy_new_text_len', length(replacegreedy_new_text)) + _
// begin greedy algorithm
set('replacegreedy_text_found', _
set('replacegreedy_text_len', length(param1)) + _
copy(replacegreedy_new_text, _
if((replacegreedy_text_len < replacegreedy_find_len) OR (replacegreedy_text_len = 0), _
(replacegreedy_new_text_len - replacegreedy_find_len) + 1, _
param1, _
replacegreedy_find_len _
) _
) + _
if(if(replacegreedy_casesensitive, _
start(replacegreedy_text_found, param2), _
starti(replacegreedy_text_found, param2) _
), _
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)) _
limit(replacegreedy_new_text, _
) + _
replacegreedy_new_text_len - replacegreedy_find_len _
for('replacegreedy_text_pos', replacegreedy_find_len, replacegreedy_text_len, _
) + param3, _
set('replacegreedy_new_text', _
replacegreedy_text_found _
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 _
) _
) _
) + _
) _
replacegreedy_new_text _
) _
) _
)
)

Revision as of 01:16, 18 January 2016

AOM Sample Rename Patterns

Tested with AOM 0.5.18.276


replacerepeat_()

Recursive replacement for replacerepeat()

replacerepeat_(text, find, replace)

// replacerepeat_(text, find, replace)
//     -- Repeatedly replaces 'find' in 'text' with 'replace', until 'text'
//     stops changing.  (Substitutes for built-in replacerepeat(), which is not
//     working.)
function('replacerepeat_', _
	set('replacerepeat_new_text', _
		replace(param1, param2, param3) _
	) + _
	if(replacerepeat_new_text<>param1, _
		replacerepeat_(replacerepeat_new_text, param2, param3), _
		replacerepeat_new_text _
	) _
)
Example usage Output
replacerepeat_('1a2aa5aaaaa8aAaAaAaA', 'aa', 'a')
1a2a5a8aAaAaAaA
replacerepeat_('[1 2  5     8        ]', '  ', ' ')
[1 2 5 8 ]

replacegreedy() / replacegreedyi()

Non-recursive, single-pass replacements for replacerepeat().

replacegreedy(text, find, replace)

replacegreedyi(text, find, replace) case-insensitive version

// replacegreedy(text, find, replace)
//     -- Replaces all occurrences of 'find' in 'text' with 'replace' using a
//     rolling window approach that results in the replacement of occurrences
//     that existed in the original text (like the built-in replace() function)
//     as well as (when length of find >= length of replace) any new occurrences
//     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', _
	set('replacegreedy_casesensitive', True) + _
	replacegreedy_internal(param1, param2, param3) _
)

// replacegreedyi(text, find, replace)
//     -- Case-insensitive version of replacegreedy()
function('replacegreedyi', _
	set('replacegreedy_casesensitive', False) + _
	replacegreedy_internal(param1, param2, param3) _
)

// replacegreedy_internal(text, find, replace)
//     -- Do not call directly; called from replacegreedy() / replacegreedyi()
function('replacegreedy_internal', _
	set('replacegreedy_find_len', length(param2)) + _
	if(replacegreedy_find_len = 0, _
		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) _
			), _
			// 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', _
					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 _
			) _
		) _
	) _
)
Example usage Output
replacegreedy('1a2aa5aaaaa8aAaAaAaA', 'aa', 'a')
1a2a5a8aAaAaAaA
replacegreedyi('1a2aa5aaaaa8aAaAaAaA', 'aa', 'a')
1a2a5a8a
replacegreedy('[1 2  5     8        ]', '  ', ' ')
[1 2 5 8 ]