Get last part and rest of string: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
No edit summary
 
(8 intermediate revisions by 3 users not shown)
Line 1: Line 1:
This function has a bug, if it is fed an empty input string it goes into an infinite loop.
{{PageAuthor|Afrow UK}}


== Description ==
This function returns the parts before and after a sub string in a string. If the sub string is not found within the string then the return values are empty.


{{PageAuthor|Afrow UK}}
'''14th July 2007''': Fixed infinite loop on empty string and changed divider character to divider string.
 
== Bug ==
larsm: That function does not work: It don't "Get last part and rest of string" it get first part and rest of string. Input: "aaa\\bbb\\ccc\\ddd" --> Output: "aaa" and "bbb\\ccc\\ddd"!
 
well, this works, but it only returns the rest, and not the last:
("aaa\bbb\ccc\ddd" --> Output: and "aaa\bbb\ccc")
 
Puts the result in $R0


== Description ==
Function RemoveLastDirectoryFromPath
This function returns the last part of a string after a certain character and the rest of the string before that character. If the character is not found within the string, then the function returns two empty strings.
  Exch $R0
  Push $R1
  Push $R2
  StrLen $R1 $R0
  ;IntOp $R1 $R1 + 1
  loop:
    IntOp $R1 $R1 - 1
    StrCpy $R2 $R0 1 $R1
    StrCmp $R2 ":" exit2
    StrCmp $R2 "\" exit1
  Goto loop
  exit1:
    StrCpy $R0 $R0 $R1
  exit2:
    Pop $R2
    Pop $R1
    Exch $R0
FunctionEnd


== Usage ==
== Usage ==
<highlight-nsis>
<highlight-nsis>
Push " " ; divider char
Push "o " ; divider str
Push "hello my" ; input string
Push "hello my" ; input string
  Call GetLastPart
  Call GetLastPart
Pop $R1 ; last part "my"
Pop $R1 ; last part "my"
Pop $R0 ; first part "hello"
Pop $R0 ; first part "hell"
</highlight-nsis>
</highlight-nsis>


Line 21: Line 48:
Exch $R0 ; input
Exch $R0 ; input
Exch
Exch
Exch $R1 ; character
Exch $R1 ; divider str
Push $R2
Push $R2
Push $R3
Push $R3
Push $R4
Push $R4
Push $R5


  StrCpy $R2 0
  StrCpy $R2 -1
  StrLen $R4 $R0
  StrLen $R4 $R0
StrLen $R5 $R1
  Loop:
  Loop:
   IntOp $R2 $R2 - 1
   IntOp $R2 $R2 + 1
   StrCpy $R3 $R0 1 $R2
   StrCpy $R3 $R0 $R5 $R2
   StrCmp $R3 $R1 Chop
   StrCmp $R3 $R1 Chop
   StrCmp $R2 -$R4 0 Loop
   StrCmp $R2 $R4 0 Loop
   StrCpy $R0 ""
   StrCpy $R0 ""
   StrCpy $R1 ""
   StrCpy $R1 ""
Line 38: Line 67:
  Chop:
  Chop:
   StrCpy $R1 $R0 $R2
   StrCpy $R1 $R0 $R2
   IntOp $R2 $R2 + 1
   IntOp $R2 $R2 + $R5
   StrCpy $R0 $R0 "" $R2
   StrCpy $R0 $R0 "" $R2
  Done:
  Done:


Pop $R5
Pop $R4
Pop $R4
Pop $R3
Pop $R3

Latest revision as of 15:45, 11 October 2009

Author: Afrow UK (talk, contrib)


Description

This function returns the parts before and after a sub string in a string. If the sub string is not found within the string then the return values are empty.

14th July 2007: Fixed infinite loop on empty string and changed divider character to divider string.

Bug

larsm: That function does not work: It don't "Get last part and rest of string" it get first part and rest of string. Input: "aaa\\bbb\\ccc\\ddd" --> Output: "aaa" and "bbb\\ccc\\ddd"!

well, this works, but it only returns the rest, and not the last: ("aaa\bbb\ccc\ddd" --> Output: and "aaa\bbb\ccc")

Puts the result in $R0

Function RemoveLastDirectoryFromPath

 Exch $R0
 Push $R1
 Push $R2
 StrLen $R1 $R0
 ;IntOp $R1 $R1 + 1
 loop:
   IntOp $R1 $R1 - 1
   StrCpy $R2 $R0 1 $R1
   StrCmp $R2 ":" exit2
   StrCmp $R2 "\" exit1 
 Goto loop
 exit1:
   StrCpy $R0 $R0 $R1
 exit2:
   Pop $R2
   Pop $R1
   Exch $R0

FunctionEnd

Usage

Push "o " ; divider str
Push "hello my" ; input string
 Call GetLastPart
Pop $R1 ; last part "my"
Pop $R0 ; first part "hell"

The Function

Function GetLastPart
Exch $R0 ; input
Exch
Exch $R1 ; divider str
Push $R2
Push $R3
Push $R4
Push $R5
 
 StrCpy $R2 -1
 StrLen $R4 $R0
 StrLen $R5 $R1
 Loop:
  IntOp $R2 $R2 + 1
  StrCpy $R3 $R0 $R5 $R2
  StrCmp $R3 $R1 Chop
  StrCmp $R2 $R4 0 Loop
   StrCpy $R0 ""
   StrCpy $R1 ""
   Goto Done
 Chop:
  StrCpy $R1 $R0 $R2
  IntOp $R2 $R2 + $R5
  StrCpy $R0 $R0 "" $R2
 Done:
 
Pop $R5
Pop $R4
Pop $R3
Pop $R2
Exch $R1 ; before
Exch
Exch $R0 ; after
FunctionEnd