StrTrimNewLines

From NSIS Wiki

Author: deguix (talk, contrib)


Description

Requires: LogicLib header file.

Version: 2.0.1.

This function removes unnecessary new lines at end of a string.

How To Use

Syntax

${StrTrimNewLines} "ResultVar" "String"

or

Push "String"
Call StrTrimNewLines
Pop "ResultVar"

Parameters

ResultVar
Variable where the string without the new line characters at the end is returned. If there is no new line characters, the return value is the same as specified in String parameter.
String
String where the new line characters at its end are removed.

Example

${StrTrimNewLines} $0 "This is just an example$\r$\n$\r$\n"
;$0 = "This is just an example"

Function Code

!define StrTrimNewLines "!insertmacro StrTrimNewLines"
 
!macro StrTrimNewLines ResultVar String
  Push "${String}"
  Call StrTrimNewLines
  Pop "${ResultVar}"
!macroend
 
Function StrTrimNewLines
/*After this point:
  ------------------------------------------
  $R0 = String (input)
  $R1 = TrimCounter (temp)
  $R2 = Temp (temp)*/
 
  ;Get input from user
  Exch $R0
  Push $R1
  Push $R2
 
  ;Initialize trim counter
  StrCpy $R1 0
 
  loop:
  ;Subtract to get "String"'s last characters
  IntOp $R1 $R1 - 1
 
  ;Verify if they are either $\r or $\n
  StrCpy $R2 $R0 1 $R1
  ${If} $R2 == `$\r`
  ${OrIf} $R2 == `$\n`
    Goto loop
  ${EndIf}
 
  ;Trim characters (if needed)
  IntOp $R1 $R1 + 1
  ${If} $R1 < 0
    StrCpy $R0 $R0 $R1
  ${EndIf}
 
/*After this point:
  ------------------------------------------
  $R0 = ResultVar (output)*/
 
  ;Return output to user
  Pop $R2
  Pop $R1
  Exch $R0
FunctionEnd

Credits

Version 2.x - Diego Pedroso (deguix).
Version 1.x - Ximon Eighteen (sunjammer) / Diego Pedroso (deguix).

Personal tools
donate
ads