aboutsummaryrefslogtreecommitdiffstats
path: root/src/libs/devcontainer/substitute.cpp
blob: 80a9986b79812dde7008ff027e3cf225e7cae014 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Copyright (C) 2025 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "substitute.h"

#include "devcontainertr.h"

#include <QRegularExpression>

namespace DevContainer::Internal {

void substituteVariables(QString &str, const Replacers &replacers)
{
    QRegularExpression re("\\$\\{([^}]+)\\}");
    QRegularExpressionMatchIterator it = re.globalMatch(str);

    struct Replace
    {
        qsizetype start;
        qsizetype length;
        QString replacement;
    };
    QList<Replace> replacements;

    while (it.hasNext()) {
        QRegularExpressionMatch match = it.next();
        QString varName = match.captured(1);
        QStringList parts = varName.split(':');
        QString variableName = parts.takeFirst();

        auto itReplacer = replacers.find(variableName);
        if (itReplacer != replacers.end()) {
            QString replacement = itReplacer.value()(parts);
            replacements.append({match.capturedStart(), match.capturedLength(), replacement});
        }
    }

    // Apply replacements in reverse order to avoid messing up indices
    for (auto it = replacements.crbegin(); it != replacements.crend(); ++it)
        str.replace(it->start, it->length, std::move(it->replacement));
}

} // namespace DevContainer::Internal