From 54da2b2911ace652dbd2c8ea852c5b8c47ab09c8 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Mon, 28 Jul 2014 23:31:32 -0700 Subject: Simplify and unify Q{ByteArray,String{,Ref}}::{simplify,trimmed} MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit As a side effect, QString::simplified() will always return a detached copy, even if it's the same contents. QStringRef::trimmed() can use the same calculation algorithm but can't use the trimmed_helper() template function due to its lack of a constructor taking begin pointer and size (std::string_view could do it). That constructor can't be added because QStringRef always refers to an existing QString, not to data in memory. Change-Id: Ib966c1741819c68c6bac5fcbb00f8ac818b3ccab Reviewed-by: Olivier Goffart Reviewed-by: Jędrzej Nowacki Reviewed-by: Marc Mutz --- src/corelib/tools/qstringalgorithms_p.h | 135 ++++++++++++++++++++++++++++++++ 1 file changed, 135 insertions(+) create mode 100644 src/corelib/tools/qstringalgorithms_p.h (limited to 'src/corelib/tools/qstringalgorithms_p.h') diff --git a/src/corelib/tools/qstringalgorithms_p.h b/src/corelib/tools/qstringalgorithms_p.h new file mode 100644 index 0000000000..5a68260c59 --- /dev/null +++ b/src/corelib/tools/qstringalgorithms_p.h @@ -0,0 +1,135 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Intel Corporation. +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtCore module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QSTRINGALGORITHMS_P_H +#define QSTRINGALGORITHMS_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists for the convenience +// of internal files. This header file may change from version to version +// without notice, or even be removed. +// +// We mean it. +// + +#include "qstring.h" +#include "qlocale_p.h" // for ascii_isspace + +QT_BEGIN_NAMESPACE + +template struct QStringAlgorithms +{ + typedef typename StringType::value_type Char; + typedef typename StringType::size_type size_type; + typedef typename QtPrivate::remove_cv::type NakedStringType; + static const bool isConst = QtPrivate::is_const::value; + + static inline bool isSpace(char ch) { return ascii_isspace(ch); } + static inline bool isSpace(QChar ch) { return ch.isSpace(); } + + // Surrogate pairs are not handled in either of the functions below. That is + // not a problem because there are no space characters (Zs, Zl, Zp) outside the + // Basic Multilingual Plane. + + static inline void trimmed_helper_positions(const Char *&begin, const Char *&end) + { + // skip white space from start + while (begin < end && isSpace(*begin)) + begin++; + // skip white space from end + if (begin < end) { + while (begin < end && isSpace(end[-1])) + end--; + } + } + + static inline StringType trimmed_helper(StringType &str) + { + const Char *begin = str.cbegin(); + const Char *end = str.cend(); + trimmed_helper_positions(begin, end); + + if (begin == str.cbegin() && end == str.cend()) + return str; + if (begin == end) + return StringType(); + return StringType(begin, end - begin); + } + + static inline StringType simplified_helper(StringType &str) + { + if (str.isEmpty()) + return str; + const Char *src = str.cbegin(); + const Char *end = str.cend(); + NakedStringType result(str.size(), Qt::Uninitialized); + + Char *dst = const_cast(result.cbegin()); + Char *ptr = dst; + forever { + while (src != end && isSpace(*src)) + ++src; + while (src != end && !isSpace(*src)) + *ptr++ = *src++; + if (src != end) + *ptr++ = QChar::Space; + else + break; + } + if (ptr != dst && ptr[-1] == QChar::Space) + --ptr; + + int newlen = ptr - dst; + if (newlen == str.size()) { + // nothing happened, return the original + return str; + } + result.resize(ptr - dst); + return qMove(result); + } +}; + +QT_END_NAMESPACE + +#endif // QSTRINGALGORITHMS_P_H -- cgit v1.2.3