RosettaCodeData/Task/Strip-comments-from-a-string/AutoIt/strip-comments-from-a-strin...

57 lines
2.0 KiB
Plaintext

Dim $aLines[4] = _
[ _
"$a = $b + $c ; Comment line 1", _
"Dim $s1 = 'some text; tiled with semicolon', $s2 = 'another text; also tiled with semicolon' ; Comment line 2 - semicolon as part of assignment", _
"_SomeFunctionCall('string parameter with ;', $anotherParam) ; Comment line 3 - semicolon as part parameter in an function call", _
"Func _AnotherFunction($param1=';', $param2=';', $param3=';') ; Comment line 4 - semicolon as default value in parameter of a function headline" _
]
For $i = 0 To 3
ConsoleWrite('+> Line ' & $i+1 & ' full:' & @CRLF & '+>' & $aLines[$i] & @CRLF)
ConsoleWrite('!> without comment:' & @CRLF & '!>' & _LineStripComment($aLines[$i]) & @CRLF & @CRLF)
Next
Func _LineStripComment($_Line)
; == tile line by all included comment marker
Local $aPartsWithMarker = StringSplit($_Line, ';')
Local $sNoComment
; == if no comment marker: return full line
If $aPartsWithMarker[0] = 0 Then Return $_Line
; == check if string in part, if is'nt: following part(s) are comment
For $i = 1 To $aPartsWithMarker[0]
If Not StringRegExp($aPartsWithMarker[$i], "('|\x22)") Then
If $i = 1 Then
Return StringStripWS($aPartsWithMarker[$i], 2)
Else
Return StringStripWS($sNoComment & $aPartsWithMarker[$i], 2)
EndIf
Else
; == check if next leftside string delimiter has uneven count
Local $iLen = StringLen($aPartsWithMarker[$i])
Local $fDetectDelim = False, $sStringDelim, $iDelimCount, $sCurr
For $j = $iLen To 1 Step -1
$sCurr = StringMid($aPartsWithMarker[$i], $j, 1)
If Not $fDetectDelim Then
If $sCurr = "'" Or $sCurr = '"' Then
$sStringDelim = $sCurr
$iDelimCount += 1
$fDetectDelim = True
EndIf
Else
If $sCurr = $sStringDelim Then $iDelimCount += 1
EndIf
Next
If Mod($iDelimCount, 2) Then
; == uneven count: so it masks the comment marker
$sNoComment &= $aPartsWithMarker[$i] & ';'
Else
; == even count: all following is comment
Return StringStripWS($sNoComment & $aPartsWithMarker[$i], 2)
EndIf
EndIf
Next
EndFunc ;==>_LineStripComment