Add utils::count_words and utils::trim_comment

This commit is contained in:
Richard Berger
2020-05-15 15:46:38 -04:00
parent 807130c771
commit db46521d64
4 changed files with 58 additions and 0 deletions

View File

@ -16,6 +16,7 @@
#include <cstdlib> #include <cstdlib>
#include "lammps.h" #include "lammps.h"
#include "error.h" #include "error.h"
#include "tokenizer.h"
#if defined(__linux__) #if defined(__linux__)
#include <unistd.h> // for readlink #include <unistd.h> // for readlink
@ -326,6 +327,26 @@ tagint utils::tnumeric(const char *file, int line, const char *str,
return ATOTAGINT(str); return ATOTAGINT(str);
} }
/* ----------------------------------------------------------------------
Return string without trailing # comment
------------------------------------------------------------------------- */
std::string utils::trim_comment(const std::string & line) {
auto end = line.find_first_of("#");
if (end != std::string::npos) {
return line.substr(0, end);
}
return std::string(line);
}
/* ----------------------------------------------------------------------
Trim comment from string and return number of words
------------------------------------------------------------------------- */
size_t utils::count_words(const std::string & text, const std::string & seperators) {
Tokenizer words(utils::trim_comment(text), seperators);
return words.count();
}
/* ------------------------------------------------------------------ */ /* ------------------------------------------------------------------ */
@ -668,4 +689,5 @@ extern "C" {
return 0; return 0;
} }
} }

View File

@ -126,6 +126,22 @@ namespace LAMMPS_NS {
*/ */
tagint tnumeric(const char *file, int line, const char *str, tagint tnumeric(const char *file, int line, const char *str,
bool do_abort, LAMMPS *lmp); bool do_abort, LAMMPS *lmp);
/**
* \brief Trim anything from '#' onward
* \param line string that should be trimmed
* \return new string without comment (string)
*/
std::string trim_comment(const std::string & line);
/**
* \brief Count words in a single line, trim anything from '#' onward
* \param text string that should be trimmed and searched
* \param seperators 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 & seperators = " \t\r\n\f");
} }
} }

View File

@ -1,3 +1,7 @@
add_executable(test_tokenizer test_tokenizer.cpp) add_executable(test_tokenizer test_tokenizer.cpp)
target_link_libraries(test_tokenizer PRIVATE lammps GTest::GMockMain GTest::GMock GTest::GTest) target_link_libraries(test_tokenizer PRIVATE lammps GTest::GMockMain GTest::GMock GTest::GTest)
add_test(Tokenizer test_tokenizer) add_test(Tokenizer test_tokenizer)
add_executable(test_utils test_utils.cpp)
target_link_libraries(test_utils PRIVATE lammps GTest::GMockMain GTest::GMock GTest::GTest)
add_test(Utils test_utils)

View File

@ -0,0 +1,16 @@
#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include "utils.h"
#include <string>
using namespace LAMMPS_NS;
using ::testing::Eq;
TEST(Utils, trim_comment) {
auto trimmed = utils::trim_comment("some text # comment");
ASSERT_THAT(trimmed, Eq("some text "));
}
TEST(Utils, count_words) {
ASSERT_EQ(utils::count_words("some text # comment"), 2);
}