StrTrimNewLines: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
(Version: 2.0.)
 
m (Call function directly)
 
(2 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{PageAuthor|deguix}}
{{PageAuthor|Anders}}


== Description ==
== Description ==


'''Requires:''' [[LogicLib]].
This function removes unnecessary new lines at the end of a string.


'''Version:''' 2.0.1.
<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>
 
This function removes unnecessary new lines at end of a string.


== How To Use ==
== How To Use ==
Line 14: Line 12:


<highlight-nsis>
<highlight-nsis>
${StrTrimNewLines} "ResultVar" "String"
${StrTrimNewLines} $ResultVar "String"
</highlight-nsis>
</highlight-nsis>
or
or
Line 20: Line 18:
Push "String"
Push "String"
Call StrTrimNewLines
Call StrTrimNewLines
Pop "ResultVar"
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.
=== Example ===
<highlight-nsis>
${StrTrimNewLines} $0 "This is just an example$\r$\n$\r$\n"
;$0 = "This is just an example"
</highlight-nsis>


== 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 ResultVar String
!macro StrTrimNewLines OutVar String
  Push "${String}"
!verbose push
  Call StrTrimNewLines
!verbose 3
  Pop "${ResultVar}"
!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>


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:
=== Example ===
  ;Subtract to get "String"'s last characters
  IntOp $R1 $R1 - 1


  ;Verify if they are either $\r or $\n
<highlight-nsis>
  StrCpy $R2 $R0 1 $R1
${StrTrimNewLines}
  ${If} $R2 == `$\r`
  ${OrIf} $R2 == `$\n`
    Goto loop
  ${EndIf}


  ;Trim characters (if needed)
Section
  IntOp $R1 $R1 + 1
${StrTrimNewLines} $0 "This is just an example$\r$\n$\r$\n"
  ${If} $R1 < 0
MessageBox mb_ok |$0|
    StrCpy $R0 $R0 $R1
SectionEnd
  ${EndIf}


/*After this point:
${UnStrTrimNewLines}
  ------------------------------------------
  $R0 = ResultVar (output)*/


  ;Return output to user
Section un.Uninstall
  Pop $R2
${UnStrTrimNewLines} $0 "This is just an example$\r$\n$\r$\n"
  Pop $R1
MessageBox mb_ok |$0|
  Exch $R0
SectionEnd
FunctionEnd
</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).