From 2f4bae4b4ffe6e913925f3d8f2c857a01aeea3bd Mon Sep 17 00:00:00 2001 From: mudathirmahgoub Date: Thu, 28 Jan 2021 14:51:28 -0600 Subject: Remove regex header from cvc4cpp.cpp (#5826) This PR replaces the heavy hammer std::regex_match for checking numbers with string operations --- src/api/cvc4cpp.cpp | 42 +++++++++++++++++++++++++++++++++++++++--- src/api/cvc4cpp.h | 3 +++ 2 files changed, 42 insertions(+), 3 deletions(-) (limited to 'src/api') diff --git a/src/api/cvc4cpp.cpp b/src/api/cvc4cpp.cpp index abec4d8dd..541c5f5a5 100644 --- a/src/api/cvc4cpp.cpp +++ b/src/api/cvc4cpp.cpp @@ -34,7 +34,6 @@ #include "api/cvc4cpp.h" #include -#include #include #include "base/check.h" @@ -3924,11 +3923,48 @@ Term Solver::mkPi() const CVC4_API_SOLVER_TRY_CATCH_END; } +bool Solver::isValidInteger(const std::string& s) const +{ + if (s.length() == 0) + { + // string should not be empty + return false; + } + + size_t index = 0; + if (s[index] == '-') + { + if (s.length() == 1) + { + // negative integers should contain at least one digit + return false; + } + index = 1; + } + + if (s[index] == '0' && s.length() > (index + 1)) + { + // From SMT-Lib 2.6: A〈numeral〉is the digit 0 or a non-empty sequence of + // digits not starting with 0. So integers like 001, 000 are not allowed + return false; + } + + // all remaining characters should be decimal digits + for (; index < s.length(); ++index) + { + if (!std::isdigit(s[index])) + { + return false; + } + } + + return true; +} + Term Solver::mkInteger(const std::string& s) const { CVC4_API_SOLVER_TRY_CATCH_BEGIN; - CVC4_API_ARG_CHECK_EXPECTED(std::regex_match(s, std::regex("-?\\d+")), s) - << " an integer "; + CVC4_API_ARG_CHECK_EXPECTED(isValidInteger(s), s) << " an integer "; Term integer = mkRealFromStrHelper(s); CVC4_API_ARG_CHECK_EXPECTED(integer.getSort() == getIntegerSort(), s) << " an integer"; diff --git a/src/api/cvc4cpp.h b/src/api/cvc4cpp.h index 66ba4f23b..60127d18b 100644 --- a/src/api/cvc4cpp.h +++ b/src/api/cvc4cpp.h @@ -3595,6 +3595,9 @@ class CVC4_PUBLIC Solver bool isInv = false, Grammar* g = nullptr) const; + /** check whether string s is a valid decimal integer */ + bool isValidInteger(const std::string& s) const; + /* The expression manager of this solver. */ std::unique_ptr d_exprMgr; /* The SMT engine of this solver. */ -- cgit v1.2.3