使用空格计算和拆分字符串,除非在NSIS中引用之间

时间:2013-10-28 02:14:05

标签: string split nsis

我的问题就像Split string on spaces in Java, except if between quotes (i.e. treat \"hello world\" as one token),但是以NSIS的方式。 希望没有任何外部插件。

  

如何根据空格拆分字符串,但将引用的子字符串作为   一个字?

     

示例:

Location "Welcome to india" Bangalore Channai "IT city"  Mysore
     

它应该作为

存储在ArrayList中
Location
Welcome to india
Bangalore
Channai
IT city
Mysore

1 个答案:

答案 0 :(得分:2)

没有插件没有数组支持,所以我省略了数组部分。

可以使用NSIS中的3个基本字符串函数(StrLen,StrCpy和StrCmp)执行任何字符串操作:

!include LogicLib.nsh
Function SplitArg
Exch $0 ; str
Push $1 ; inQ
Push $3 ; idx
Push $4 ; tmp
StrCpy $1 0
StrCpy $3 0
loop:
    StrCpy $4 $0 1 $3
    ${If} $4 == '"'
        ${If} $1 <> 0 
            StrCpy $0 $0 "" 1
            IntOp $3 $3 - 1
        ${EndIf}
        IntOp $1 $1 !
    ${EndIf}
    ${If} $4 == '' ; The end?
        StrCpy $1 0
        StrCpy $4 ' '
    ${EndIf} 
    ${If} $4 == ' '
    ${AndIf} $1 = 0
        StrCpy $4 $0 $3
        StrCpy $1 $4 "" -1
        ${IfThen} $1 == '"' ${|} StrCpy $4 $4 -1 ${|}
        killspace:
            IntOp $3 $3 + 1
            StrCpy $0 $0 "" $3
            StrCpy $1 $0 1
            StrCpy $3 0
            StrCmp $1 ' ' killspace
        Push $0 ; Remaining
        Exch 4
        Pop $0
        StrCmp $4 "" 0 moreleft
            Pop $4
            Pop $3
            Pop $1
            Return
        moreleft:
        Exch $4
        Exch 2
        Pop $1
        Pop $3
        Return
    ${EndIf}
    IntOp $3 $3 + 1
    Goto loop
FunctionEnd


Section
push 'Location "Welcome to india" Bangalore Channai "IT city"  Mysore'
loop:
    call SplitArg
    Pop $0
    StrCmp $0 "" done
    DetailPrint Item=|$0|
    goto loop
done:
SectionEnd