Added string trimming functions that trim only the left/right side.
This commit is contained in:
parent
f5cd39eda3
commit
e08022f02c
@ -95,6 +95,12 @@ namespace MUtils
|
|||||||
MUTILS_API bool remove_file(const QString &fileName);
|
MUTILS_API bool remove_file(const QString &fileName);
|
||||||
MUTILS_API bool remove_directory(const QString &folderPath, const bool &recursive);
|
MUTILS_API bool remove_directory(const QString &folderPath, const bool &recursive);
|
||||||
|
|
||||||
|
//String utils
|
||||||
|
MUTILS_API QString& trim_right(QString &str);
|
||||||
|
MUTILS_API QString& trim_left(QString &str);
|
||||||
|
MUTILS_API QString trim_right(const QString &str);
|
||||||
|
MUTILS_API QString trim_left(const QString &str);
|
||||||
|
|
||||||
//String sorting
|
//String sorting
|
||||||
MUTILS_API void natural_string_sort(QStringList &list, const bool bIgnoreCase);
|
MUTILS_API void natural_string_sort(QStringList &list, const bool bIgnoreCase);
|
||||||
|
|
||||||
|
@ -38,6 +38,7 @@
|
|||||||
#include <QTextCodec>
|
#include <QTextCodec>
|
||||||
#include <QPair>
|
#include <QPair>
|
||||||
#include <QListIterator>
|
#include <QListIterator>
|
||||||
|
#include <QMutex>
|
||||||
|
|
||||||
//CRT
|
//CRT
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
@ -111,6 +112,49 @@ QString MUtils::rand_str(const bool &bLong)
|
|||||||
return QString("%1%2").arg(rand_str(false), rand_str(false));
|
return QString("%1%2").arg(rand_str(false), rand_str(false));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
// STRING UTILITY FUNCTIONS
|
||||||
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
static QScopedPointer<QRegExp> g_str_trim_rx_r;
|
||||||
|
static QScopedPointer<QRegExp> g_str_trim_rx_l;
|
||||||
|
static QMutex g_str_trim_lock;
|
||||||
|
|
||||||
|
static QString& trim_helper(QString &str, QScopedPointer<QRegExp> ®ex, const char *const pattern)
|
||||||
|
{
|
||||||
|
QMutexLocker lock(&g_str_trim_lock);
|
||||||
|
if (regex.isNull())
|
||||||
|
{
|
||||||
|
regex.reset(new QRegExp(QLatin1String(pattern)));
|
||||||
|
}
|
||||||
|
str.remove(*regex.data());
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
|
QString& MUtils::trim_right(QString &str)
|
||||||
|
{
|
||||||
|
static const char *const TRIM_RIGHT = "\\s+$";
|
||||||
|
return trim_helper(str, g_str_trim_rx_r, TRIM_RIGHT);
|
||||||
|
}
|
||||||
|
|
||||||
|
QString& MUtils::trim_left(QString &str)
|
||||||
|
{
|
||||||
|
static const char *const TRIM_LEFT = "^\\s+";
|
||||||
|
return trim_helper(str, g_str_trim_rx_l, TRIM_LEFT);
|
||||||
|
}
|
||||||
|
|
||||||
|
QString MUtils::trim_right(const QString &str)
|
||||||
|
{
|
||||||
|
QString temp(str);
|
||||||
|
return trim_right(temp);
|
||||||
|
}
|
||||||
|
|
||||||
|
QString MUtils::trim_left(const QString &str)
|
||||||
|
{
|
||||||
|
QString temp(str);
|
||||||
|
return trim_left(temp);
|
||||||
|
}
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
// GENERATE FILE NAME
|
// GENERATE FILE NAME
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
@ -494,10 +538,10 @@ void MUtils::natural_string_sort(QStringList &list, const bool bIgnoreCase)
|
|||||||
// CLEAN FILE PATH
|
// CLEAN FILE PATH
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
static const char FILENAME_ILLEGAL_CHARS[] = "\\/:*?<>\"";
|
|
||||||
|
|
||||||
QString MUtils::clean_file_name(const QString &name)
|
QString MUtils::clean_file_name(const QString &name)
|
||||||
{
|
{
|
||||||
|
static const char FILENAME_ILLEGAL_CHARS[] = "\\/:*?<>\"";
|
||||||
|
|
||||||
QString result(name);
|
QString result(name);
|
||||||
if (result.contains(QLatin1Char('"')))
|
if (result.contains(QLatin1Char('"')))
|
||||||
{
|
{
|
||||||
@ -519,12 +563,11 @@ QString MUtils::clean_file_name(const QString &name)
|
|||||||
result.replace(QLatin1Char(FILENAME_ILLEGAL_CHARS[i]), QLatin1Char('_'));
|
result.replace(QLatin1Char(FILENAME_ILLEGAL_CHARS[i]), QLatin1Char('_'));
|
||||||
}
|
}
|
||||||
|
|
||||||
const QRegExp spaces("\\s+$");
|
trim_right(result);
|
||||||
result.remove(spaces);
|
|
||||||
while (result.endsWith(QLatin1Char('.')))
|
while (result.endsWith(QLatin1Char('.')))
|
||||||
{
|
{
|
||||||
result.chop(1);
|
result.chop(1);
|
||||||
result.remove(spaces);
|
trim_right(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
|
Loading…
Reference in New Issue
Block a user