StrRev

From NSIS Wiki
Jump to navigationJump to search
Author: Lloigor (talk, contrib)


Description

Reverse a string.

Usage

${StrRev} $StrVar "String"   ;; $StrVar="gnirtS"

Function

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Reverse a string
;; P1 :o: Reversed string
;; P2 :i: Original string
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
!define StrRev "!insertmacro _StrRev"
!macro _StrRev _REV_ _STR_
   Push "${_STR_}"
   Call StrRev
   Pop ${_REV_}
!macroend
 
Function StrRev
   Exch $1  ;; Orig string
   Push $0  ;; Reversed string
   Exch
   Push $2  ;; String Len / Counter
   Push $3  ;; Current character
 
   StrCpy $0 ""
   StrLen $2 "$1"
 
   IntCmp $2 0 +5
      IntOp $2 $2 - 1
      StrCpy $3 "$1" 1 $2
      StrCpy $0 "$0$3"
      Goto -4
 
   Pop $3
   Pop $2
   Pop $1
   Exch $0
FunctionEnd