From 6cb5345cd086a29a975b7fe99490e5fd1cdb1251 Mon Sep 17 00:00:00 2001 From: Richard Berger Date: Thu, 11 Jun 2020 13:37:22 -0400 Subject: [PATCH] Add optimized version of count_words for default whitespace chars --- src/utils.cpp | 29 +++++++++++++++++++++++++++++ src/utils.h | 10 +++++++++- unittest/utils/test_utils.cpp | 4 ++++ 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/src/utils.cpp b/src/utils.cpp index 928a84883c..86f56e7b2a 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -368,6 +368,35 @@ std::string utils::trim_comment(const std::string & line) { Return number of words ------------------------------------------------------------------------- */ +size_t utils::count_words(const std::string & text) { + size_t count = 0; + const char * buf = text.c_str(); + char c = *buf; + + while (c) { + if (c == ' ' || c == '\t' || c == '\r' || c == '\n' || c == '\f') { + c = *++buf; + continue; + }; + + ++count; + c = *++buf; + + while (c) { + if (c == ' ' || c == '\t' || c == '\r' || c == '\n' || c == '\f') { + break; + } + c = *++buf; + } + } + + return count; +} + +/* ---------------------------------------------------------------------- + Return number of words +------------------------------------------------------------------------- */ + size_t utils::count_words(const std::string & text, const std::string & separators) { size_t count = 0; size_t start = text.find_first_not_of(separators); diff --git a/src/utils.h b/src/utils.h index 562293f2f3..bce9ff3e66 100644 --- a/src/utils.h +++ b/src/utils.h @@ -156,7 +156,15 @@ namespace LAMMPS_NS { * \param separators string containing characters that will be treated as whitespace * \return number of words found */ - size_t count_words(const std::string & text, const std::string & separators = " \t\r\n\f"); + size_t count_words(const std::string & text, const std::string & separators); + + /** + * \brief Count words in string, ignore any whitespace matching " \t\r\n\f" + * \param text string that should be searched + * \param separators string containing characters that will be treated as whitespace + * \return number of words found + */ + size_t count_words(const std::string & text); /** * \brief Count words in a single line, trim anything from '#' onward diff --git a/unittest/utils/test_utils.cpp b/unittest/utils/test_utils.cpp index 9830207c3e..5660c097f1 100644 --- a/unittest/utils/test_utils.cpp +++ b/unittest/utils/test_utils.cpp @@ -28,6 +28,10 @@ TEST(Utils, count_words) { ASSERT_EQ(utils::count_words("some text # comment"), 4); } +TEST(Utils, count_words_non_default) { + ASSERT_EQ(utils::count_words("some text # comment", " #"), 3); +} + TEST(Utils, trim_and_count_words) { ASSERT_EQ(utils::trim_and_count_words("some text # comment"), 2); }