| 1 | // Copyright (C) 2025 The Qt Company Ltd. |
|---|---|
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #include "qaccessiblehelper_p.h" |
| 5 | |
| 6 | QT_BEGIN_NAMESPACE |
| 7 | |
| 8 | using namespace Qt::StringLiterals; |
| 9 | |
| 10 | /* This function will return the offset of the '&' in the text that would be |
| 11 | preceding the accelerator character. |
| 12 | If this text does not have an accelerator, -1 will be returned. */ |
| 13 | static qsizetype qt_accAmpIndex(const QString &text) |
| 14 | { |
| 15 | #ifndef QT_NO_SHORTCUT |
| 16 | if (text.isEmpty()) |
| 17 | return -1; |
| 18 | |
| 19 | qsizetype fa = 0; |
| 20 | while ((fa = text.indexOf(ch: u'&', from: fa)) != -1) { |
| 21 | ++fa; |
| 22 | if (fa < text.size()) { |
| 23 | // ignore "&&" |
| 24 | if (text.at(i: fa) == u'&') { |
| 25 | ++fa; |
| 26 | continue; |
| 27 | } else { |
| 28 | return fa - 1; |
| 29 | break; |
| 30 | } |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | return -1; |
| 35 | #else |
| 36 | Q_UNUSED(text); |
| 37 | return -1; |
| 38 | #endif |
| 39 | } |
| 40 | |
| 41 | QString qt_accStripAmp(const QString &text) |
| 42 | { |
| 43 | QString newText(text); |
| 44 | qsizetype ampIndex = qt_accAmpIndex(text: newText); |
| 45 | if (ampIndex != -1) |
| 46 | newText.remove(i: ampIndex, len: 1); |
| 47 | |
| 48 | return newText.replace(before: "&&"_L1, after: "&"_L1); |
| 49 | } |
| 50 | |
| 51 | QT_END_NAMESPACE |
| 52 |
