MCPcopy Create free account
hub / github.com/FreesmTeam/FreesmLauncher / expandVariables

Function expandVariables

launcher/launch/LaunchTask.cpp:257–308  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

255}
256
257QString expandVariables(const QString& input, QProcessEnvironment dict)
258{
259 QString result = input;
260
261 enum { base, maybeBrace, variable, brace } state = base;
262 int startIdx = -1;
263 for (int i = 0; i < result.length();) {
264 QChar c = result.at(i++);
265 switch (state) {
266 case base:
267 if (c == '$')
268 state = maybeBrace;
269 break;
270 case maybeBrace:
271 if (c == '{') {
272 state = brace;
273 startIdx = i;
274 } else if (c.isLetterOrNumber() || c == '_') {
275 state = variable;
276 startIdx = i - 1;
277 } else {
278 state = base;
279 }
280 break;
281 case brace:
282 if (c == '}') {
283 const auto res = dict.value(result.mid(startIdx, i - 1 - startIdx), "");
284 if (!res.isEmpty()) {
285 result.replace(startIdx - 2, i - startIdx + 2, res);
286 i = startIdx - 2 + res.length();
287 }
288 state = base;
289 }
290 break;
291 case variable:
292 if (!c.isLetterOrNumber() && c != '_') {
293 const auto res = dict.value(result.mid(startIdx, i - startIdx - 1), "");
294 if (!res.isEmpty()) {
295 result.replace(startIdx - 1, i - startIdx, res);
296 i = startIdx - 1 + res.length();
297 }
298 state = base;
299 }
300 break;
301 }
302 }
303 if (state == variable) {
304 if (const auto res = dict.value(result.mid(startIdx), ""); !res.isEmpty())
305 result.replace(startIdx - 1, result.length() - startIdx + 1, res);
306 }
307 return result;
308}
309
310QString LaunchTask::substituteVariables(QString& cmd, bool isLaunch) const
311{

Callers 1

substituteVariablesMethod · 0.85

Calls 4

valueMethod · 0.80
atMethod · 0.45
isEmptyMethod · 0.45
replaceMethod · 0.45

Tested by

no test coverage detected