Archive: Problem with TrimNewlines


Problem with TrimNewlines
I've been using the TrimNewlines function from the NSIS User Manual (Appendix B, Useful Functions). While testing my NSIS script I found that the TrimNewlines function returns an empty string if the input string does not have any newlines at the end. I expected it to return the input string unchanged.

Here is how I changed the function to do this:


Function TrimNewlines
Exch $R0
Push $R1
Push $R2
StrCpy $R1 0

loop:
IntOp $R1 $R1 - 1
StrCpy $R2 $R0 1 $R1
StrCmp $R2 "$\r" loop
StrCmp $R2 "$\n" loop
IntOp $R1 $R1 + 1
IntCmp $R1 0 no_trim_needed
StrCpy $R0 $R0 $R1

no_trim_needed:
Pop $R2
Pop $R1
Exch $R0
FunctionEnd

If the stack has "whatever$\r$\n" or simply "whatever", the revised function returns "whatever" on the stack.

Done. Thanks.