From d35ed2eb1b05a48fd45d91fb75938972893afc02 Mon Sep 17 00:00:00 2001 From: LoRd_MuldeR Date: Thu, 25 Dec 2014 00:49:29 +0100 Subject: [PATCH] Added helper function for parsing regular expressions. --- include/MUtils/Global.h | 5 +++++ src/Global.cpp | 31 +++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/include/MUtils/Global.h b/include/MUtils/Global.h index 286efa4..0d6822d 100644 --- a/include/MUtils/Global.h +++ b/include/MUtils/Global.h @@ -95,6 +95,11 @@ namespace MUtils MUTILS_API QString clean_file_name(const QString &name); MUTILS_API QString clean_file_path(const QString &path); + //Regular expressions + MUTILS_API bool regexp_parse_uint32(const QRegExp ®exp, quint32 &value); + MUTILS_API bool regexp_parse_uint32(const QRegExp ®exp, quint32 *values, const size_t &count); + + //Internationalization MUTILS_API QStringList available_codepages(const bool &noAliases = true); //Internal diff --git a/src/Global.cpp b/src/Global.cpp index 6c4be19..3741d87 100644 --- a/src/Global.cpp +++ b/src/Global.cpp @@ -395,6 +395,37 @@ QString MUtils::clean_file_path(const QString &path) } /////////////////////////////////////////////////////////////////////////////// +// REGULAR EXPESSION HELPER +/////////////////////////////////////////////////////////////////////////////// + +bool MUtils::regexp_parse_uint32(const QRegExp ®exp, quint32 &value) +{ + return regexp_parse_uint32(regexp, &value, 1); +} + +bool MUtils::regexp_parse_uint32(const QRegExp ®exp, quint32 *values, const size_t &count) +{ + const QStringList caps = regexp.capturedTexts(); + + if(caps.isEmpty() || (quint32(caps.count()) <= count)) + { + return false; + } + + for(size_t i = 0; i < count; i++) + { + bool ok = false; + values[i] = caps[i+1].toUInt(&ok); + if(!ok) + { + return false; + } + } + + return true; +} + +/////////////////////////////////////////////////////////////////////////////// // AVAILABLE CODEPAGES /////////////////////////////////////////////////////////////////////////////// -- 2.11.0