From 31e9a7e10c9e13ba757a6e7f2fad4641654f234a Mon Sep 17 00:00:00 2001 From: Reijo Tomperi Date: Sat, 27 Dec 2008 20:30:54 +0000 Subject: [PATCH] preprocessor optimized to handle faster removal of spaces near newline --- preprocessor.cpp | 40 +++++++++++++++++++++++++++------------- preprocessor.h | 8 ++++++++ 2 files changed, 35 insertions(+), 13 deletions(-) diff --git a/preprocessor.cpp b/preprocessor.cpp index 66a94ba90..de1ab9fd1 100644 --- a/preprocessor.cpp +++ b/preprocessor.cpp @@ -149,6 +149,30 @@ void Preprocessor::preprocess(std::istream &istr, std::map 0 && tmp[prev] == '\n' ) || + ( i+1 < str.size() && str[i+1] == '\n' ) + ) + ) + { + // Ignore space that has new line in either side of it + } + else + { + tmp.append( 1,str[i] ); + prev++; + } + } + + return tmp; +} + void Preprocessor::preprocess(std::istream &istr, const std::string &filename, std::string &processedFile, std::list &resultConfigurations) { processedFile = read(istr, filename); @@ -160,21 +184,11 @@ void Preprocessor::preprocess(std::istream &istr, const std::string &filename, s if ( !processedFile.empty() && processedFile[0] == ' ' ) processedFile.erase( 0, processedFile.find_first_not_of(" ") ); - // TODO, this is very slow with very big files, make it faster - std::string::size_type loc = 0; - while ( (loc = processedFile.find("\n ", loc)) != std::string::npos ) - processedFile.erase( 1 + loc, 1 ); - - // Remove all trailing spaces.. - loc = 0; - while ( (loc = processedFile.find(" \n", loc)) != std::string::npos ) - { - processedFile.erase( loc, 1 ); - if ( loc > 0 ) - --loc; - } + // Remove space characters that are after or before new line character + processedFile = removeSpaceNearNL( processedFile ); // Using the backslash at the end of a line.. + std::string::size_type loc = 0; while ( (loc = processedFile.rfind("\\\n")) != std::string::npos ) { processedFile.erase(loc, 2); diff --git a/preprocessor.h b/preprocessor.h index 8424846e8..e77620df6 100644 --- a/preprocessor.h +++ b/preprocessor.h @@ -63,6 +63,14 @@ public: private: + /** + * Remove space that has new line character on left or right side of it. + * + * @param str The string to be converted + * @return The string where space characters have been removed. + */ + static std::string removeSpaceNearNL( const std::string &str ); + /** * Get all possible configurations. By looking at the ifdefs and ifndefs in filedata */