StrFilter

From NSIS Wiki
Jump to navigationJump to search
Author: Instructor (talk, contrib)


Page for NSIS 2.07 and below users

You can use the latest version of headers (recommended) or the following function code (put the function code in your script before calling it)

Function Description

____________________________________________________________________________
 
                           StrFilter v1.2
____________________________________________________________________________
 
Thanks sunjammer (Function "StrUpper")
 
 
Features:
1. Convert string to uppercase or lowercase.
2. Set symbol filter.
 
 
Syntax:
${StrFilter} "[string]" "[options]" "[symbols1]" "[symbols2]" $var
 
"[string]"       ;[string]
                 ;  input string
                 ;
"[options]"      ;[+|-][1|2|3|12|23|31][eng|rus]
                 ;  +   : covert string to uppercase
                 ;  -   : covert string to lowercase
                 ;  1   : only Digits
                 ;  2   : only Letters
                 ;  3   : only Special
                 ;  12  : only Digits  + Letters
                 ;  23  : only Letters + Special
                 ;  31  : only Special + Digits
                 ;  eng : English symbols (default)
                 ;  rus : Russian symbols
                 ;
"[symbols1]"     ;[symbols1]
                 ;  symbols include (not changeable)
                 ;
"[symbols2]"     ;[symbols2]
                 ;  symbols exclude
                 ;
$var             ;output (result)
 
Note:
-Error flag if syntax error
-Same symbol to include & to exclude = to exclude


Example (UpperCase):

Section
	${StrFilter} "123abc 456DEF 7890|%#" "+" "" "" $R0
	; $R0="123ABC 456DEF 7890|%#"
SectionEnd

Example (LowerCase):

Section
	${StrFilter} "123abc 456DEF 7890|%#" "-" "ef" "" $R0
	; $R0="123abc 456dEF 7890|%#"
SectionEnd

Example (Filter1):

Section
	${StrFilter} "123abc 456DEF 7890|%#" "2" "|%" "" $R0
	; $R0="abcDEF|%"       ;only Letters + |%
SectionEnd

Example (Filter2):

Section
	${StrFilter} "123abc 456DEF 7890|%#" "13" "af" "4590" $R0
	; $R0="123a 6F 78|%#"  ;only Digits + Special + af - 4590
SectionEnd

Example (Filter3):

Section
	${StrFilter} "123abc 456DEF 7890|%#" "+12" "b" "def" $R0
	; $R0="123AbC4567890"  ;only Digits + Letters + b - def
SectionEnd

Example (Filter4):

Section
	${StrFilter} "123abc�?БВ 456DEFгде 7890|%#" "+12rus" "д" "ге" $R0
	; $R0="123�?БВ456д7890"  ;only Digits + Letters + д - ге
SectionEnd

Example (English + Russian Letters):

Section
	${StrFilter} "123abc�?БВ 456DEFгде 7890|%#" "2rus" "" "" $R0
	; $R0="�?БВгде"        ;only Russian Letters
	${StrFilter} "123abc�?БВ 456DEFгде 7890|%#" "2" "$R0" "" $R0
	; $R0="abc�?БВDEFгде"  ;only English + Russian Letters
SectionEnd

Example (Word Capitalize):

Section
	Push "_01-PERPETUOUS_DREAMER__-__THE_SOUND_OF_GOODBYE_(ORIG._MIX).MP3_"
	Call Capitalize
	Pop $R0
	; $R0="_01-Perpetuous_Dreamer__-__The_Sound_Of_Goodbye_(Orig._Mix).mp3_"
 
	${WordReplace} "$R0" "_" " " "+*" $R0
	; $R0=" 01-Perpetuous Dreamer - The Sound Of Goodbye (Orig. Mix).mp3 "
 
	${WordReplace} "$R0" " " "" "{}" $R0
	; $R0="01-Perpetuous Dreamer - The Sound Of Goodbye (Orig. Mix).mp3"
SectionEnd
 
Function Capitalize
	Exch $R0
	Push $0
	Push $1
	Push $2
 
	${StrFilter} '$R0' '-eng' '' '' $R0
	${StrFilter} '$R0' '-rus' '' '' $R0
 
	StrCpy $0 0
 
	loop:
	IntOp $0 $0 + 1
	StrCpy $1 $R0 1 $0
	StrCmp $1 '' end
	StrCmp $1 ' ' +5
	StrCmp $1 '_' +4
	StrCmp $1 '-' +3
	StrCmp $1 '(' +2
	StrCmp $1 '[' 0 loop
	IntOp $0 $0 + 1
	StrCpy $1 $R0 1 $0
	StrCmp $1 '' end
 
	${StrFilter} '$1' '+eng' '' '' $1
	${StrFilter} '$1' '+rus' '' '' $1
 
	StrCpy $2 $R0 $0
	IntOp $0 $0 + 1
	StrCpy $R0 $R0 '' $0
	IntOp $0 $0 - 2
	StrCpy $R0 '$2$1$R0'
	goto loop
 
	end:
	Pop $2
	Pop $1
	Pop $0
	Exch $R0
FunctionEnd

Function Code

Function StrFilter
	!define StrFilter `!insertmacro StrFilterCall`
 
	!macro StrFilterCall _STRING _FILTER _INCLUDE _EXCLUDE _RESULT
		Push `${_STRING}`
		Push `${_FILTER}`
		Push `${_INCLUDE}`
		Push `${_EXCLUDE}`
		Call StrFilter
		Pop ${_RESULT}
	!macroend
 
	Exch $2
	Exch
	Exch $1
	Exch
	Exch 2
	Exch $0
	Exch 2
	Exch 3
	Exch $R0
	Exch 3
	Push $3
	Push $4
	Push $5
	Push $6
	Push $7
	Push $R1
	Push $R2
	Push $R3
	Push $R4
	Push $R5
	Push $R6
	Push $R7
	Push $R8
	ClearErrors
 
	StrCpy $R2 $0 '' -3
	StrCmp $R2 eng eng
	StrCmp $R2 rus rus
	eng:
	StrCpy $4 65
	StrCpy $5 90
	StrCpy $6 97
	StrCpy $7 122
	goto langend
	rus:
	StrCpy $4 192
	StrCpy $5 223
	StrCpy $6 224
	StrCpy $7 255
	goto langend
	;...
 
	langend:
	StrCpy $R7 ''
	StrCpy $R8 ''
 
	StrCmp $2 '' 0 begin
 
	restart1:
	StrCpy $2 ''
	StrCpy $3 $0 1
	StrCmp $3 '+' +2
	StrCmp $3 '-' 0 +3
	StrCpy $0 $0 '' 1
	goto +2
	StrCpy $3 ''
 
	IntOp $0 $0 + 0
	StrCmp $0 0 +5
	StrCpy $R7 $0 1 0
	StrCpy $R8 $0 1 1
	StrCpy $R2 $0 1 2
	StrCmp $R2 '' filter error
 
	restart2:
	StrCmp $3 '' end
	StrCpy $R7 ''
	StrCpy $R8 '+-'
	goto begin
 
	filter:
	StrCmp $R7 '1' +3
	StrCmp $R7 '2' +2
	StrCmp $R7 '3' 0 error
 
	StrCmp $R8 '' begin
	StrCmp $R7$R8 '23' +2
	StrCmp $R7$R8 '32' 0 +3
	StrCpy $R7 -1
	goto begin
	StrCmp $R7$R8 '13' +2
	StrCmp $R7$R8 '31' 0 +3
	StrCpy $R7 -2
	goto begin
	StrCmp $R7$R8 '12' +2
	StrCmp $R7$R8 '21' 0 error
	StrCpy $R7 -3
 
	begin:
	StrCpy $R6 0
	StrCpy $R1 ''
 
	loop:
	StrCpy $R2 $R0 1 $R6
	StrCmp $R2 '' restartchk
 
	StrCmp $2 '' +7
	StrCpy $R4 0
	StrCpy $R5 $2 1 $R4
	StrCmp $R5 '' addsymbol
	StrCmp $R5 $R2 skipsymbol
	IntOp $R4 $R4 + 1
	goto -4
 
	StrCmp $1 '' +7
	StrCpy $R4 0
	StrCpy $R5 $1 1 $R4
	StrCmp $R5 '' +4
	StrCmp $R5 $R2 addsymbol
	IntOp $R4 $R4 + 1
	goto -4
 
	StrCmp $R7 '1' +2
	StrCmp $R7 '-1' 0 +4
	StrCpy $R4 48
	StrCpy $R5 57
	goto loop2
	StrCmp $R8 '+-' 0 +2
	StrCmp $3 '+' 0 +4
	StrCpy $R4 $4
	StrCpy $R5 $5
	goto loop2
	StrCpy $R4 $6
	StrCpy $R5 $7
 
	loop2:
	IntFmt $R3 '%c' $R4
	StrCmp $R2 $R3 found
	StrCmp $R4 $R5 notfound
	IntOp $R4 $R4 + 1
	goto loop2
 
	found:
	StrCmp $R8 '+-' setcase
	StrCmp $R7 '3' skipsymbol
	StrCmp $R7 '-3' addsymbol
	StrCmp $R8 '' addsymbol skipsymbol
 
	notfound:
	StrCmp $R8 '+-' addsymbol
	StrCmp $R7 '3' 0 +2
	StrCmp $R5 57 addsymbol +3
	StrCmp $R7 '-3' 0 +5
	StrCmp $R5 57 skipsymbol
	StrCpy $R4 48
	StrCpy $R5 57
	goto loop2
	StrCmp $R8 '' skipsymbol addsymbol
 
	setcase:
	StrCpy $R2 $R3
	addsymbol:
	StrCpy $R1 $R1$R2
	skipsymbol:
	IntOp $R6 $R6 + 1
	goto loop
 
	error:
	SetErrors
	StrCpy $R0 ''
	goto end
 
	restartchk:
	StrCpy $R0 $R1
	StrCmp $2 '' 0 restart1
	StrCmp $R8 '+-' 0 restart2
 
	end:
	Pop $R8
	Pop $R7
	Pop $R6
	Pop $R5
	Pop $R4
	Pop $R3
	Pop $R2
	Pop $R1
	Pop $7
	Pop $6
	Pop $5
	Pop $4
	Pop $3
	Pop $2
	Pop $1
	Pop $0
	Exch $R0
FunctionEnd