StrTrimNewLines

From NSIS Wiki

Author: Anders (talk, contrib)


Description

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

Note: A similar function is part of NSIS and is located in StrFunc.nsh.

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.

Function Code

!define StrTrimNewLines "!insertmacro StrTrimNewLines Init ''"
!define UnStrTrimNewLines "!insertmacro StrTrimNewLines Init Un"
!macro StrTrimNewLines OutVar String
!verbose push
!verbose 3
!if "${OutVar}" == "Init"
	!undef ${String}StrTrimNewLines
	!define ${String}StrTrimNewLines "!insertmacro StrTrimNewLines "
	!if "${String}" != ""
	Function un.StrTrimNewLines
	!else
	Function StrTrimNewLines
	!endif
	!insertmacro StrTrimNewLines Func ''
	FunctionEnd
!else if "${OutVar}" == "Func"
	Exch $0
	Push $1
n:	StrCpy $1 $0 1 -1
	StrCmp $1 '$\r' +2
	StrCmp $1 '$\n' +1 e
	StrCpy $0 $0 -1
	Goto n
e:	Pop $1
	Exch $0
!else
	Push "${String}"
	!ifdef __UNINSTALL__
	Call un.StrTrimNewLines
	!else
	Call StrTrimNewLines
	!endif
	Pop "${OutVar}"
!endif
!verbose pop
!macroend


Example

${StrTrimNewLines}
 
Section
${StrTrimNewLines} $0 "This is just an example$\r$\n$\r$\n"
MessageBox mb_ok |$0|
SectionEnd
 
${UnStrTrimNewLines}
 
Section un.Uninstall
${UnStrTrimNewLines} $0 "This is just an example$\r$\n$\r$\n"
MessageBox mb_ok |$0|
SectionEnd


Credits

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

Personal tools
donate