Skip to content

Commit f7361a6

Browse files
committed
Changed: StringReplace to use faster algorithm.
1 parent 8e8a27d commit f7361a6

File tree

1 file changed

+21
-21
lines changed

1 file changed

+21
-21
lines changed

WinHttp.au3

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,7 +1137,7 @@ Func _WinHttpSimpleFormFill(ByRef $hInternet, $sActionPage = Default, $sFormId =
11371137
$sCredName = $aStrSplit[0]
11381138
$sCredPass = $aStrSplit[1]
11391139
EndIf
1140-
$sAdditionalHeaders = StringReplace($sAdditionalHeaders, $aCred[0], "", 1)
1140+
$sAdditionalHeaders = StringReplace($sAdditionalHeaders, $aCred[0], "", 1, 0)
11411141
EndIf
11421142
EndIf
11431143
; Get page source
@@ -1218,7 +1218,7 @@ Func _WinHttpSimpleFormFill(ByRef $hInternet, $sActionPage = Default, $sFormId =
12181218
$sMethod = __WinHttpAttribVal($sAttributes, "method")
12191219
; Requested form is found. Set $fSend flag to true
12201220
$fSend = True
1221-
$sHTML = StringReplace($sHTML, $sForm, ">")
1221+
$sHTML = StringReplace($sHTML, $sForm, ">", 0, 1)
12221222
Local $sSpr1 = Chr(27), $sSpr2 = Chr(26)
12231223
__WinHttpNormalizeForm($sForm, $sSpr1, $sSpr2)
12241224
$aInput = StringRegExp($sForm, "(?si)<\h*(?:input|textarea|label|fieldset|legend|select|optgroup|option|button)\h*(.*?)/*\h*>", 3)
@@ -1241,8 +1241,8 @@ Func _WinHttpSimpleFormFill(ByRef $hInternet, $sActionPage = Default, $sFormId =
12411241
For $i = 0 To UBound($aInput) - 1 ; for all input elements
12421242
__WinHttpFormAttrib($aInputIds, $i, $aInput[$i])
12431243
If $aInputIds[1][$i] Then ; if there is 'name' field then add it
1244-
$aInputIds[1][$i] = __WinHttpURLEncode(StringReplace($aInputIds[1][$i], $sSpr1, " "), $sEnctype)
1245-
$aInputIds[2][$i] = __WinHttpURLEncode(StringReplace(StringReplace($aInputIds[2][$i], $sSpr2, ">"), $sSpr1, " "), $sEnctype)
1244+
$aInputIds[1][$i] = __WinHttpURLEncode(StringReplace($aInputIds[1][$i], $sSpr1, " ", 0, 1), $sEnctype)
1245+
$aInputIds[2][$i] = __WinHttpURLEncode(StringReplace(StringReplace($aInputIds[2][$i], $sSpr2, ">", 0, 1), $sSpr1, " ", 0, 1), $sEnctype)
12461246
$sAddData &= $aInputIds[1][$i] & "=" & $aInputIds[2][$i] & "&"
12471247
If $aInputIds[3][$i] = "submit" Then $sSubmit &= $aInputIds[1][$i] & "=" & $aInputIds[2][$i] & $sGrSep ; add to overall "submit" string
12481248
If $aInputIds[3][$i] = "radio" Then $sRadio &= $aInputIds[1][$i] & "=" & $aInputIds[2][$i] & $sGrSep ; add to overall "radio" string
@@ -1288,12 +1288,12 @@ Func _WinHttpSimpleFormFill(ByRef $hInternet, $sActionPage = Default, $sFormId =
12881288
If $sPassedData = $aInputIds[2][$j] Then
12891289
For $sChunkSub In StringSplit($sRadio, $sGrSep, 3) ; go tru all "radio" controls
12901290
If $sChunkSub == $aInputIds[1][$j] & "=" & $sPassedData Then
1291-
$sAddData = StringRegExpReplace(StringReplace($sAddData, "&", "&&"), "(?:&|\A)\Q" & $aInputIds[1][$j] & "\E(.*?)(?:&|\Z)", "&")
1292-
$sAddData = StringReplace(StringReplace($sAddData, "&&", "&"), "&&", "&")
1291+
$sAddData = StringRegExpReplace(StringReplace($sAddData, "&", "&&", 0, 1), "(?:&|\A)\Q" & $aInputIds[1][$j] & "\E(.*?)(?:&|\Z)", "&")
1292+
$sAddData = StringReplace(StringReplace($sAddData, "&&", "&", 0, 1), "&&", "&", 0, 1)
12931293
If StringLeft($sAddData, 1) = "&" Then $sAddData = StringTrimLeft($sAddData, 1)
12941294
$sAddData &= "&" & $sChunkSub
1295-
$sRadio = StringRegExpReplace(StringReplace($sRadio, $sGrSep, $sGrSep & $sGrSep), "(?:" & $sGrSep & "|\A)\Q" & $aInputIds[1][$j] & "\E(.*?)(?:" & $sGrSep & "|\Z)", $sGrSep)
1296-
$sRadio = StringReplace(StringReplace($sRadio, $sGrSep & $sGrSep, $sGrSep), $sGrSep & $sGrSep, $sGrSep)
1295+
$sRadio = StringRegExpReplace(StringReplace($sRadio, $sGrSep, $sGrSep & $sGrSep, 0, 1), "(?:" & $sGrSep & "|\A)\Q" & $aInputIds[1][$j] & "\E(.*?)(?:" & $sGrSep & "|\Z)", $sGrSep)
1296+
$sRadio = StringReplace(StringReplace($sRadio, $sGrSep & $sGrSep, $sGrSep, 0, 1), $sGrSep & $sGrSep, $sGrSep, 0, 1)
12971297
EndIf
12981298
Next
12991299
EndIf
@@ -1304,9 +1304,9 @@ Func _WinHttpSimpleFormFill(ByRef $hInternet, $sActionPage = Default, $sFormId =
13041304
$sButton = StringRegExpReplace($sButton, "\Q" & $aInputIds[1][$j] & "=" & $sPassedData & "\E" & $sGrSep & "*", "")
13051305
__WinHttpTrimBounds($sButton, $sGrSep)
13061306
Else
1307-
$sAddData = StringRegExpReplace(StringReplace($sAddData, "&", "&&"), "(?:&|\A)\Q" & $aInputIds[1][$j] & "=" & $aInputIds[2][$j] & "\E(?:&|\Z)", "&" & $aInputIds[1][$j] & "=" & $sPassedData & "&")
1307+
$sAddData = StringRegExpReplace(StringReplace($sAddData, "&", "&&", 0, 1), "(?:&|\A)\Q" & $aInputIds[1][$j] & "=" & $aInputIds[2][$j] & "\E(?:&|\Z)", "&" & $aInputIds[1][$j] & "=" & $sPassedData & "&")
13081308
$iNumRepl = @extended
1309-
$sAddData = StringReplace($sAddData, "&&", "&")
1309+
$sAddData = StringReplace($sAddData, "&&", "&", 0, 1)
13101310
If $iNumRepl > 1 Then ; equalize ; TODO: remove duplicates
13111311
$sAddData = StringRegExpReplace($sAddData, "(?:&|\A)\Q" & $aInputIds[1][$j] & "\E=.*?(?:&|\Z)", "&", $iNumRepl - 1)
13121312
EndIf
@@ -1340,11 +1340,11 @@ Func _WinHttpSimpleFormFill(ByRef $hInternet, $sActionPage = Default, $sFormId =
13401340
ElseIf $aInputIds[1][$j] == $aSplit[1] And $aInputIds[3][$j] = "radio" Then
13411341
For $sChunkSub In StringSplit($sRadio, $sGrSep, 3) ; go tru all "radio" controls
13421342
If $sChunkSub == $aInputIds[1][$j] & "=" & $sPassedData Then
1343-
$sAddData = StringReplace(StringReplace(StringRegExpReplace(StringReplace($sAddData, "&", "&&"), "(?:&|\A)\Q" & $aInputIds[1][$j] & "\E(.*?)(?:&|\Z)", "&"), "&&", "&"), "&&", "&")
1343+
$sAddData = StringReplace(StringReplace(StringRegExpReplace(StringReplace($sAddData, "&", "&&", 0, 1), "(?:&|\A)\Q" & $aInputIds[1][$j] & "\E(.*?)(?:&|\Z)", "&"), "&&", "&", 0, 1), "&&", "&", 0, 1)
13441344
If StringLeft($sAddData, 1) = "&" Then $sAddData = StringTrimLeft($sAddData, 1)
13451345
$sAddData &= "&" & $sChunkSub
1346-
$sRadio = StringRegExpReplace(StringReplace($sRadio, $sGrSep, $sGrSep & $sGrSep), "(?:" & $sGrSep & "|\A)\Q" & $aInputIds[1][$j] & "\E(.*?)(?:" & $sGrSep & "|\Z)", $sGrSep)
1347-
$sRadio = StringReplace(StringReplace($sRadio, $sGrSep & $sGrSep, $sGrSep), $sGrSep & $sGrSep, $sGrSep)
1346+
$sRadio = StringRegExpReplace(StringReplace($sRadio, $sGrSep, $sGrSep & $sGrSep, 0, 1), "(?:" & $sGrSep & "|\A)\Q" & $aInputIds[1][$j] & "\E(.*?)(?:" & $sGrSep & "|\Z)", $sGrSep)
1347+
$sRadio = StringReplace(StringReplace($sRadio, $sGrSep & $sGrSep, $sGrSep, 0, 1), $sGrSep & $sGrSep, $sGrSep, 0, 1)
13481348
EndIf
13491349
Next
13501350
ContinueLoop 2 ; process next parameter
@@ -1358,9 +1358,9 @@ Func _WinHttpSimpleFormFill(ByRef $hInternet, $sActionPage = Default, $sFormId =
13581358
ContinueLoop 2 ; process next parameter
13591359
EndIf
13601360
Next
1361-
$sAddData = StringRegExpReplace(StringReplace($sAddData, "&", "&&"), "(?:&|\A)\Q" & $aSplit[1] & "\E=.*?(?:&|\Z)", "&" & $aSplit[1] & "=" & $sPassedData & "&")
1361+
$sAddData = StringRegExpReplace(StringReplace($sAddData, "&", "&&", 0, 1), "(?:&|\A)\Q" & $aSplit[1] & "\E=.*?(?:&|\Z)", "&" & $aSplit[1] & "=" & $sPassedData & "&")
13621362
$iNumRepl = @extended
1363-
$sAddData = StringReplace($sAddData, "&&", "&")
1363+
$sAddData = StringReplace($sAddData, "&&", "&", 0, 1)
13641364
If $iNumRepl > 1 Then ; remove duplicates
13651365
$sAddData = StringRegExpReplace($sAddData, "(?:&|\A)\Q" & $aSplit[1] & "\E=.*?(?:&|\Z)", "&", $iNumRepl - 1)
13661366
EndIf
@@ -1381,8 +1381,8 @@ Func _WinHttpSimpleFormFill(ByRef $hInternet, $sActionPage = Default, $sFormId =
13811381
For $i = 0 To UBound($aInput) - 1 ; for all input elements
13821382
__WinHttpFormAttrib($aInputIds, $i, $aInput[$i])
13831383
If $aInputIds[1][$i] Then ; if there is 'name' field
1384-
$aInputIds[1][$i] = StringReplace($aInputIds[1][$i], $sSpr1, " ")
1385-
$aInputIds[2][$i] = StringReplace(StringReplace($aInputIds[2][$i], $sSpr2, ">"), $sSpr1, " ")
1384+
$aInputIds[1][$i] = StringReplace($aInputIds[1][$i], $sSpr1, " ", 0, 1)
1385+
$aInputIds[2][$i] = StringReplace(StringReplace($aInputIds[2][$i], $sSpr2, ">", 0, 1), $sSpr1, " ", 0, 1)
13861386
If $aInputIds[3][$i] = "file" Then
13871387
$sAddData &= "--" & $sBoundary & @CRLF & _
13881388
$sCDisp & $aInputIds[1][$i] & '"; filename=""' & @CRLF & @CRLF & _
@@ -1525,7 +1525,7 @@ Func _WinHttpSimpleFormFill(ByRef $hInternet, $sActionPage = Default, $sFormId =
15251525
EndIf
15261526
Next
15271527
$sAddData = StringRegExpReplace($sAddData, '(?s)\Q' & $sCDisp & $aSplit[1] & '"' & '\E\r\n\r\n.*?\r\n', _
1528-
$sCDisp & $aSplit[1] & '"' & @CRLF & @CRLF & StringReplace($sPassedData, "\", "\\") & @CRLF)
1528+
$sCDisp & $aSplit[1] & '"' & @CRLF & @CRLF & StringReplace($sPassedData, "\", "\\", 0, 1) & @CRLF)
15291529
$iNumRepl = @extended
15301530
If $iNumRepl > 1 Then ; remove duplicates
15311531
$sAddData = StringRegExpReplace($sAddData, '(?s)\Q--' & $sBoundary & @CRLF & $sCDisp & $aSplit[1] & '"' & '\E\r\n\r\n.*?\r\n', "", $iNumRepl - 1)
@@ -2038,7 +2038,7 @@ EndFunc
20382038
Func __WinHttpURLEncode($vData, $sEncType = "")
20392039
If IsBool($vData) Then Return $vData
20402040
$vData = __WinHttpHTMLDecode($vData)
2041-
If $sEnctype = "text/plain" Then Return StringReplace($vData, " ", "+")
2041+
If $sEnctype = "text/plain" Then Return StringReplace($vData, " ", "+", 0, 1)
20422042
Local $aData = StringToASCIIArray($vData, Default, Default, 2)
20432043
Local $sOut
20442044
For $i = 0 To UBound($aData) - 1
@@ -2055,7 +2055,7 @@ Func __WinHttpURLEncode($vData, $sEncType = "")
20552055
EndFunc
20562056

20572057
Func __WinHttpHTMLDecode($vData)
2058-
Return StringReplace(StringReplace(StringReplace(StringReplace(StringReplace($vData, "&apos;", "'"), "&amp;", "&"), "&lt;", "<"), "&gt;", ">"), "&quot;", '"')
2058+
Return StringReplace(StringReplace(StringReplace(StringReplace(StringReplace($vData, "&apos;", "'", 0, 1), "&amp;", "&", 0, 1), "&lt;", "<", 0, 1), "&gt;", ">", 0, 1), "&quot;", '"', 0, 1)
20592059
EndFunc
20602060

20612061
Func __WinHttpNormalizeActionURL($sActionPage, ByRef $sAction, ByRef $iScheme, ByRef $sNewURL, ByRef $sEnctype, ByRef $sMethod, ByRef $sReferer, $sURL = "")
@@ -2080,7 +2080,7 @@ Func __WinHttpNormalizeActionURL($sActionPage, ByRef $sAction, ByRef $iScheme, B
20802080
$sAction = $aCrackURL[6] & $sAction
20812081
EndIf
20822082
ElseIf StringLeft($sAction, 1) = "#" Then
2083-
$sAction = StringReplace($sActionPage, StringRegExpReplace($sActionPage, "(.*?)(#.*?)", "$2"), $sAction)
2083+
$sAction = StringReplace($sActionPage, StringRegExpReplace($sActionPage, "(.*?)(#.*?)", "$2"), $sAction, 0, 1)
20842084
ElseIf StringLeft($sAction, 1) = "/" Then
20852085
$aCrackURL = _WinHttpCrackUrl($sURL)
20862086
If Not @error Then

0 commit comments

Comments
 (0)