Unverified Commit db46521d authored by Richard Berger's avatar Richard Berger
Browse files

Add utils::count_words and utils::trim_comment

parent 807130c7
Loading
Loading
Loading
Loading
+22 −0
Changes for src/utils.cpp: 22 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@
#include <cstdlib>
#include "lammps.h"
#include "error.h"
#include "tokenizer.h"

#if defined(__linux__)
#include <unistd.h>  // for readlink
@@ -326,6 +327,26 @@ tagint utils::tnumeric(const char *file, int line, const char *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;
  }

}
+16 −0
Changes for src/utils.h: 16 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -126,6 +126,22 @@ namespace LAMMPS_NS {
     */
    tagint tnumeric(const char *file, int line, const char *str,
                    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");
  }
}

+4 −0
Changes for unittest/utils/CMakeLists.txt: 4 added lines, 0 removed lines.
Original line number Diff line number Diff line
add_executable(test_tokenizer test_tokenizer.cpp)
target_link_libraries(test_tokenizer PRIVATE lammps GTest::GMockMain GTest::GMock GTest::GTest)
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)
 No newline at end of file
+16 −0
Changes for unittest/utils/test_utils.cpp: 16 added lines, 0 removed lines.
Original line number Diff line number Diff line
#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);
}
 No newline at end of file