From 4b4d2c02c0651e13653d7af0cb4c3788e5967d9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Kvalsvik?= Date: Mon, 4 Apr 2016 20:23:09 +0200 Subject: [PATCH] Reimplementation of stripComments The implementation has been rewritten to use iterators and renamed for internal use. The public static function still exists for testability and easy verification, but should be considered an internal part of the parser. --- opm/parser/eclipse/Parser/Parser.cpp | 66 ++++++++++++++---------- opm/parser/eclipse/RawDeck/RawConsts.hpp | 4 ++ 2 files changed, 42 insertions(+), 28 deletions(-) diff --git a/opm/parser/eclipse/Parser/Parser.cpp b/opm/parser/eclipse/Parser/Parser.cpp index 4ed2d056f..2170a889f 100644 --- a/opm/parser/eclipse/Parser/Parser.cpp +++ b/opm/parser/eclipse/Parser/Parser.cpp @@ -176,6 +176,15 @@ namespace Opm { addDefaultKeywords(); } + template< typename Itr > + static inline Itr find_comment( Itr begin, Itr end ) { + + auto itr = std::find( begin, end, '-' ); + for( ; itr != end; itr = std::find( itr + 1, end, '-' ) ) + if( (itr + 1) != end && *( itr + 1 ) == '-' ) return itr; + + return end; + } /** This function will remove return a copy of the input string @@ -185,38 +194,39 @@ namespace Opm { ABC --Comment => ABC ABC '--Comment1' --Comment2 => ABC '--Comment1' ABC "-- Not balanced quote? => ABC "-- Not balanced quote? - */ - std::string Parser::stripComments(const std::string& inputString) { - std::string uncommentedString; - size_t offset = 0; - while (true) { - size_t commentPos = inputString.find("--" , offset); - if (commentPos == std::string::npos) { - uncommentedString = inputString; - break; - } else { - size_t quoteStart = inputString.find_first_of("'\"" , offset); - if (quoteStart == std::string::npos || quoteStart > commentPos) { - uncommentedString = inputString.substr(0 , commentPos ); - break; - } else { - char quoteChar = inputString[quoteStart]; - size_t quoteEnd = inputString.find( quoteChar , quoteStart + 1); - if (quoteEnd == std::string::npos) { - // Quotes are not balanced - probably an error?! - uncommentedString = inputString; - break; - } else - offset = quoteEnd + 1; - } - } - } + template< typename Itr > + static inline Itr strip_comments( Itr begin, Itr end ) { - return uncommentedString; + auto pos = find_comment( begin, end ); + + if( pos == end ) return end; + + auto qbegin = std::find_if( begin, end, RawConsts::is_quote ); + + if( qbegin == end || qbegin > pos ) + return pos; + + auto qend = std::find( qbegin + 1, end, *qbegin ); + + // Quotes are not balanced - probably an error?! + if( qend == end ) return end; + + return strip_comments( qend + 1, end ); } + static inline std::string& strip_comments( std::string& str ) { + str.erase( strip_comments( str.begin(), str.end() ), str.end() ); + return str; + } + + /* stripComments only exists so that the unit tests can verify it. + * strip_comment is the actual (internal) implementation + */ + std::string Parser::stripComments( const std::string& str ) { + return { str.begin(), strip_comments( str.begin(), str.end() ) }; + } /* About INCLUDE: Observe that the ECLIPSE parser is slightly unlogical @@ -501,7 +511,7 @@ bool Parser::parseState(std::shared_ptr parserState) const { std::string line; while (std::getline(*parserState->inputstream, line)) { if (line.find("--") != std::string::npos) - line = stripComments( line ); + line = strip_comments( line ); line = trim_right( line ); // Removing garbage (eg. \r) line = doSpecialHandlingForTitleKeyword(line, parserState); diff --git a/opm/parser/eclipse/RawDeck/RawConsts.hpp b/opm/parser/eclipse/RawDeck/RawConsts.hpp index 38dbd5c91..98d9d0838 100644 --- a/opm/parser/eclipse/RawDeck/RawConsts.hpp +++ b/opm/parser/eclipse/RawDeck/RawConsts.hpp @@ -38,6 +38,10 @@ namespace Opm { static inline bool is_separator( char ch ) { return ch == '\t' || ch == ' '; } + + static inline bool is_quote( char ch ) { + return ch == quote || ch == '"'; + } } }