Search the Community
Showing results for tags '_generatevar_autosplit'.
-
_QuoteAutoIt() for strings in code that contain quote
Trong posted a topic in AutoIt Example Scripts
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: ; #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