Jump to content
Sign in to follow this  
Insolence

Little string parser

Recommended Posts

EDIT: Should change title to command parser***

EDIT #2: Modified to ignore quotes if preceded by a backslash, like in PHP

This is great for a pseudo-console, I commented it pretty well I think. If anyone sees any optimization-ability please let me know.

Also, please try to break it with correct syntax ( doesn't parse through ' " ' and such, just regular double quotes " " )

FunctionName ParamN, ParamN

; Errors:
;  1 - Uneven number of quotes
;  2 - Extra comma outside of quotes

$Input = 'SetTitle "AutoIT Help, tis a file.\", "New Title", 5, 19, 20'

; Remove function, we don't need it
$FirstSpace = StringInStr ( $Input, " " )
$Parameters = StringMid ( $Input, $FirstSpace )

; Setup param count
$ParamCount = 0

; Checking all possible comma's for a new parameter
$ParamSplit = StringSplit($Parameters, ',')

;For $i = 1 to $ParamSplit[0]
;   MsgBox("","", $ParamSplit[0] & "   -" & $ParamSplit[$i] & "-" )
;Next

; Setting up parameter array to max possible parameters (each comma is a possible new parameter)
Dim $Parameter[$ParamSplit[0]]

For $i = 1 to $ParamSplit[0]
    IsInQuotes ( $ParamSplit[$i] )
    
; If not in quotes, add a new parameter
    If @error = 0 Then 
    ; If no value and outside of quotes, must be an extra comma
        If $ParamSplit[$i] = "" Then
            SetError(2)
            Return -1
        EndIf
        
    ;MsgBox("","", $ParamSplit[$i])
        
    ; Subracting one from the array to compensate for the StringSplit array
    ; and removing quotes.  Also making sure '\"' are parsed correctly
        $ParamSplit[$i] = StringReplace( $ParamSplit[$i], '\"', "{QUOTE}" )
        $ParamSplit[$i] = StringReplace( $ParamSplit[$i], '"', "" )
        $ParamSplit[$i] = StringReplace( $ParamSplit[$i], '{QUOTE}', '"' )
        
        $Parameter[$ParamCount] = _StringTrimWS( $ParamSplit[$i] )
        
    ; Check 1 to the param count
        $ParamCount = $ParamCount + 1
    ElseIf @error = 1 Then  
    ; If comma IS found inside quotes, combine it with the next split
        $ParamSplit[$i+1] = $ParamSplit[$i] & "," &$ParamSplit[$i+1]
    EndIf
Next

; Getting rid of any extra parameter stuff
ReDim $Parameter[$ParamCount]
    
For $i = 0 to UBound($Parameter) - 1
    MsgBox("","", $Parameter[$i])
Next





; Functions

            
Func StringCount ( $String, $Delimeter )
    Local $Count
    
    $Count = StringSplit ( $String, $Delimeter )
    
    Return $Count[0] - 1
EndFunc

Func _StringTrimWS ( $String )
    Return StringStripWS ( StringStripWS ( $String, 1 ), 2 )
EndFunc

Func CombineStrings( $arString )
    Local $ReturnString
    
    If IsArray($arString) = 0 Then Return -1
        
; Combine all parameters
    For $i = 0 to UBound($arString) - 1
        $ReturnString = $ReturnString & $arString[$i] & " "
    Next
    
; Get rid of last " "
    $ReturnString = StringTrimRight ( $ReturnString, 1 )
    
    Return $ReturnString
EndFunc

Func IsInQuotes ( $String, $Pos = -1)
    Local $Check, $occurances, $increment
    
; Shrink $String from beginning to the character
    $String = StringMid ( $String, 1, $Pos )
    
    $increment = 1
    $occurances = 1 
    Do
        $Check = StringInStr ( $String, '"', -1, $increment )
        
    ; Check for a \ before a quote, to signal it's not the escaping quote
        If StringMid( $String, $Check - 1, $Check) <> '\"' Then $occurances = $occurances + 1
            
        $increment = $increment + 1
    Until $Check = 0
    
; Occurances of quotes is even, so it must be outside quotes
    If IsInt($occurances / 2) Then 
        Return 1
    Else    
        SetError(1)
    EndIf
EndFunc
Edited by Insolence

"I thoroughly disapprove of duels. If a man should challenge me, I would take him kindly and forgivingly by the hand and lead him to a quiet place and kill him." - Mark TwainPatient: "It hurts when I do $var_"Doctor: "Don't do $var_" - Lar.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...