WordReplace: Difference between revisions
From NSIS Wiki
Jump to navigationJump to search
m (Updated author links.) |
m (Added category links.) |
||
Line 286: | Line 286: | ||
FunctionEnd | FunctionEnd | ||
</highlight-nsis> | </highlight-nsis> | ||
[[{{ns:14}}:String Functions]] |
Revision as of 22:01, 30 April 2005
Author: Instructor (talk, contrib) |
Links
- Latest version of headers "nsh.zip"
- http://forums.winamp.com/showthread.php?s=&threadid=203228&goto=lastpost
The Function
/* ______________________________________________________________________________ WordReplace v2.0 (replace or delete) ______________________________________________________________________________ 2004 Shengalts Aleksander (Shengalts@mail.ru) Replace or delete word from string. Syntax: Push "[string]" ;[string] ; input string Push "[word1]" ;[word1] ; word to replace or delete Push "[word2]" ;[word2] ; replace with (if empty delete) Push "[E][options]" ;[options] ; +number : word number from start ; -number : word number from end ; +number* : word number from start multiple-replace ; -number* : word number from end multiple-replace ; + : replace or delete all founded ; +* : multiple-replace all founded ; {} : if exists replace or delete all delimiters ; from edges (no errorlevel output) ; {}* : if exists multiple-replace all delimiters ; from edges (no errorlevel output) ; ;[E] ; with errorlevel output ; IfErrors: ; $var=1 word to replace or delete not found ; $var=2 no such word number ; $var=3 syntax error ; (Use: +1,-1,+1*,-1*,+,+*,{},{}*) ;[] ; no errorlevel output (default) ; If some errors found then (result=input string) ; Call WordReplace ;call function ; Pop $var ;output (result) Example (replace): Section Push "C:\io.sys C:\logo.sys C:\WINDOWS" Push "SYS" Push "bmp" Push "+2" Call WordReplace Pop $R0 ;now contain: "C:\io.sys C:\logo.bmp C:\WINDOWS" SectionEnd Example (delete): Section Push "C:\io.sys C:\logo.sys C:\WINDOWS" Push "SYS" Push "" Push "+" Call WordReplace Pop $R0 ;now contain: "C:\io. C:\logo. C:\WINDOWS" SectionEnd Example (multiple-replace 1): Section Push "C:\io.sys C:\logo.sys C:\WINDOWS" Push " " Push " " Push "+1*" ; +1* or +2* or +3* or +4* or +5* or +6* Call WordReplace Pop $R0 ;now contain: "C:\io.sys C:\logo.sys C:\WINDOWS" SectionEnd Example (multiple-replace 2): Section Push "C:\io.sys C:\logo.sysSYSsys C:\WINDOWS" Push "sys" Push "bmp" Push "+*" Call WordReplace Pop $R0 ;now contain: "C:\io.bmp C:\logo.bmp C:\WINDOWS" SectionEnd Example (multiple-replace 3): Section Push "sysSYSsysC:\io.sys C:\logo.sys C:\WINDOWSsysSYSsys" Push "sys" Push "|" Push "{}*" Call WordReplace Pop $R0 ;now contain: "|C:\io.sys C:\logo.sys C:\WINDOWS|" SectionEnd Example (With errorlevel output): Section Push "C:\io.sys C:\logo.sys" Push "sys" Push "bmp" Push "E+3" Call WordReplace Pop $R0 ;now contain: "2" (no such word number "+3") IfErrors 0 noerrors MessageBox MB_OK 'Errorlevel=$R0' IDOK end noerrors: MessageBox MB_OK 'No errors' end: SectionEnd*/ ;--------------------------------------------------------------------------- Function WordReplace Exch $2 Exch Exch $1 Exch Exch 2 Exch $0 Exch 2 Exch 3 Exch $R0 Exch 3 Push $3 Push $4 Push $5 Push $6 Push $7 Push $8 Push $9 Push $R1 ClearErrors StrCpy $R1 $R0 StrCpy $9 '' StrCpy $3 $2 1 StrCmp $3 'E' 0 +4 StrCpy $9 E StrCpy $2 $2 '' 1 goto -4 StrLen $7 $0 StrCpy $4 $2 3 StrCpy $5 $2 2 StrCmp $4 '{}*' +3 StrCmp $5 '{}' +2 goto errorchk StrCmp $7 0 end StrCpy $5 '' StrCpy $6 '' StrCpy $3 $R0 $7 StrCmp $3 $0 0 +4 StrCpy $R0 $R0 '' $7 StrCpy $5 '$1$5' goto -4 StrCpy $3 $R0 '' -$7 StrCmp $3 $0 0 +4 StrCpy $R0 $R0 -$7 StrCpy $6 '$6$1' goto -4 StrCmp $4 '{}*' 0 +5 StrCmp $5 '' +2 StrCpy $5 $1 StrCmp $6 '' +2 StrCpy $6 $1 StrCpy $R0 '$5$R0$6' goto end errorchk: StrCpy $3 $2 1 StrCpy $2 $2 '' 1 StrCmp $3 '+' +2 StrCmp $3 '-' 0 error3 StrCmp $R0 '' error1 StrCmp $7 0 error1 StrCpy $4 $2 1 -1 StrCpy $5 $2 1 IntOp $2 $2 + 0 StrCmp $2 0 0 one StrCmp $5 0 error2 StrCpy $3 '' all: StrCpy $5 0 StrCpy $2 $R0 $7 $5 StrCmp $2$3 '' error1 StrCmp $2 '' +4 StrCmp $2 $0 +5 IntOp $5 $5 + 1 goto -5 StrCpy $R0 '$3$R0' goto end StrCpy $2 $R0 $5 IntOp $5 $5 + $7 StrCmp $4 '*' 0 +3 StrCpy $6 $R0 $7 $5 StrCmp $6 $0 -3 StrCpy $R0 $R0 '' $5 StrCpy $3 '$3$2$1' goto all one: StrCpy $5 0 StrCpy $8 0 goto loop preloop: IntOp $5 $5 + 1 loop: StrCpy $6 $R0 $7 $5 StrCmp $6$8 0 error1 StrCmp $6 '' minus StrCmp $6 $0 0 preloop IntOp $8 $8 + 1 StrCmp $3$8 +$2 found IntOp $5 $5 + $7 goto loop minus: StrCmp $3 '-' 0 error2 StrCpy $3 + IntOp $2 $8 - $2 IntOp $2 $2 + 1 IntCmp $2 0 error2 error2 one found: StrCpy $3 $R0 $5 StrCmp $4 '*' 0 +5 StrCpy $6 $3 '' -$7 StrCmp $6 $0 0 +3 StrCpy $3 $3 -$7 goto -3 IntOp $5 $5 + $7 StrCmp $4 '*' 0 +3 StrCpy $6 $R0 $7 $5 StrCmp $6 $0 -3 StrCpy $R0 $R0 '' $5 StrCpy $R0 '$3$1$R0' goto end error3: StrCpy $R0 3 goto error error2: StrCpy $R0 2 goto error error1: StrCpy $R0 1 error: StrCmp $9 'E' +3 StrCpy $R0 $R1 goto +2 SetErrors end: Pop $R1 Pop $9 Pop $8 Pop $7 Pop $6 Pop $5 Pop $4 Pop $3 Pop $2 Pop $1 Pop $0 Exch $R0 FunctionEnd