Allow variable recursion

Simple recursion of variable expansion allows for multiple use cases in
portability of designs.  This also includes basic prevention of
recursion loops

Fixes https://gitlab.com/kicad/code/kicad/issues/10398
This commit is contained in:
Seth Hillbrand 2023-01-20 16:43:47 -08:00
parent 9986eb5cf6
commit 3465fe3e50
1 changed files with 15 additions and 1 deletions

View File

@ -115,8 +115,15 @@ wxString ExpandTextVars( const wxString& aSource,
//
// Stolen from wxExpandEnvVars and then heavily optimized
//
wxString KIwxExpandEnvVars( const wxString& str, const PROJECT* aProject )
wxString KIwxExpandEnvVars( const wxString& str, const PROJECT* aProject, std::set<wxString>* aSet = nullptr )
{
// If the same string is inserted twice, we have a loop
if( aSet )
{
if( auto [ _, result ] = aSet->insert( str ); !result )
return str;
}
size_t strlen = str.length();
wxString strResult;
@ -278,6 +285,13 @@ wxString KIwxExpandEnvVars( const wxString& str, const PROJECT* aProject )
}
}
std::set<wxString> loop_check;
auto first_pos = strResult.find_first_of( wxS( "{(%" ) );
auto last_pos = strResult.find_last_of( wxS( "})%" ) );
if( first_pos != strResult.npos && last_pos != strResult.npos && first_pos != last_pos )
strResult = KIwxExpandEnvVars( strResult, aProject, aSet ? aSet : &loop_check );
return strResult;
}