mirror of
https://github.com/OPM/ResInsight.git
synced 2025-02-25 18:55:39 -06:00
Performance : Use std::string when parsing address strings
This commit is contained in:
@@ -105,6 +105,17 @@ bool RiaStdStringTools::startsWithAlphabetic( const std::string& s )
|
||||
return isalpha( s[0] ) != 0;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
std::string RiaStdStringTools::toUpper( const std::string& s )
|
||||
{
|
||||
auto strCopy( s );
|
||||
std::transform( strCopy.begin(), strCopy.end(), strCopy.begin(), []( unsigned char c ) { return std::toupper( c ); } );
|
||||
|
||||
return strCopy;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
@@ -120,15 +131,25 @@ bool RiaStdStringTools::endsWith( const std::string& mainStr, const std::string&
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
std::vector<std::string> RiaStdStringTools::splitStringBySpace( const std::string& s )
|
||||
std::vector<std::string> RiaStdStringTools::splitString( const std::string& s, char delimiter )
|
||||
{
|
||||
std::vector<std::string> words;
|
||||
|
||||
splitByDelimiter( s, words );
|
||||
splitByDelimiter( s, words, delimiter );
|
||||
|
||||
return words;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
std::string RiaStdStringTools::joinStrings( const std::vector<std::string>& s, char delimiter )
|
||||
{
|
||||
std::string delimiterString( 1, delimiter );
|
||||
|
||||
return join( s.begin(), s.end(), delimiterString );
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
|
||||
@@ -20,7 +20,7 @@
|
||||
|
||||
#include <algorithm>
|
||||
#include <iterator>
|
||||
#include <sstream>
|
||||
#include <numeric>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
@@ -39,9 +39,12 @@ public:
|
||||
static bool containsAlphabetic( const std::string& s );
|
||||
static bool startsWithAlphabetic( const std::string& s );
|
||||
|
||||
static std::string toUpper( const std::string& s );
|
||||
|
||||
static bool endsWith( const std::string& mainStr, const std::string& toMatch );
|
||||
|
||||
static std::vector<std::string> splitStringBySpace( const std::string& s );
|
||||
static std::vector<std::string> splitString( const std::string& s, char delimiter );
|
||||
static std::string joinStrings( const std::vector<std::string>& s, char delimiter );
|
||||
|
||||
static int computeEditDistance( const std::string& x, const std::string& y );
|
||||
|
||||
@@ -57,13 +60,22 @@ private:
|
||||
template <class Container>
|
||||
void RiaStdStringTools::splitByDelimiter( const std::string& str, Container& cont, char delimiter )
|
||||
{
|
||||
std::stringstream ss( str );
|
||||
std::string token;
|
||||
while ( std::getline( ss, token, delimiter ) )
|
||||
size_t start;
|
||||
size_t end = 0;
|
||||
|
||||
while ( ( start = str.find_first_not_of( delimiter, end ) ) != std::string::npos )
|
||||
{
|
||||
if ( token.find_first_not_of( delimiter ) != std::string::npos )
|
||||
{
|
||||
cont.push_back( token );
|
||||
}
|
||||
end = str.find( delimiter, start );
|
||||
cont.push_back( str.substr( start, end - start ) );
|
||||
}
|
||||
}
|
||||
|
||||
template <typename InputIt>
|
||||
std::string join( InputIt begin, InputIt end, const std::string& separator = ", " )
|
||||
{
|
||||
auto compose_key = [&separator]( std::string& key, const std::string& key_part ) -> std::string {
|
||||
return key.empty() ? key_part : key + separator + key_part;
|
||||
};
|
||||
|
||||
return std::accumulate( begin, end, std::string(), compose_key );
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user