diff --git a/opm/parser/eclipse/Parser/Parser.cpp b/opm/parser/eclipse/Parser/Parser.cpp index 51d57b940..39c73db5c 100644 --- a/opm/parser/eclipse/Parser/Parser.cpp +++ b/opm/parser/eclipse/Parser/Parser.cpp @@ -158,10 +158,10 @@ namespace Opm { of the data section of any keyword. */ - void handleRandomText(const std::string& keywordString ) const { + void handleRandomText(const string_view& keywordString ) const { std::string errorKey; std::stringstream msg; - std::string trimmedCopy = boost::algorithm::trim_copy( keywordString ); + std::string trimmedCopy = keywordString.string(); if (trimmedCopy == "/") { errorKey = ParseContext::PARSE_RANDOM_SLASH; @@ -250,9 +250,8 @@ namespace Opm { 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; + static inline string_view strip_comments( const string_view& str ) { + return { str.begin(), strip_comments( str.begin(), str.end() ) }; } /* stripComments only exists so that the unit tests can verify it. @@ -310,7 +309,7 @@ namespace Opm { return m_internalParserKeywords.at(internalKeywordName).get(); } - const ParserKeyword* Parser::matchingKeyword(const std::string& name) const { + const ParserKeyword* Parser::matchingKeyword(const string_view& name) const { for (auto iter = m_wildCardKeywords.begin(); iter != m_wildCardKeywords.end(); ++iter) { if (iter->second->matches(name)) return iter->second; @@ -322,15 +321,18 @@ namespace Opm { return (m_wildCardKeywords.count(internalKeywordName) > 0); } - bool Parser::isRecognizedKeyword(const std::string& deckKeywordName) const { - if (!ParserKeyword::validDeckName(deckKeywordName)) { - return false; + bool Parser::isRecognizedKeyword(const std::string& name ) const { + return this->isRecognizedKeyword( string_view( name ) ); } - if (m_deckParserKeywords.count(deckKeywordName) > 0) + bool Parser::isRecognizedKeyword(const string_view& name ) const { + if( !ParserKeyword::validDeckName( name ) ) + return false; + + if( m_deckParserKeywords.count( name.string() ) ) return true; - return matchingKeyword( deckKeywordName ); + return matchingKeyword( name ); } void Parser::addParserKeyword( std::unique_ptr< const ParserKeyword >&& parserKeyword) { @@ -549,19 +551,18 @@ bool Parser::parseState(std::shared_ptr parserState) const { return std::find_if_not( rbegin, rend, ws ).base(); } - static inline std::string& trim( std::string& str ) { + static inline string_view trim( const string_view& str ) { auto fst = trim_left( str.begin(), str.end() ); auto lst = trim_right( fst, str.end() ); - return str.assign( fst, lst ); + return { fst, lst }; } - static inline bool getline( string_view& input, std::string& line ) { + static inline bool getline( string_view& input, string_view& line ) { if( input.empty() ) return false; auto end = std::find( input.begin(), input.end(), '\n' ); - line.erase(); - line.assign( input.begin(), end ); + line = string_view( input.begin(), end ); const auto mod = end == input.end() ? 0 : 1; input = string_view( end + mod, input.end() ); return true; @@ -576,10 +577,12 @@ bool Parser::parseState(std::shared_ptr parserState) const { if (parserState->rawKeyword && parserState->rawKeyword->isFinished()) return true; - std::string line; - while (getline(parserState->input(), line)) { - line = strip_comments( line ); - line = trim( line ); // Removing garbage (eg. \r) + string_view liner; + while (getline(parserState->input(), liner)) { + liner = strip_comments( liner ); + liner = trim( liner ); // Removing garbage (eg. \r) + + auto line = liner.string(); doSpecialHandlingForTitleKeyword(line, parserState); std::string keywordString; parserState->line()++; @@ -589,7 +592,7 @@ bool Parser::parseState(std::shared_ptr parserState) const { continue; if (parserState->rawKeyword == NULL) { - if (RawKeyword::isKeywordPrefix(line, keywordString)) { + if (RawKeyword::isKeywordPrefix(liner, keywordString)) { parserState->rawKeyword = createRawKeyword(keywordString, parserState); } else /* We are looking at some random gibberish?! */ diff --git a/opm/parser/eclipse/Parser/Parser.hpp b/opm/parser/eclipse/Parser/Parser.hpp index 2389a58f3..8b480589f 100644 --- a/opm/parser/eclipse/Parser/Parser.hpp +++ b/opm/parser/eclipse/Parser/Parser.hpp @@ -38,6 +38,7 @@ namespace Opm { class Deck; class ParseContext; class RawKeyword; + class string_view; struct ParserState; /// The hub of the parsing process. @@ -66,6 +67,7 @@ namespace Opm { void addParserKeyword(std::unique_ptr< const ParserKeyword >&& parserKeyword); const ParserKeyword* getKeyword(const std::string& name) const; + bool isRecognizedKeyword( const string_view& deckKeywordName) const; bool isRecognizedKeyword( const std::string& deckKeywordName) const; const ParserKeyword* getParserKeywordFromDeckName(const std::string& deckKeywordName) const; std::vector getAllDeckNames () const; @@ -111,7 +113,7 @@ namespace Opm { std::map m_wildCardKeywords; bool hasWildCardKeyword(const std::string& keyword) const; - const ParserKeyword* matchingKeyword(const std::string& keyword) const; + const ParserKeyword* matchingKeyword(const string_view& keyword) const; bool tryParseKeyword(std::shared_ptr parserState) const; bool parseState(std::shared_ptr parserState) const; diff --git a/opm/parser/eclipse/Parser/ParserKeyword.cpp b/opm/parser/eclipse/Parser/ParserKeyword.cpp index 220c62191..f808b8c36 100644 --- a/opm/parser/eclipse/Parser/ParserKeyword.cpp +++ b/opm/parser/eclipse/Parser/ParserKeyword.cpp @@ -201,7 +201,7 @@ namespace Opm { } - bool ParserKeyword::validNameStart(const std::string& name) { + bool ParserKeyword::validNameStart( const string_view& name) { if (name.length() > ParserConst::maxKeywordLength) return false; @@ -220,7 +220,7 @@ namespace Opm { return std::all_of( name.begin() + 1, name.end(), ok ); } - std::string ParserKeyword::getDeckName(const std::string& str ) { + std::string ParserKeyword::getDeckName( const string_view& str ) { auto first_sep = std::find_if( str.begin(), str.end(), RawConsts::is_separator ); @@ -231,7 +231,11 @@ namespace Opm { return { str.begin(), str.begin() + 9 }; } - bool ParserKeyword::validDeckName(const std::string& name) { + bool ParserKeyword::validDeckName( const std::string& name) { + return validDeckName( string_view( name ) ); + } + + bool ParserKeyword::validDeckName( const string_view& name) { if( !validNameStart( name ) ) return false; @@ -558,6 +562,24 @@ namespace Opm { return false; } + bool ParserKeyword::matches(const string_view& name ) const { + if (!validDeckName(name )) + return false; + + else if( m_deckNames.count( name.string() ) ) + return true; + + else if (hasMatchRegex()) { +#ifdef HAVE_REGEX + return std::regex_match( name.begin(), name.end(), m_matchRegex); +#else + return boost::regex_match( name.begin(), name.end(), m_matchRegex); +#endif + } + + return false; + } + bool ParserKeyword::equal(const ParserKeyword& other) const { // compare the deck names. we don't care about the ordering of the strings. if (m_deckNames != other.m_deckNames) diff --git a/opm/parser/eclipse/Parser/ParserKeyword.hpp b/opm/parser/eclipse/Parser/ParserKeyword.hpp index 6965046bd..827e49ff2 100644 --- a/opm/parser/eclipse/Parser/ParserKeyword.hpp +++ b/opm/parser/eclipse/Parser/ParserKeyword.hpp @@ -43,6 +43,7 @@ namespace Opm { class ParserDoubleItem; class ParserRecord; class RawKeyword; + class string_view; class ParserKeyword { public: @@ -63,12 +64,14 @@ namespace Opm { typedef std::set SectionNameSet; - static std::string getDeckName(const std::string& rawString); + static std::string getDeckName(const string_view& rawString); static bool validInternalName(const std::string& name); static bool validDeckName(const std::string& name); + static bool validDeckName(const string_view& name); bool hasMatchRegex() const; void setMatchRegex(const std::string& deckNameRegexp); bool matches(const std::string& deckKeywordName) const; + bool matches(const string_view& ) const; bool hasDimension() const; void addRecord(std::shared_ptr record); void addDataRecord(std::shared_ptr record); @@ -122,7 +125,7 @@ namespace Opm { bool m_isTableCollection; std::string m_Description; - static bool validNameStart(const std::string& name); + static bool validNameStart(const string_view& name); void initDeckNames( const Json::JsonObject& jsonConfig ); void initSectionNames( const Json::JsonObject& jsonConfig ); void initMatchRegex( const Json::JsonObject& jsonObject ); diff --git a/opm/parser/eclipse/RawDeck/RawKeyword.cpp b/opm/parser/eclipse/RawDeck/RawKeyword.cpp index c9e4a02ee..a34522d73 100644 --- a/opm/parser/eclipse/RawDeck/RawKeyword.cpp +++ b/opm/parser/eclipse/RawDeck/RawKeyword.cpp @@ -119,7 +119,7 @@ namespace Opm { return str; } - bool RawKeyword::isKeywordPrefix(const std::string& line, std::string& keyword ) { + bool RawKeyword::isKeywordPrefix(const string_view& line, std::string& keyword ) { // make the keyword string ALL_UPPERCASE because Eclipse seems // to be case-insensitive (although this is one of its // undocumented features...) diff --git a/opm/parser/eclipse/RawDeck/RawKeyword.hpp b/opm/parser/eclipse/RawDeck/RawKeyword.hpp index 36c8164a3..9e3ca25cc 100644 --- a/opm/parser/eclipse/RawDeck/RawKeyword.hpp +++ b/opm/parser/eclipse/RawDeck/RawKeyword.hpp @@ -30,6 +30,7 @@ namespace Opm { class RawRecord; + class string_view; /// Class representing a RawKeyword, meaning both the actual keyword phrase, and the records, /// represented as a list of RawRecord objects. @@ -51,7 +52,7 @@ namespace Opm { // iterator interface. const RawRecord& getFirstRecord( ) const; - static bool isKeywordPrefix(const std::string& line, std::string& keywordName); + static bool isKeywordPrefix(const string_view& line, std::string& keywordName); bool isPartialRecordStringEmpty() const; bool isFinished() const;