From a1ae0e00672c272300923da8e4418480700a9242 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Kvalsvik?= Date: Mon, 11 Apr 2016 18:14:14 +0200 Subject: [PATCH] Updated various functions to accept string_view To take advantage of parser's internal string_view representation of keyword names, several signatures have been updated to accept string_view. --- opm/parser/eclipse/Parser/Parser.cpp | 52 ++++++++++----------- opm/parser/eclipse/Parser/Parser.hpp | 2 +- opm/parser/eclipse/Parser/ParserKeyword.cpp | 2 +- opm/parser/eclipse/Parser/ParserKeyword.hpp | 2 +- opm/parser/eclipse/RawDeck/RawKeyword.cpp | 15 ++++-- opm/parser/eclipse/RawDeck/RawKeyword.hpp | 3 ++ 6 files changed, 42 insertions(+), 34 deletions(-) diff --git a/opm/parser/eclipse/Parser/Parser.cpp b/opm/parser/eclipse/Parser/Parser.cpp index 8a89947cf..05d4aec7c 100644 --- a/opm/parser/eclipse/Parser/Parser.cpp +++ b/opm/parser/eclipse/Parser/Parser.cpp @@ -43,6 +43,7 @@ namespace Opm { + const std::string emptystr = ""; struct ParserState { @@ -67,7 +68,7 @@ namespace Opm { std::list< file > input_files; std::vector< file* > input_stack; RawKeywordPtr rawKeyword; - std::string nextKeyword; + string_view nextKeyword = emptystr; boost::filesystem::path& current_path() { return this->input_stack.back()->path; @@ -329,11 +330,11 @@ namespace Opm { if( !ParserKeyword::validDeckName( name ) ) return false; - if( m_deckParserKeywords.count( name.string() ) ) - return true; + if( m_deckParserKeywords.count( name ) ) + return true; - return matchingKeyword( name ); -} + return matchingKeyword( name ); + } void Parser::addParserKeyword( std::unique_ptr< const ParserKeyword >&& parserKeyword) { /* since string_view's don't own their memory, to update a value in the map @@ -371,22 +372,22 @@ void Parser::addParserKeyword(const Json::JsonObject& jsonKeyword) { const ParserKeyword* Parser::getKeyword( const std::string& name ) const { - auto iter = m_deckParserKeywords.find( name ); - - if (iter == m_deckParserKeywords.end()) - throw std::invalid_argument( "Keyword '" + name + "' not found" ); - - return iter->second; + return getParserKeywordFromDeckName( string_view( name ) ); } -const ParserKeyword* Parser::getParserKeywordFromDeckName(const std::string& deckKeywordName) const { - if( m_deckParserKeywords.count( deckKeywordName ) ) - return m_deckParserKeywords.at(deckKeywordName); +const ParserKeyword* Parser::getParserKeywordFromDeckName( const std::string& name ) const { + return getParserKeywordFromDeckName( string_view( name ) ); +} - const auto* wildCardKeyword = matchingKeyword( deckKeywordName ); +const ParserKeyword* Parser::getParserKeywordFromDeckName(const string_view& name ) const { + auto candidate = m_deckParserKeywords.find( name ); + + if( candidate != m_deckParserKeywords.end() ) return candidate->second; + + const auto* wildCardKeyword = matchingKeyword( name ); if ( !wildCardKeyword ) - throw std::invalid_argument("Do not have parser keyword for parsing: " + deckKeywordName); + throw std::invalid_argument( "Do not have parser keyword for parsing: " + name ); return wildCardKeyword; } @@ -471,8 +472,8 @@ bool Parser::parseState(std::shared_ptr parserState) const { throw std::invalid_argument("Input JSON object is not an array"); } - RawKeywordPtr Parser::createRawKeyword(const std::string& initialLine, std::shared_ptr parserState) const { - std::string keywordString = ParserKeyword::getDeckName(initialLine); + RawKeywordPtr Parser::createRawKeyword(const string_view& kw, std::shared_ptr parserState) const { + auto keywordString = ParserKeyword::getDeckName( kw ); if( !isRecognizedKeyword( keywordString ) ) { if( ParserKeyword::validDeckName( keywordString ) ) { @@ -579,18 +580,17 @@ bool Parser::parseState(std::shared_ptr parserState) const { bool Parser::tryParseKeyword(std::shared_ptr parserState) const { if (parserState->nextKeyword.length() > 0) { parserState->rawKeyword = createRawKeyword(parserState->nextKeyword, parserState); - parserState->nextKeyword = ""; + parserState->nextKeyword = emptystr; } if (parserState->rawKeyword && parserState->rawKeyword->isFinished()) return true; - string_view liner; - while (getline(parserState->input(), liner)) { - liner = strip_comments( liner ); - liner = trim( liner ); // Removing garbage (eg. \r) + string_view line; + while (getline(parserState->input(), line)) { + line = strip_comments( line ); + line = trim( line ); // Removing garbage (eg. \r) - auto line = liner.string(); std::string keywordString; parserState->line()++; @@ -599,7 +599,7 @@ bool Parser::parseState(std::shared_ptr parserState) const { continue; if (parserState->rawKeyword == NULL) { - if (RawKeyword::isKeywordPrefix(liner, keywordString)) { + if (RawKeyword::isKeywordPrefix(line, keywordString)) { parserState->rawKeyword = createRawKeyword(keywordString, parserState); } else /* We are looking at some random gibberish?! */ @@ -612,7 +612,7 @@ bool Parser::parseState(std::shared_ptr parserState) const { return true; } } - parserState->rawKeyword->addRawRecordString(line); + parserState->rawKeyword->addRawRecordString(line.string()); } if (parserState->rawKeyword diff --git a/opm/parser/eclipse/Parser/Parser.hpp b/opm/parser/eclipse/Parser/Parser.hpp index 4856629be..7f9e5f52a 100644 --- a/opm/parser/eclipse/Parser/Parser.hpp +++ b/opm/parser/eclipse/Parser/Parser.hpp @@ -118,7 +118,7 @@ namespace Opm { bool tryParseKeyword(std::shared_ptr parserState) const; bool parseState(std::shared_ptr parserState) const; - std::shared_ptr< RawKeyword > createRawKeyword(const std::string& keywordString, std::shared_ptr parserState) const; + std::shared_ptr< RawKeyword > createRawKeyword(const string_view& keywordString, std::shared_ptr parserState) const; void addDefaultKeywords(); }; diff --git a/opm/parser/eclipse/Parser/ParserKeyword.cpp b/opm/parser/eclipse/Parser/ParserKeyword.cpp index f808b8c36..001ebeaf4 100644 --- a/opm/parser/eclipse/Parser/ParserKeyword.cpp +++ b/opm/parser/eclipse/Parser/ParserKeyword.cpp @@ -220,7 +220,7 @@ namespace Opm { return std::all_of( name.begin() + 1, name.end(), ok ); } - std::string ParserKeyword::getDeckName( const string_view& str ) { + string_view ParserKeyword::getDeckName( const string_view& str ) { auto first_sep = std::find_if( str.begin(), str.end(), RawConsts::is_separator ); diff --git a/opm/parser/eclipse/Parser/ParserKeyword.hpp b/opm/parser/eclipse/Parser/ParserKeyword.hpp index 827e49ff2..c460625c1 100644 --- a/opm/parser/eclipse/Parser/ParserKeyword.hpp +++ b/opm/parser/eclipse/Parser/ParserKeyword.hpp @@ -64,7 +64,7 @@ namespace Opm { typedef std::set SectionNameSet; - static std::string getDeckName(const string_view& rawString); + static string_view 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); diff --git a/opm/parser/eclipse/RawDeck/RawKeyword.cpp b/opm/parser/eclipse/RawDeck/RawKeyword.cpp index 32b7d96f1..4da0950f0 100644 --- a/opm/parser/eclipse/RawDeck/RawKeyword.cpp +++ b/opm/parser/eclipse/RawDeck/RawKeyword.cpp @@ -27,17 +27,22 @@ namespace Opm { - RawKeyword::RawKeyword(const std::string& name, Raw::KeywordSizeEnum sizeType , const std::string& filename, size_t lineNR) { + RawKeyword::RawKeyword(const string_view& name, Raw::KeywordSizeEnum sizeType , const std::string& filename, size_t lineNR) { if (sizeType == Raw::SLASH_TERMINATED || sizeType == Raw::UNKNOWN) { - commonInit(name,filename,lineNR); + commonInit(name.string(),filename,lineNR); m_sizeType = sizeType; } else throw std::invalid_argument("Error - invalid sizetype on input"); } - RawKeyword::RawKeyword(const std::string& name , const std::string& filename, size_t lineNR , size_t inputSize, bool isTableCollection ) { - commonInit(name,filename,lineNR); + RawKeyword::RawKeyword(const std::string& name, Raw::KeywordSizeEnum sizeType , const std::string& filename, size_t lineNR) : + RawKeyword( string_view( name ), sizeType, filename, lineNR ) {} + RawKeyword::RawKeyword(const std::string& name , const std::string& filename, size_t lineNR , size_t inputSize, bool isTableCollection ) : + RawKeyword(string_view( name ), filename, lineNR, inputSize, isTableCollection ) {} + + RawKeyword::RawKeyword(const string_view& name , const std::string& filename, size_t lineNR , size_t inputSize, bool isTableCollection ) { + commonInit(name.string(),filename,lineNR); if (isTableCollection) { m_sizeType = Raw::TABLE_COLLECTION; m_numTables = inputSize; @@ -125,7 +130,7 @@ namespace Opm { // make the keyword string ALL_UPPERCASE because Eclipse seems // to be case-insensitive (although this is one of its // undocumented features...) - keyword = uppercase( ParserKeyword::getDeckName( line ) ); + keyword = uppercase( ParserKeyword::getDeckName( line ).string() ); return isValidKeyword( keyword ); } diff --git a/opm/parser/eclipse/RawDeck/RawKeyword.hpp b/opm/parser/eclipse/RawDeck/RawKeyword.hpp index 9e3ca25cc..73f3d124b 100644 --- a/opm/parser/eclipse/RawDeck/RawKeyword.hpp +++ b/opm/parser/eclipse/RawDeck/RawKeyword.hpp @@ -39,6 +39,9 @@ namespace Opm { class RawKeyword { public: + RawKeyword(const string_view& name , Raw::KeywordSizeEnum sizeType , const std::string& filename, size_t lineNR); + RawKeyword(const string_view& name , const std::string& filename, size_t lineNR , size_t inputSize , bool isTableCollection = false); + RawKeyword(const std::string& name , Raw::KeywordSizeEnum sizeType , const std::string& filename, size_t lineNR); RawKeyword(const std::string& name , const std::string& filename, size_t lineNR , size_t inputSize , bool isTableCollection = false);