diff --git a/src/modules/fsresizer/ResizeFSJob.cpp b/src/modules/fsresizer/ResizeFSJob.cpp index 4a7b0da59..e10894c30 100644 --- a/src/modules/fsresizer/ResizeFSJob.cpp +++ b/src/modules/fsresizer/ResizeFSJob.cpp @@ -29,32 +29,36 @@ #include "utils/Logger.h" ResizeFSJob::RelativeSize::RelativeSize() - : m_unit( None ) - , m_value( 0 ) + : m_value( 0 ) + , m_unit( None ) { } -ResizeFSJob::RelativeSize::RelativeSize( const QString& s) - : m_unit( None ) - , m_value( 0 ) +template +void matchUnitSuffix( + const QString& s, + const char (&suffix)[N], + ResizeFSJob::RelativeSize::Unit matchedUnit, + int& value, + ResizeFSJob::RelativeSize::Unit& unit + ) { - QString valuePart; - - if ( s.endsWith( '%' ) ) + if ( s.endsWith( suffix ) ) { - valuePart = s.left( s.length() - 1 ); - m_unit = Percent; + value = s.left( s.length() - N + 1 ).toInt(); + unit = matchedUnit; } - if ( s.endsWith( "MiB" ) ) - { - valuePart = s.left( s.length() - 3 ); - m_unit = Absolute; - } - - if ( ( m_unit != None ) && !valuePart.isEmpty() ) - m_value = valuePart.toInt(); - +} + + +ResizeFSJob::RelativeSize::RelativeSize( const QString& s) + : m_value( 0 ) + , m_unit( None ) +{ + matchUnitSuffix( s, "%", Percent, m_value, m_unit ); + matchUnitSuffix( s, "MiB", Absolute, m_value, m_unit ); + if ( !m_value ) m_unit = None; } diff --git a/src/modules/fsresizer/ResizeFSJob.h b/src/modules/fsresizer/ResizeFSJob.h index 681328f81..415d04c81 100644 --- a/src/modules/fsresizer/ResizeFSJob.h +++ b/src/modules/fsresizer/ResizeFSJob.h @@ -32,6 +32,7 @@ class PLUGINDLLEXPORT ResizeFSJob : public Calamares::CppJob { Q_OBJECT +public: class RelativeSize { public: @@ -45,15 +46,14 @@ class PLUGINDLLEXPORT ResizeFSJob : public Calamares::CppJob Absolute }; - quint64 value() const { return m_value; } + int value() const { return m_value; } Unit unit() const { return m_unit; } private: - quint64 m_value; + int m_value; Unit m_unit; } ; -public: explicit ResizeFSJob( QObject* parent = nullptr ); virtual ~ResizeFSJob() override;