All pastes #813515 Raw Edit

Anonymous

public text v1 · immutable
·published 2007-12-12 16:22 UTC
rendered paste body
;╔══════════════════════════════════════════════════════════════════════════════╗
;║ An generic macro that replaces characters in a string(depreciated)	  MACRO ║
;╟──────────────────────────────────────────────────────────────────────────────╢
;║ SET VARS:									║
;║ 	RETURN_STRING								║
;║ 										║
;║ USED VARS:									║
;║ 	none									║
;║ 										║
;║ DEPENDS:									║
;║	none									║
;║ 										║
;║ PARAMS:									║
;║ 	ARG1 - String to operate on						║
;║ 	ARG2 - Argument to find							║
;║ 	ARG3 - Argument to replace						║
;╚══════════════════════════════════════════════════════════════════════════════╝
[macro-findReplace]
	;────────────────────────────────────────────────────────────────────────────────────────────────────
	;Set up the initial values
	;────────────────────────────────────────────────────────────────────────────────────────────────────
	exten => s,1,Set(i=1)
	
	;────────────────────────────────────────────────────────────────────────────────────────────────────
	;Cut string by find argument, occurance {i} 
	;────────────────────────────────────────────────────────────────────────────────────────────────────
	exten => s,n(REPEAT),Set(RESULT=${CUT(ARG1|${ARG2}|${i})})
	exten => s,n,GotoIf($["${RESULT}" = ""]?DONE)

	;────────────────────────────────────────────────────────────────────────────────────────────────────
	;If cut returned a result combine both halfs with replace argument in middle
	;────────────────────────────────────────────────────────────────────────────────────────────────────
	exten => s,n,Set(__FR_STRING=${FR_STRING}${ARG3}${RESULT})

	;────────────────────────────────────────────────────────────────────────────────────────────────────
	;Increment {i} and test again
	;────────────────────────────────────────────────────────────────────────────────────────────────────
	exten => s,n,Set(i=$[${i} + 1]) 
	exten => s,n,Goto(REPEAT)

	;────────────────────────────────────────────────────────────────────────────────────────────────────
	;If there where no more occurances of find argument, remove leading character (erronous replace)
	;and set result to RETURN_STRING
	;────────────────────────────────────────────────────────────────────────────────────────────────────
	exten => s,n(DONE),Set(__RETURN_STRING=${FR_STRING:1})
	exten => s,n,Set(MACRO_RESULT=)