User:Belove/AOM Sample Rename Patterns

From AniDB
Revision as of 23:16, 17 January 2016 by Belove (talk | contribs) (→‎replacegreedy() / replacegreedyi(): slightly more efficient)
Jump to navigation Jump to search

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 any new occurrences that emerge as a result of the
//     replacements made.  (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_text_len', length(param1)) + _
	set('replacegreedy_find_len', length(param2)) + _
	if((replacegreedy_text_len < replacegreedy_find_len) OR (replacegreedy_text_len = 0), _
		param1, _
		set('replacegreedy_new_text', 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 ]