Unverified Commit 46239e45 authored by Richard Berger's avatar Richard Berger
Browse files

Add utils::is_double and utils::is_integer

parent 9e3759d0
Loading
Loading
Loading
Loading
+34 −0
Changes for src/utils.cpp: 34 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -348,6 +348,40 @@ size_t utils::count_words(const std::string & text, const std::string & seperato
  return words.count();
}

/* ----------------------------------------------------------------------
   Return whether string is a valid integer number
------------------------------------------------------------------------- */

bool utils::is_integer(const std::string & str) {
  if (str.size() == 0) {
    return false;
  }

  for (auto c : str) {
    if (isdigit(c) || c == '-' || c == '+') continue;
    return false;
  }
  return true;
}

/* ----------------------------------------------------------------------
   Return whether string is a valid floating-point number
------------------------------------------------------------------------- */

bool utils::is_double(const std::string & str) {
  if (str.size() == 0) {
    return false;
  }

  for (auto c : str) {
    if (isdigit(c)) continue;
    if (c == '-' || c == '+' || c == '.') continue;
    if (c == 'e' || c == 'E') continue;
    return false;
  }
  return true;
}

/* ------------------------------------------------------------------ */

extern "C" {
+14 −0
Changes for src/utils.h: 14 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -142,6 +142,20 @@ namespace LAMMPS_NS {
     * \return number of words found
     */
    size_t count_words(const std::string & text, const std::string & seperators = " \t\r\n\f");

    /**
     * \brief Check if string can be converted to valid integer
     * \param text string that should be checked
     * \return true, if string contains valid integer, false otherwise
     */
    bool is_integer(const std::string & str);

    /**
     * \brief Check if string can be converted to valid floating-point number
     * \param text string that should be checked
     * \return true, if string contains valid floating-point number, false otherwise
     */
    bool is_double(const std::string & str);
  }
}

+36 −0
Changes for unittest/utils/test_utils.cpp: 36 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -14,3 +14,39 @@ TEST(Utils, trim_comment) {
TEST(Utils, count_words) {
    ASSERT_EQ(utils::count_words("some text # comment"), 2);
}

TEST(Utils, valid_integer) {
    ASSERT_TRUE(utils::is_integer("10"));
}

TEST(Utils, valid_double) {
    ASSERT_TRUE(utils::is_double("10.0"));
}

TEST(Utils, empty_not_an_integer) {
    ASSERT_FALSE(utils::is_integer(""));
}

TEST(Utils, empty_not_a_double) {
    ASSERT_FALSE(utils::is_double(""));
}

TEST(Utils, double_not_an_integer) {
    ASSERT_FALSE(utils::is_integer("10.0"));
}

TEST(Utils, integer_is_double) {
    ASSERT_TRUE(utils::is_double("10"));
}

TEST(Utils, is_double_with_exponential) {
    ASSERT_TRUE(utils::is_double("10e22"));
}

TEST(Utils, is_double_with_neg_exponential) {
    ASSERT_TRUE(utils::is_double("10e-22"));
}

TEST(Utils, signed_double_and_exponential) {
    ASSERT_TRUE(utils::is_double("-10E-22"));
}