Strip Leading Zeros: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
(Strip leading zeros)
 
No edit summary
 
Line 64: Line 64:
   Already applied: |0|
   Already applied: |0|
   Completed
   Completed
[[Category:Code Examples]]
[[Category:Tutorials]]

Latest revision as of 10:54, 4 April 2007

Description

Strings with a leading zero are treated as octal. So stripping zeros using IntOp won't work:

IntOp $v "00009" + 0

The Script

Name "TrimLeadingZeros"
OutFile "TrimLeadingZeros.exe"
ShowInstDetails show
 
 
var gen
 
Function TrimLeadingZerosCall
 
	Exch $R1
	Push $R2
Loop:
	StrCpy $R2 "$R1" 1
	StrCmp "$R2" "0" TrimLeft
	Goto Done
 
TrimLeft:
	StrCpy $R1 "$R1" "" 1
	Goto Loop
 
Done:
	StrCmp $R1 "" 0 +2
	StrCpy $R1 "0"
 
	Pop $R2
	Exch $R1
 
FunctionEnd
 
 
!macro TrimLeadingZeros NUM
	Push ${NUM}
	Call TrimLeadingZerosCall
	Pop ${NUM}
!macroend
 
!define TrimLeadingZeros `!insertmacro TrimLeadingZeros`
 
Section ""
 
	StrCpy $gen "00009"
	${TrimLeadingZeros} $gen
	DetailPrint "Already applied: |$gen|"
 
	StrCpy $gen "00000"
	${TrimLeadingZeros} $gen
	DetailPrint "Already applied: |$gen|"
 
SectionEnd

Output

  Already applied: |9|
  Already applied: |0|
  Completed