1 /* This file is part of the KDE libraries
2 Copyright (C) 2008 Chusslove Illich <caslav.ilic@gmx.net>
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
20 #include <common_helpers_p.h>
22 // If pos points to alphanumeric X in "...(X)...", which is preceded or
23 // followed only by non-alphanumerics, then "(X)" gets removed.
24 static QString
removeReducedCJKAccMark (const QString
&label
, int pos
)
26 if ( pos
> 0 && pos
+ 1 < label
.length()
27 && label
[pos
- 1] == '(' && label
[pos
+ 1] == ')'
28 && label
[pos
].isLetterOrNumber())
30 // Check if at start or end, ignoring non-alphanumerics.
31 int len
= label
.length();
33 while (p1
>= 0 && !label
[p1
].isLetterOrNumber()) {
38 while (p2
< len
&& !label
[p2
].isLetterOrNumber()) {
44 return label
.left(pos
- 1) + label
.mid(p2
+ 1);
45 } else if (p2
+ 1 == len
) {
46 return label
.left(p1
) + label
.mid(pos
+ 2);
52 QString
removeAcceleratorMarker (const QString
&label_
)
54 QString label
= label_
;
57 bool accmarkRemoved
= false;
59 p
= label
.indexOf('&', p
);
60 if (p
< 0 || p
+ 1 == label
.length()) {
64 if (label
[p
+ 1].isLetterOrNumber()) {
66 label
= label
.left(p
) + label
.mid(p
+ 1);
68 // May have been an accelerator in CJK-style "(&X)"
69 // at the start or end of text.
70 label
= removeReducedCJKAccMark(label
, p
);
72 accmarkRemoved
= true;
73 } else if (label
[p
+ 1] == '&') {
74 // Escaped accelerator marker.
75 label
= label
.left(p
) + label
.mid(p
+ 1);
81 // If no marker was removed, and there are CJK characters in the label,
82 // also try to remove reduced CJK marker -- something may have removed
83 // ampersand beforehand.
84 if (!accmarkRemoved
) {
86 foreach (const QChar
&c
, label
) {
87 if (c
.unicode() >= 0x2e00) { // rough, but should be sufficient
95 p
= label
.indexOf('(', p
);
99 label
= removeReducedCJKAccMark(label
, p
+ 1);