Jump to content

Recommended Posts

Posted (edited)

Hi all,

Here is a custom function to expand the strings/macros - it can expand even variables with one $ symbol, i.e - "Some $Var"

And also, this function bypassing the bug(?) i found recently :) ...

$Var = "Our Value"
$Str = "Our String"

;================= Example with $iMode = -1, default - Only $String$ expanded =================
$sString = "This is <$Var$> and this is <$Str$>"
$ExpandedStr = _ExpandVarStrings($sString, -1)

MsgBox(64, "Expand Var Strings", "Was:" & @LF & $sString & @LF & @LF & "Expanded:" & @LF & $ExpandedStr)
;==========================================================================================================


;================= Example with $iMode = 1, Only $String expanded =================
$sString = "$Var goes here, and $Str goes here"
$ExpandedStr = _ExpandVarStrings($sString, 1)

MsgBox(64, "Expand Var Strings", "Was:" & @LF & $sString & @LF & @LF & "Expanded:" & @LF & $ExpandedStr)
;==========================================================================================================


;================= Example with $iMode = 2, All strings expanded ($String and $String$) =================
$sString = "$Var not equel $Str$"
$ExpandedStr = _ExpandVarStrings($sString, 2)

MsgBox(64, "Expand Var Strings", "Was:" & @LF & $sString & @LF & @LF & "Expanded:" & @LF & $ExpandedStr)
;==========================================================================================================


;===============================================================================
; Function Name:    _ExpandVarStrings()
; Description:      Expand string variable/macro inside string (using different modes).
; Parameter(s):        $sString - String that contain variables that need to be expanded.
;                      $iMode - Define the expand mode...
;                               -1 - (default) will expand only $Var$/@Macro@.
;                               1 - will expand only $Var/@Macro.
;                               2 - will expand $Var$/@Macro@ and $Var/@Macro.
; Requirement(s):   None.
; Return Value(s):  On success - Return initial string with expanded variables.
;
;                   On failure - If $sString not contain string variables that need to be expanded (according to $iMode), 
;                                will returned initial $sString.
;
; Note(s):          Only Global Scope variables are expanded.
; Author(s):        G.Sandler (a.k.a CreatoR)
;===============================================================================
Func _ExpandVarStrings($sString, $iMode=-1)
    Local $aExtractStrs, $sPattern, $sEval, $Old_Opt_EVS
    Local $sRetStr = $sString, $sExpndStr = ""
    Local $aSymbExpnd[2] = ["$", "@"]
    
    Local $Old_Func_Opt_EVS = Opt("ExpandVarStrings", 0)
    
    For $i = 0 To 1
        Switch $iMode
            Case 1
                $sPattern = "(?i)(\" & $aSymbExpnd[$i] & "[0-9a-z_]+)"
            Case 2
                $sPattern = "(?i)(\" & $aSymbExpnd[$i] & "[0-9a-z_]+\" & $aSymbExpnd[$i] & ")|(\" & $aSymbExpnd[$i] & "[0-9a-z_]+)"
            Case Else
                $sPattern = "(?i)(\" & $aSymbExpnd[$i] & "[0-9a-z_]+\" & $aSymbExpnd[$i] & ")"
        EndSwitch
        
        Local $aExtractStrs = StringRegExp($sString, $sPattern, 3)
        If Not IsArray($aExtractStrs) Then ContinueLoop
        
        For $iE = 0 To UBound($aExtractStrs)-1
            If $i = 0 Then
                $sEval = Eval(StringReplace($aExtractStrs[$iE], $aSymbExpnd[$i], ""))
                If $sEval <> "" Then $sRetStr = StringReplace($sRetStr, $aExtractStrs[$iE], $sEval)
            ElseIf $iMode <> -1 Or StringRight($aExtractStrs[$iE], 1) = $aSymbExpnd[$i] Then
                $sExpndStr = $aExtractStrs[$iE]
                If StringRight($sExpndStr, 1) <> $aSymbExpnd[$i] Then $sExpndStr &= $aSymbExpnd[$i]
                $Old_Opt_EVS = Opt("ExpandVarStrings", 1)
                $sExpndStr = $sExpndStr
                Opt("ExpandVarStrings", $Old_Opt_EVS)
                If $aExtractStrs[$iE] <> "" Then $sRetStr = StringReplace($sRetStr, $aExtractStrs[$iE], $sExpndStr)
            EndIf
        Next
    Next
    
    Opt("ExpandVarStrings", $Old_Func_Opt_EVS)
    Return $sRetStr
EndFunc

Edit 1: Fixed small problem when Opt("ExpandVarStrings", 1) was previously set by user.

Edit 2: Added note to the func comment about the usage limitation of this function.

Edited by MsCreatoR

 

  Reveal hidden contents

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Posted

Maybe i don't get it right, but why not let execute do all the work?

Func _Expand($s)
   Local $o = Opt("ExpandVarStrings",1)
   $s = StringReplace($s,'"','""')
   Local $r = Execute('"' & $s & '"')
   Opt("ExpandVarStrings",$o)
   Return $r
EndFunc

$a = "aa"
$x = "xx"
ConsoleWrite(_Expand("@AutoItVersion@, $a$, $x$") & @CRLF)

Please don't get me wrong, i don't want to criticize your code...

CoProc Multi Process Helper libraryTrashBin.nfshost.com store your AutoIt related files here!AutoIt User Map
Posted

  Quote

why not let execute do all the work?

Well, this will not work then:

$a = "aa"
$x = "xx"
ConsoleWrite(_Expand("3.2.8.1, $a, $x") & @CRLF)oÝ÷ ٩ݺ¹

 

  Reveal hidden contents

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Posted (edited)

Btw, there is no need to use Execute in that case..

Func _Expand($s)
    Local $o = Opt("ExpandVarStrings",1)
    $s = $s
    Opt("ExpandVarStrings",$o)
    Return $s
EndFunc

$a = "aa"
$x = "xx"
ConsoleWrite(_Expand("@AutoItVersion@, $a$, $x$") & @CRLF)

:)

Or i am not fully understand of what you mean by this example :">

Edited by MsCreatoR

 

  Reveal hidden contents

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Posted

Ok it was just a rhetorical question, no need to answer, i read your patterns.

I know now what functionality your after :)

But anyway, this will never work:

Func whatever()
   $x = "some val"
   ConsoleWrite(_ExpandVarStrings("$x$") & @CRLF)
EndFunc
whatever()

Because $x is not in the global scope, thus Eval() cant reach it.

Same goes for my suggested concept...

CoProc Multi Process Helper libraryTrashBin.nfshost.com store your AutoIt related files here!AutoIt User Map
Posted

  Quote

But anyway, this will never work:

Too bad :) - i thoght that Eval() can handle all kind of variables.

I added note to the comments of the function, because i can not see a solution for that one.

 

  Reveal hidden contents

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...