StrRep: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
(See "versions history" heading)
Line 59: Line 59:
; Replaces all ocurrences of a given needle within a haystack with another string
; Replaces all ocurrences of a given needle within a haystack with another string
; Written by dandaman32
; Written by dandaman32
 
''''''Bold text'''''''Italic text'''''''
Var STR_REPLACE_VAR_0
Var STR_REPLACE_VAR_0
Var STR_REPLACE_VAR_1
Var STR_REPLACE_VAR_1

Revision as of 18:54, 25 January 2007

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
''''''Bold text'''''''Italic text'''''''
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 "${HAYSTACK}"
  Push "${NEEDLE}"
  Push "${NEEDLE2}"
  Call StrReplace
  Pop "${OUT}"
!macroend
 
!define StrReplace '!insertmacro "_strReplaceConstructor"'

Versions History

3.0.1
Fixed bug where macro pushed strings in the wrong order
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 (dandaman32).
Version 2.x - Diego Pedroso (deguix).
Version 1.x Afrow UK / Diego Pedroso (deguix).
Version 1.0 - Hendri Adriaens (Smile2Me)