KDECore
common_helpers.cpp
Go to the documentation of this file.
00001 /* This file is part of the KDE libraries 00002 Copyright (C) 2008 Chusslove Illich <caslav.ilic@gmx.net> 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Library General Public 00006 License as published by the Free Software Foundation; either 00007 version 2 of the License, or (at your option) any later version. 00008 00009 This library is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 Library General Public License for more details. 00013 00014 You should have received a copy of the GNU Library General Public License 00015 along with this library; see the file COPYING.LIB. If not, write to 00016 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00017 Boston, MA 02110-1301, USA. 00018 */ 00019 00020 #include <common_helpers_p.h> 00021 00022 // If pos points to alphanumeric X in "...(X)...", which is preceded or 00023 // followed only by non-alphanumerics, then "(X)" gets removed. 00024 static QString removeReducedCJKAccMark (const QString &label, int pos) 00025 { 00026 if ( pos > 0 && pos + 1 < label.length() 00027 && label[pos - 1] == QLatin1Char('(') && label[pos + 1] == QLatin1Char(')') 00028 && label[pos].isLetterOrNumber()) 00029 { 00030 // Check if at start or end, ignoring non-alphanumerics. 00031 int len = label.length(); 00032 int p1 = pos - 2; 00033 while (p1 >= 0 && !label[p1].isLetterOrNumber()) { 00034 --p1; 00035 } 00036 ++p1; 00037 int p2 = pos + 2; 00038 while (p2 < len && !label[p2].isLetterOrNumber()) { 00039 ++p2; 00040 } 00041 --p2; 00042 00043 if (p1 == 0) { 00044 return label.left(pos - 1) + label.mid(p2 + 1); 00045 } else if (p2 + 1 == len) { 00046 return label.left(p1) + label.mid(pos + 2); 00047 } 00048 } 00049 return label; 00050 } 00051 00052 QString removeAcceleratorMarker (const QString &label_) 00053 { 00054 QString label = label_; 00055 00056 int p = 0; 00057 bool accmarkRemoved = false; 00058 while (true) { 00059 p = label.indexOf(QLatin1Char('&'), p); 00060 if (p < 0 || p + 1 == label.length()) { 00061 break; 00062 } 00063 00064 if (label[p + 1].isLetterOrNumber()) { 00065 // Valid accelerator. 00066 label = label.left(p) + label.mid(p + 1); 00067 00068 // May have been an accelerator in CJK-style "(&X)" 00069 // at the start or end of text. 00070 label = removeReducedCJKAccMark(label, p); 00071 00072 accmarkRemoved = true; 00073 } else if (label[p + 1] == QLatin1Char('&')) { 00074 // Escaped accelerator marker. 00075 label = label.left(p) + label.mid(p + 1); 00076 } 00077 00078 ++p; 00079 } 00080 00081 // If no marker was removed, and there are CJK characters in the label, 00082 // also try to remove reduced CJK marker -- something may have removed 00083 // ampersand beforehand. 00084 if (!accmarkRemoved) { 00085 bool hasCJK = false; 00086 foreach (const QChar &c, label) { 00087 if (c.unicode() >= 0x2e00) { // rough, but should be sufficient 00088 hasCJK = true; 00089 break; 00090 } 00091 } 00092 if (hasCJK) { 00093 p = 0; 00094 while (true) { 00095 p = label.indexOf(QLatin1Char('('), p); 00096 if (p < 0) { 00097 break; 00098 } 00099 label = removeReducedCJKAccMark(label, p + 1); 00100 ++p; 00101 } 00102 } 00103 } 00104 00105 return label; 00106 }
KDE 4.6 API Reference