fix logic
[personal-kdelibs.git] / kdecore / localization / common_helpers.cpp
blob39b7163371a62683b2d3c2734df99d67d3fa5f99
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();
32 int p1 = pos - 2;
33 while (p1 >= 0 && !label[p1].isLetterOrNumber()) {
34 --p1;
36 ++p1;
37 int p2 = pos + 2;
38 while (p2 < len && !label[p2].isLetterOrNumber()) {
39 ++p2;
41 --p2;
43 if (p1 == 0) {
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);
49 return label;
52 QString removeAcceleratorMarker (const QString &label_)
54 QString label = label_;
56 int p = 0;
57 bool accmarkRemoved = false;
58 while (true) {
59 p = label.indexOf('&', p);
60 if (p < 0 || p + 1 == label.length()) {
61 break;
64 if (label[p + 1].isLetterOrNumber()) {
65 // Valid accelerator.
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);
78 ++p;
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) {
85 bool hasCJK = false;
86 foreach (const QChar &c, label) {
87 if (c.unicode() >= 0x2e00) { // rough, but should be sufficient
88 hasCJK = true;
89 break;
92 if (hasCJK) {
93 p = 0;
94 while (true) {
95 p = label.indexOf('(', p);
96 if (p < 0) {
97 break;
99 label = removeReducedCJKAccMark(label, p + 1);
100 ++p;
105 return label;