StrTrimNewLines: Difference between revisions
From NSIS Wiki
Jump to navigationJump to search
m (Corrected link to LogicLib header file.) |
m (Call function directly) |
||
(One intermediate revision by the same user not shown) | |||
Line 1: | Line 1: | ||
{{PageAuthor| | {{PageAuthor|Anders}} | ||
== Description == | == Description == | ||
This function removes unnecessary new lines at the end of a string. | |||
<div style="background-color:#FFFFC0;color:#000000;border:0.1em solid #000000;padding:0.5em;"><b>Note:</b> A similar function is part of NSIS and is located in StrFunc.nsh.</div><p> | |||
== How To Use == | == How To Use == | ||
Line 14: | Line 12: | ||
<highlight-nsis> | <highlight-nsis> | ||
${StrTrimNewLines} | ${StrTrimNewLines} $ResultVar "String" | ||
</highlight-nsis> | </highlight-nsis> | ||
or | or | ||
Line 20: | Line 18: | ||
Push "String" | Push "String" | ||
Call StrTrimNewLines | Call StrTrimNewLines | ||
Pop | Pop $ResultVar | ||
</highlight-nsis> | </highlight-nsis> | ||
Line 30: | Line 28: | ||
; String | ; String | ||
: String where the new line characters at its end are removed. | : String where the new line characters at its end are removed. | ||
== Function Code == | == Function Code == | ||
<highlight-nsis> | <highlight-nsis> | ||
!define StrTrimNewLines "!insertmacro StrTrimNewLines" | !define StrTrimNewLines "!insertmacro StrTrimNewLines Init ''" | ||
!define UnStrTrimNewLines "!insertmacro StrTrimNewLines Init Un" | |||
!macro StrTrimNewLines | !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 | !macroend | ||
</highlight-nsis> | |||
=== Example === | |||
<highlight-nsis> | |||
${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 | |||
</highlight-nsis> | </highlight-nsis> | ||
== Credits == | == Credits == | ||
Version 3.x - [[User:Anders|Anders]].<br> | |||
Version 2.x - Diego Pedroso ([[User:deguix|deguix]]).<br> | Version 2.x - Diego Pedroso ([[User:deguix|deguix]]).<br> | ||
Version 1.x - Ximon Eighteen ([[User:sunjammer|sunjammer]]) / Diego Pedroso ([[User:deguix|deguix]]). | Version 1.x - Ximon Eighteen ([[User:sunjammer|sunjammer]]) / Diego Pedroso ([[User:deguix|deguix]]). | ||
[[Category:String Functions]] | [[Category:String Functions]] |
Latest revision as of 13:35, 4 July 2018
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).