wiRedPanda
Logic Circuit Simulator
Loading...
Searching...
No Matches
CodeGenUtils.h
Go to the documentation of this file.
1// Copyright 2015 - 2026, GIBIS-UNIFESP and the wiRedPanda contributors
2// SPDX-License-Identifier: GPL-3.0-or-later
3
7
8#pragma once
9
10#include <algorithm>
11
12#include <QRegularExpression>
13#include <QString>
14
15namespace CodeGenUtils {
16
18inline QString stripAccents(const QString &input)
19{
20 QString normalized = input.normalized(QString::NormalizationForm_D);
21 static const QRegularExpression diacriticMarks("[\\p{Mn}]");
22 return normalized.remove(diacriticMarks);
23}
24
36inline QString removeForbiddenChars(const QString &input, const bool stripFirst = false)
37{
38 QString result = (stripFirst ? stripAccents(input) : input).toLower().trimmed().replace(' ', '_').replace('-', '_');
39 result.erase(std::remove_if(result.begin(), result.end(),
40 [](const QChar c) { return !QChar::isLetterOrNumber(c.unicode()) && c != QLatin1Char('_'); }),
41 result.end());
42
43 if (result.isEmpty()) {
44 result = "_unnamed";
45 } else if (result[0].isDigit()) {
46 result.prepend('_');
47 }
48
49 return result;
50}
51
60inline QString sanitizeComment(const QString &input)
61{
62 QString result = input;
63 result.replace(QLatin1Char('\r'), QLatin1Char(' '));
64 result.replace(QLatin1Char('\n'), QLatin1Char(' '));
65 return result;
66}
67
68} // namespace CodeGenUtils
QString removeForbiddenChars(const QString &input, const bool stripFirst=false)
Converts input into a legal language identifier.
QString sanitizeComment(const QString &input)
Makes input safe to embed in a single-line "//" comment.
QString stripAccents(const QString &input)
Strips Unicode diacritic marks (accents) from input using NFC → NFD decomposition.