StrRep: Difference between revisions
From NSIS Wiki
Jump to navigationJump to search
m (Corrected link to LogicLib header file.) |
No edit summary |
||
Line 1: | Line 1: | ||
{{PageAuthor| | {{PageAuthor|dandaman32}} | ||
== Description == | == Description == | ||
''' | '''Version: 3.0''' | ||
This function searches and replaces all occurrences of a substring in a string. | This function searches and replaces all occurrences of a substring in a string. | ||
Line 13: | Line 11: | ||
=== Syntax === | === Syntax === | ||
; Single command | |||
: This is by far the easiest way to use StrReplace. | |||
<highlight-nsis> | <highlight-nsis> | ||
${ | ${StrReplace} '$0' '\' '_' 'C:\Documents and Settings\Dan\Desktop\PSCP Frontend.exe' | ||
MessageBox MB_OK $0 ; will be C:_Documents and Settings_Dan_Desktop_PSCP Frontend.exe | |||
</highlight-nsis> | </highlight-nsis> | ||
; Push/function call | |||
: For when you're used to NSIS :) | |||
<highlight-nsis> | <highlight-nsis> | ||
Push " | Push "old" | ||
Push " | Push "brand new" | ||
Push " | Push "Shame on you, you broke your old needle!" | ||
Call | Call StrReplace | ||
Pop " | Pop $0 | ||
MessageBox MB_OK $0 ; will be "Shame on you, you broke your brand new needle!" | |||
</highlight-nsis> | </highlight-nsis> | ||
; Usage | |||
: ${StrReplace} "$result_var" "SubString" "RepString" "String" | |||
=== Parameters === | === Parameters === | ||
; | ; $result_var | ||
: Variable where resulting operation of the replacement is returned. If ''SubString'' is not found, the value is the same as ''String''. | : Variable where resulting operation of the replacement is returned. If ''SubString'' is not found, the value is the same as ''String''. | ||
Line 42: | Line 49: | ||
<highlight-nsis> | <highlight-nsis> | ||
${ | ${StrReplace} $0 "just " "" "This is just an example" | ||
;$0 = "This is an example" | ;$0 = "This is an example" | ||
</highlight-nsis> | </highlight-nsis> | ||
Line 49: | Line 56: | ||
<highlight-nsis> | <highlight-nsis> | ||
; StrReplace | |||
; Replaces all ocurrences of a given needle within a haystack with another string | |||
; Written by dandaman32 | |||
Var STR_REPLACE_VAR_0 | |||
Var STR_REPLACE_VAR_1 | |||
Var STR_REPLACE_VAR_2 | |||
Var STR_REPLACE_VAR_3 | |||
Var STR_REPLACE_VAR_4 | |||
Var STR_REPLACE_VAR_5 | |||
Var STR_REPLACE_VAR_6 | |||
Var STR_REPLACE_VAR_7 | |||
Var STR_REPLACE_VAR_8 | |||
Function StrReplace | |||
Exch $ | Exch $STR_REPLACE_VAR_2 | ||
Exch | Exch 1 | ||
Exch $ | Exch $STR_REPLACE_VAR_1 | ||
Exch 2 | Exch 2 | ||
Exch $ | Exch $STR_REPLACE_VAR_0 | ||
StrCpy $STR_REPLACE_VAR_3 -1 | |||
StrLen $STR_REPLACE_VAR_4 $STR_REPLACE_VAR_1 | |||
StrLen $STR_REPLACE_VAR_6 $STR_REPLACE_VAR_0 | |||
loop: | |||
IntOp $STR_REPLACE_VAR_3 $STR_REPLACE_VAR_3 + 1 | |||
StrCpy $STR_REPLACE_VAR_5 $STR_REPLACE_VAR_0 $STR_REPLACE_VAR_4 $STR_REPLACE_VAR_3 | |||
StrCmp $STR_REPLACE_VAR_5 $STR_REPLACE_VAR_1 found | |||
StrCmp $STR_REPLACE_VAR_3 $STR_REPLACE_VAR_6 done | |||
Goto loop | |||
found: | |||
StrCpy $STR_REPLACE_VAR_5 $STR_REPLACE_VAR_0 $STR_REPLACE_VAR_3 | |||
IntOp $STR_REPLACE_VAR_8 $STR_REPLACE_VAR_3 + $STR_REPLACE_VAR_4 | |||
StrCpy $STR_REPLACE_VAR_7 $STR_REPLACE_VAR_0 "" $STR_REPLACE_VAR_8 | |||
StrCpy $STR_REPLACE_VAR_0 $STR_REPLACE_VAR_5$STR_REPLACE_VAR_2$STR_REPLACE_VAR_7 | |||
StrLen $STR_REPLACE_VAR_6 $STR_REPLACE_VAR_0 | |||
Goto loop | |||
done: | |||
Pop $STR_REPLACE_VAR_1 ; Prevent "invalid opcode" errors and keep the | |||
Pop $STR_REPLACE_VAR_1 ; stack as it was before the function was called | |||
Exch $STR_REPLACE_VAR_0 | |||
FunctionEnd | |||
!macro _strReplaceConstructor OUT NEEDLE NEEDLE2 HAYSTACK | |||
${ | Push "${NEEDLE}" | ||
Push "${NEEDLE2}" | |||
Push "${HAYSTACK}" | |||
Call StrReplace | |||
Pop "${OUT}" | |||
!macroend | |||
!define StrReplace '!insertmacro "_strReplaceConstructor"' | |||
</highlight-nsis> | </highlight-nsis> | ||
== Versions History == | == Versions History == | ||
; 3.0 | |||
: Updated by dandaman32 - uses less code and doesn't depend on LogicLib | |||
: Also: changed macro syntax to work like the PHP eqivalent | |||
;2.0.1 | ;2.0.1 | ||
Line 152: | Line 119: | ||
== Credits == | == Credits == | ||
Version 3.x - Dan Fuhry ([[User:dandaman32]]).<br> | |||
Version 2.x - Diego Pedroso ([[User:deguix|deguix]]).<br> | Version 2.x - Diego Pedroso ([[User:deguix|deguix]]).<br> | ||
Version 1.x [[User:Afrow UK|Afrow UK]] / Diego Pedroso ([[User:deguix|deguix]]).<br> | Version 1.x [[User:Afrow UK|Afrow UK]] / Diego Pedroso ([[User:deguix|deguix]]).<br> |
Revision as of 07:27, 18 March 2006
Author: dandaman32 (talk, contrib) |
Description
Version: 3.0
This function searches and replaces all occurrences of a substring in a string.
How To Use
Syntax
- Single command
- This is by far the easiest way to use StrReplace.
${StrReplace} '$0' '\' '_' 'C:\Documents and Settings\Dan\Desktop\PSCP Frontend.exe' MessageBox MB_OK $0 ; will be C:_Documents and Settings_Dan_Desktop_PSCP Frontend.exe
- Push/function call
- For when you're used to NSIS :)
Push "old" Push "brand new" Push "Shame on you, you broke your old needle!" Call StrReplace Pop $0 MessageBox MB_OK $0 ; will be "Shame on you, you broke your brand new needle!"
- Usage
- ${StrReplace} "$result_var" "SubString" "RepString" "String"
Parameters
- $result_var
- Variable where resulting operation of the replacement is returned. If SubString is not found, the value is the same as String.
- String
- String where to search for SubString.
- SubString
- String to search in String and to be replaced by RepString.
- RepString
- String to replace all occurrences of SubString inside String.
Example
${StrReplace} $0 "just " "" "This is just an example" ;$0 = "This is an example"
Function Code
; StrReplace ; Replaces all ocurrences of a given needle within a haystack with another string ; Written by dandaman32 Var STR_REPLACE_VAR_0 Var STR_REPLACE_VAR_1 Var STR_REPLACE_VAR_2 Var STR_REPLACE_VAR_3 Var STR_REPLACE_VAR_4 Var STR_REPLACE_VAR_5 Var STR_REPLACE_VAR_6 Var STR_REPLACE_VAR_7 Var STR_REPLACE_VAR_8 Function StrReplace Exch $STR_REPLACE_VAR_2 Exch 1 Exch $STR_REPLACE_VAR_1 Exch 2 Exch $STR_REPLACE_VAR_0 StrCpy $STR_REPLACE_VAR_3 -1 StrLen $STR_REPLACE_VAR_4 $STR_REPLACE_VAR_1 StrLen $STR_REPLACE_VAR_6 $STR_REPLACE_VAR_0 loop: IntOp $STR_REPLACE_VAR_3 $STR_REPLACE_VAR_3 + 1 StrCpy $STR_REPLACE_VAR_5 $STR_REPLACE_VAR_0 $STR_REPLACE_VAR_4 $STR_REPLACE_VAR_3 StrCmp $STR_REPLACE_VAR_5 $STR_REPLACE_VAR_1 found StrCmp $STR_REPLACE_VAR_3 $STR_REPLACE_VAR_6 done Goto loop found: StrCpy $STR_REPLACE_VAR_5 $STR_REPLACE_VAR_0 $STR_REPLACE_VAR_3 IntOp $STR_REPLACE_VAR_8 $STR_REPLACE_VAR_3 + $STR_REPLACE_VAR_4 StrCpy $STR_REPLACE_VAR_7 $STR_REPLACE_VAR_0 "" $STR_REPLACE_VAR_8 StrCpy $STR_REPLACE_VAR_0 $STR_REPLACE_VAR_5$STR_REPLACE_VAR_2$STR_REPLACE_VAR_7 StrLen $STR_REPLACE_VAR_6 $STR_REPLACE_VAR_0 Goto loop done: Pop $STR_REPLACE_VAR_1 ; Prevent "invalid opcode" errors and keep the Pop $STR_REPLACE_VAR_1 ; stack as it was before the function was called Exch $STR_REPLACE_VAR_0 FunctionEnd !macro _strReplaceConstructor OUT NEEDLE NEEDLE2 HAYSTACK Push "${NEEDLE}" Push "${NEEDLE2}" Push "${HAYSTACK}" Call StrReplace Pop "${OUT}" !macroend !define StrReplace '!insertmacro "_strReplaceConstructor"'
Versions History
- 3.0
- Updated by dandaman32 - uses less code and doesn't depend on LogicLib
- Also: changed macro syntax to work like the PHP eqivalent
- 2.0.1
- Fixed stack problems.
Credits
Version 3.x - Dan Fuhry (User:dandaman32).
Version 2.x - Diego Pedroso (deguix).
Version 1.x Afrow UK / Diego Pedroso (deguix).
Version 1.0 - Hendri Adriaens (Smile2Me)