Trong Posted August 24 Posted August 24 (edited) Whenever I embed strings in code that contain quotes I have to separate them very carefully, so I wrote this function to do it automatically correctly: expandcollapse popup; #FUNCTION# ==================================================================================================================== ; Name...........: _QuoteAutoIt ; Description ...: Wraps a string in AutoIt-compatible quotes (single or double) with minimal escaping. ; Syntax.........: _QuoteAutoIt($s) ; Parameters ....: $s - The input string. ; Return values .: The quoted string literal. ; Author ........: Adapted for use in _GenerateVar_AutoSplit(). ; =============================================================================================================================== Func _QuoteAutoIt($s) ; Case 1: contains double-quote but no single-quote -> wrap with single-quotes If StringInStr($s, '"') And Not StringInStr($s, "'") Then Return "'" & $s & "'" ; Case 2: contains single-quote but no double-quote -> wrap with double-quotes ElseIf Not StringInStr($s, '"') And StringInStr($s, "'") Then Return '"' & $s & '"' ; Case 3: contains both, or neither -> escape double-quotes and use double-quotes wrapper Else $s = StringReplace($s, '"', '""') Return '"' & $s & '"' EndIf EndFunc ; #FUNCTION# ==================================================================================================================== ; Name...........: _GenerateVar_AutoSplit ; Description ...: Generates AutoIt source code that assigns a string to a variable, automatically splitting long strings. ; Syntax.........: _GenerateVar_AutoSplit($sString, $sVarName [, $iLen = 1000]) ; Parameters ....: $sString - The input string. ; $sVarName - The variable name (with or without leading $). ; $iLen - [optional] Maximum chunk length per line. Default is 1000. ; Return values .: String containing AutoIt code lines for variable assignment. ; Author ........: Dao Van Trong - TRONG.PRO ; Remarks .......: - If the string is empty, returns "$VarName = ''". ; - Uses _QuoteAutoIt() for proper quoting. ; =============================================================================================================================== Func _GenerateVar_AutoSplit($sString, $sVarName='VarName', $iLen = 1000) ; Ensure variable name starts with $ If StringLeft($sVarName, 1) <> "$" Then $sVarName = "$" & $sVarName ; Validate chunk length If Not IsInt($iLen) Then $iLen = Int($iLen) If $iLen < 1 Then $iLen = 1 ; Convert to string if not already If Not IsString($sString) Then $sString = String($sString) Local $iTotal = StringLen($sString) If $iTotal = 0 Then ; Empty string Return $sVarName & " = " & _QuoteAutoIt("") EndIf Local $sOut = "" Local $iPos = 1 Local $bFirstLine = True While $iPos <= $iTotal Local $sPiece = StringMid($sString, $iPos, $iLen) Local $sQuoted = _QuoteAutoIt($sPiece) If $bFirstLine Then $sOut &= $sVarName & " = " & $sQuoted & @CRLF $bFirstLine = False Else $sOut &= $sVarName & " &= " & $sQuoted & @CRLF EndIf $iPos += $iLen WEnd ; Remove trailing CRLF Return StringTrimRight($sOut, StringLen(@CRLF)) EndFunc Edited yesterday at 06:34 AM by Trong make better Regards,
AspirinJunkie Posted August 25 Posted August 25 Are you aware that you can escape the respective quote characters in AutoIt string definitions by simply doubling them? So to create an AutoIt-compliant string definition for the AutoIt code itself from a string, you do not need to split it into individual strings and reassemble them, but simply double the quotes. This would be shorter, probably clearer and also better performing. In concrete terms, the following should also achieve your actual goal $result = '"' & StringReplace($raw, '"', '""', 0, 1) & '"' ; or equally: $result = "'" & StringReplace($raw, "'", "''", 0, 1) & "'" Andreik, Trong and SOLVE-SMART 1 2
Trong Posted Saturday at 03:28 AM Author Posted Saturday at 03:28 AM (edited) Thanks for your feedback Edited yesterday at 06:32 AM by Trong Regards,
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now