From ff301622b64cb9e17f4824f6eced69c2ba2c24d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Kvalsvik?= Date: Wed, 27 Apr 2016 15:02:45 +0200 Subject: [PATCH] Append newline to input; remove branch in getline By allowing getline to assume that the next line always starts after the found \n *or* that after the found newline is the \0, we can avoid a branch and potential stall. --- opm/parser/eclipse/Parser/Parser.cpp | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/opm/parser/eclipse/Parser/Parser.cpp b/opm/parser/eclipse/Parser/Parser.cpp index 951ba5b08..706d6d2ee 100644 --- a/opm/parser/eclipse/Parser/Parser.cpp +++ b/opm/parser/eclipse/Parser/Parser.cpp @@ -133,9 +133,13 @@ inline bool getline( string_view& input, string_view& line ) { auto end = std::find( input.begin(), input.end(), '\n' ); line = string_view( input.begin(), end ); - const auto mod = end == input.end() ? 0 : 1; - input = string_view( end + mod, input.end() ); + input = string_view( end + 1, input.end() ); return true; + + /* we know that we always append a newline onto the input string, so we can + * safely assume that end+1 will either be end-of-input (i.e. empty range) + * or the start of the next line + */ } /* @@ -154,7 +158,7 @@ inline std::string clean( const std::string& str ) { //if( line.begin() == line.end() ) continue; dst.append( line.begin(), line.end() ); - dst.append( 1, '\n' ); + dst.push_back( '\n' ); } return dst; @@ -249,7 +253,7 @@ ParserState::ParserState(const ParseContext& __parseContext) {} void ParserState::loadString(const std::string& input) { - this->input_stack.push( clean( input ) ); + this->input_stack.push( clean( input + "\n" ) ); } void ParserState::loadFile(const boost::filesystem::path& inputFile) { @@ -282,9 +286,10 @@ void ParserState::loadFile(const boost::filesystem::path& inputFile) { auto* fp = ufp.get(); std::string buffer; std::fseek( fp, 0, SEEK_END ); - buffer.resize( std::ftell( fp ) ); + buffer.resize( std::ftell( fp ) + 1 ); std::rewind( fp ); - std::fread( &buffer[ 0 ], 1, buffer.size(), fp ); + std::fread( &buffer[ 0 ], 1, buffer.size() - 1, fp ); + buffer.back() = '\n'; this->input_stack.push( clean( buffer ), inputFileCanonical ); }