File indexing completed on 2024-11-10 04:57:23
0001 /* 0002 KWin - the KDE window manager 0003 This file is part of the KDE project. 0004 0005 SPDX-FileCopyrightText: 2023 Xaver Hugl <xaver.hugl@gmail.com> 0006 0007 SPDX-License-Identifier: GPL-2.0-or-later 0008 */ 0009 #include "version.h" 0010 0011 #include <QChar> 0012 #include <QList> 0013 0014 namespace KWin 0015 { 0016 0017 Version::Version(uint32_t major, uint32_t minor, uint32_t patch) 0018 : m_major(major) 0019 , m_minor(minor) 0020 , m_patch(patch) 0021 { 0022 } 0023 0024 bool Version::isValid() const 0025 { 0026 return m_major > 0 || m_minor > 0 || m_patch > 0; 0027 } 0028 0029 uint32_t Version::majorVersion() const 0030 { 0031 return m_major; 0032 } 0033 0034 uint32_t Version::minorVersion() const 0035 { 0036 return m_minor; 0037 } 0038 0039 uint32_t Version::patchVersion() const 0040 { 0041 return m_patch; 0042 } 0043 0044 Version Version::parseString(QByteArrayView versionString) 0045 { 0046 // Skip any leading non digit 0047 int start = 0; 0048 while (start < versionString.length() && !QChar::fromLatin1(versionString[start]).isDigit()) { 0049 start++; 0050 } 0051 0052 // Strip any non digit, non '.' characters from the end 0053 int end = start; 0054 while (end < versionString.length() && (versionString[end] == '.' || QChar::fromLatin1(versionString[end]).isDigit())) { 0055 end++; 0056 } 0057 0058 const QByteArray result = versionString.toByteArray().mid(start, end - start); 0059 const QList<QByteArray> tokens = result.split('.'); 0060 if (tokens.empty()) { 0061 return Version(0, 0, 0); 0062 } 0063 const uint64_t major = tokens.at(0).toInt(); 0064 const uint64_t minor = tokens.count() > 1 ? tokens.at(1).toInt() : 0; 0065 const uint64_t patch = tokens.count() > 2 ? tokens.at(2).toInt() : 0; 0066 0067 return Version(major, minor, patch); 0068 } 0069 0070 QString Version::toString() const 0071 { 0072 if (m_patch == 0) { 0073 return QString::number(m_major) + '.' + QString::number(m_minor); 0074 } else { 0075 return QString::number(m_major) + '.' + QString::number(m_minor) + '.' + QString::number(m_patch); 0076 } 0077 } 0078 0079 QByteArray Version::toByteArray() const 0080 { 0081 if (m_patch == 0) { 0082 return QByteArray::number(m_major) + '.' + QByteArray::number(m_minor); 0083 } else { 0084 return QByteArray::number(m_major) + '.' + QByteArray::number(m_minor) + '.' + QByteArray::number(m_patch); 0085 } 0086 } 0087 }