2018-03-05 10:55:39 +01:00
|
|
|
/////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
//
|
2019-01-09 15:15:34 +01:00
|
|
|
// Copyright (C) 2018- Equinor ASA
|
2018-03-05 10:55:39 +01:00
|
|
|
//
|
|
|
|
|
// ResInsight is free software: you can redistribute it and/or modify
|
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
//
|
|
|
|
|
// ResInsight is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
|
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
|
//
|
|
|
|
|
// See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
|
|
|
|
|
// for more details.
|
|
|
|
|
//
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
#include "RiaTextStringTools.h"
|
2024-09-30 11:21:17 +02:00
|
|
|
#include "RiaStdStringTools.h"
|
2018-03-05 10:55:39 +01:00
|
|
|
|
2020-02-21 11:28:54 +01:00
|
|
|
#include <QRegularExpression>
|
2018-03-05 10:55:39 +01:00
|
|
|
#include <QString>
|
2020-09-02 15:04:48 +02:00
|
|
|
#include <QStringList>
|
2018-03-05 10:55:39 +01:00
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2019-09-06 10:40:57 +02:00
|
|
|
///
|
2018-03-05 10:55:39 +01:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
2019-09-06 10:40:57 +02:00
|
|
|
bool RiaTextStringTools::compare( const QString& expected, const QString& actual )
|
2018-03-05 10:55:39 +01:00
|
|
|
{
|
|
|
|
|
// Suggestions for improvement
|
|
|
|
|
// 1. report line number for first change
|
|
|
|
|
// 2. report line numbers for all changes
|
|
|
|
|
// 3. add support for compare with content of a text file on disk
|
|
|
|
|
|
2023-06-26 13:12:41 +02:00
|
|
|
return expected.compare( actual ) == 0;
|
2018-03-05 10:55:39 +01:00
|
|
|
}
|
|
|
|
|
|
2018-05-23 07:48:19 +02:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
2019-09-06 10:40:57 +02:00
|
|
|
///
|
2018-05-23 07:48:19 +02:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
2019-09-06 10:40:57 +02:00
|
|
|
QString RiaTextStringTools::trimAndRemoveDoubleSpaces( const QString& s )
|
2018-05-23 07:48:19 +02:00
|
|
|
{
|
2019-09-06 10:40:57 +02:00
|
|
|
int length;
|
2018-05-23 07:48:19 +02:00
|
|
|
QString trimmed = s.trimmed();
|
|
|
|
|
|
2019-09-06 10:40:57 +02:00
|
|
|
do
|
2018-05-23 07:48:19 +02:00
|
|
|
{
|
2019-09-06 10:40:57 +02:00
|
|
|
length = trimmed.size();
|
|
|
|
|
trimmed = trimmed.replace( " ", " " );
|
|
|
|
|
} while ( trimmed.size() < length );
|
|
|
|
|
|
2018-05-23 07:48:19 +02:00
|
|
|
return trimmed;
|
|
|
|
|
}
|
2020-09-02 15:04:48 +02:00
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
|
///
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2020-10-09 08:38:18 +02:00
|
|
|
QString RiaTextStringTools::commonRoot( const QStringList& stringList )
|
2020-09-02 15:04:48 +02:00
|
|
|
{
|
2020-10-14 18:55:17 +02:00
|
|
|
QString root;
|
|
|
|
|
if ( !stringList.isEmpty() )
|
2020-09-02 15:04:48 +02:00
|
|
|
{
|
2020-10-14 18:55:17 +02:00
|
|
|
root = stringList.front();
|
|
|
|
|
for ( const auto& item : stringList )
|
2020-09-02 15:04:48 +02:00
|
|
|
{
|
2020-10-14 18:55:17 +02:00
|
|
|
if ( root.length() > item.length() )
|
|
|
|
|
{
|
|
|
|
|
root.truncate( item.length() );
|
|
|
|
|
}
|
2020-09-02 15:04:48 +02:00
|
|
|
|
2020-10-14 18:55:17 +02:00
|
|
|
for ( int i = 0; i < root.length(); ++i )
|
2020-09-02 15:04:48 +02:00
|
|
|
{
|
2020-10-14 18:55:17 +02:00
|
|
|
if ( root[i] != item[i] )
|
|
|
|
|
{
|
|
|
|
|
root.truncate( i );
|
|
|
|
|
break;
|
|
|
|
|
}
|
2020-09-02 15:04:48 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return root;
|
|
|
|
|
}
|
2020-10-09 08:38:18 +02:00
|
|
|
|
2021-02-26 14:27:59 +01:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
|
///
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
|
QString RiaTextStringTools::commonSuffix( const QStringList& stringList )
|
|
|
|
|
{
|
|
|
|
|
QString suffix;
|
|
|
|
|
if ( !stringList.isEmpty() )
|
|
|
|
|
{
|
|
|
|
|
suffix = stringList.back();
|
|
|
|
|
for ( const auto& item : stringList )
|
|
|
|
|
{
|
|
|
|
|
if ( suffix.length() > item.length() )
|
|
|
|
|
{
|
|
|
|
|
suffix = suffix.right( item.length() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for ( int i = 0; i < suffix.length(); i++ )
|
|
|
|
|
{
|
|
|
|
|
int suffixIndex = suffix.length() - i - 1;
|
|
|
|
|
int itemIndex = item.length() - i - 1;
|
|
|
|
|
if ( suffix[suffixIndex] != item[itemIndex] )
|
|
|
|
|
{
|
|
|
|
|
suffix = suffix.right( i );
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return suffix;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-09 08:38:18 +02:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
|
///
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
|
QString RiaTextStringTools::trimNonAlphaNumericCharacters( const QString& s )
|
|
|
|
|
{
|
|
|
|
|
QString trimmedString = s;
|
|
|
|
|
QRegularExpression trimRe( "[^a-zA-Z0-9]+$" );
|
|
|
|
|
trimmedString.replace( trimRe, "" );
|
|
|
|
|
return trimmedString;
|
|
|
|
|
}
|
2022-03-11 13:24:01 +01:00
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
|
///
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
|
QStringList RiaTextStringTools::splitSkipEmptyParts( const QString& text, const QString& sep /*= " " */ )
|
|
|
|
|
{
|
2024-09-30 11:21:17 +02:00
|
|
|
bool skipEmptyParts = true;
|
|
|
|
|
return splitString( text, sep, skipEmptyParts );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
|
///
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
|
QStringList RiaTextStringTools::splitString( const QString& text, const QString& sep, bool skipEmptyParts )
|
|
|
|
|
{
|
|
|
|
|
return text.split( sep, skipEmptyParts ? Qt::SkipEmptyParts : Qt::KeepEmptyParts, Qt::CaseInsensitive );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
|
///
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
|
QStringList RiaTextStringTools::splitString( const QString& text, const QRegExp& regExp, bool skipEmptyParts )
|
|
|
|
|
{
|
|
|
|
|
return regExp.splitString( text, skipEmptyParts ? Qt::SkipEmptyParts : Qt::KeepEmptyParts );
|
2022-03-11 13:24:01 +01:00
|
|
|
}
|
|
|
|
|
|
2022-10-12 12:41:08 +02:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
|
///
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2023-02-26 10:48:40 +01:00
|
|
|
QString RiaTextStringTools::replaceTemplateTextWithValues( const QString& templateText, const std::map<QString, QString>& valueMap )
|
2022-10-12 12:41:08 +02:00
|
|
|
{
|
|
|
|
|
QString resolvedText = templateText;
|
|
|
|
|
|
|
|
|
|
// Use a regular expression to find all occurrences of ${key} in the text and replace with the value
|
|
|
|
|
|
|
|
|
|
for ( const auto& [key, value] : valueMap )
|
|
|
|
|
{
|
|
|
|
|
QString regexString = key;
|
|
|
|
|
regexString.replace( "$", "\\$" );
|
|
|
|
|
regexString += "\\b";
|
|
|
|
|
|
|
|
|
|
QRegularExpression rx( regexString );
|
|
|
|
|
|
|
|
|
|
resolvedText.replace( rx, value );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return resolvedText;
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-30 11:21:17 +02:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
|
/// Qt recommends pass-by-value instead of pass-by-const-ref for QStringView
|
|
|
|
|
/// https://doc.qt.io/qt-6/qstringview.html
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
|
bool RiaTextStringTools::isTextEqual( QStringView text, QStringView compareText )
|
|
|
|
|
{
|
|
|
|
|
return text.compare( compareText, Qt::CaseInsensitive ) == 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
|
///
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
|
bool RiaTextStringTools::isNumber( const QString& text, const QString& decimalPoint )
|
|
|
|
|
{
|
|
|
|
|
if ( text.isEmpty() || decimalPoint.isEmpty() )
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto stdString = text.toStdString();
|
|
|
|
|
auto decimalChar = decimalPoint.toLatin1()[0];
|
|
|
|
|
|
|
|
|
|
return RiaStdStringTools::isNumber( stdString, decimalChar );
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-11 13:24:01 +01:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
|
///
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
|
QStringList RiaTextStringTools::splitSkipEmptyParts( const QString& text, const QRegExp& regExp )
|
|
|
|
|
{
|
2024-09-30 11:21:17 +02:00
|
|
|
bool skipEmptyParts = true;
|
|
|
|
|
|
|
|
|
|
return splitString( text, regExp, skipEmptyParts );
|
2022-03-11 13:24:01 +01:00
|
|
|
}
|
2023-05-12 12:41:52 +02:00
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
|
///
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
|
std::strong_ordering operator<=>( const QString& lhs, const QString& rhs )
|
|
|
|
|
{
|
|
|
|
|
return lhs.compare( rhs ) <=> 0;
|
|
|
|
|
}
|