Sort String 2

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


Description

This is a re-written version of the String Sort function. This has a new part to it which is not in the previous function (String Sort 1). Note: Words are referred to as 'chunks'

Usage

Push 4 ; get text after 4th chunk from string end
 Push 2 ; get 2 chunks before the 4th
  Push "Hello all, I am Afrow UK!!!" ; input string
    Call AdvStrSort
  Pop $R0

$R0 will now be: "I am"

Note: You can also Push higher chunk counts than there actually are in the string, and nothing will go wrong or be affected.

Usage 2

Push 10 ; get text after 10th chunk from string end
 Push 2 ; get 2 chunks before the 10th
  Push "$R1" ; input string (unknown string, but has spaces)
    Call AdvStrSort
  Pop $R0

$R0 will now be the first two chunks of the string, even though there could have been only 5 chunks in the unknown string.

The Function

Function AdvStrSort
  Exch $0 ; input string
  Exch
  Exch $1 ; count to get part
  Exch
  Exch 2
  Exch $2 ; get ammount of chunks from end
  Exch 2
  Push $3
  Push $4
  Push $5
  Push $6
  Push $7
  StrCpy $0 " $0"
  StrCpy $3 0
  StrCpy $4 0
  loop:
    IntOp $3 $3 - 1
    StrCpy $6 $0 1 $3
    StrCmp $6 "" skip
    StrCmp $6 " " roger ; to change chunk seperators, edit this (" ")
    Goto loop
  roger:
    StrCpy $7 $0 "" $3
    IntOp $4 $4 + 1
    StrCmp $4 $2 0 loop
    StrCmp $1 $2 0 +3
    StrCpy $0 $7
    Goto end
  skip:
    StrCpy $4 0
    StrLen $5 $7
  top:
    IntOp $4 $4 + 1
  loop2:
    IntOp $5 $5 - 1
    StrCpy $3 $7 1 -$5
    StrCmp $3 "" end
    StrCmp $3 " " 0 loop2 ; to change chunk seperators, edit this too (" ")
    StrCmp $4 $1 0 top
    StrCpy $0 $7 -$5
  end:
    StrLen $1 $0
    IntOp $1 $1 - 1
    StrCpy $0 $0 $1 -$1
  Pop $7
  Pop $6
  Pop $5
  Pop $4
  Pop $3
  Pop $2 
  Pop $1
  Exch $0 ; output string
FunctionEnd

-Stu