diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/api/cvc4cpp.cpp | 100 | ||||
-rw-r--r-- | src/api/cvc4cpp.h | 53 | ||||
-rw-r--r-- | src/util/integer_gmp_imp.cpp | 2 | ||||
-rw-r--r-- | src/util/string.cpp | 10 | ||||
-rw-r--r-- | src/util/string.h | 7 |
5 files changed, 171 insertions, 1 deletions
diff --git a/src/api/cvc4cpp.cpp b/src/api/cvc4cpp.cpp index 80fb8a5fb..8044508c7 100644 --- a/src/api/cvc4cpp.cpp +++ b/src/api/cvc4cpp.cpp @@ -2073,6 +2073,106 @@ CVC4::Expr Term::getExpr(void) const // to the new API. !!! const CVC4::Node& Term::getNode(void) const { return *d_node; } +namespace detail { +const Rational& getRational(const CVC4::Node& node) +{ + return node.getConst<Rational>(); +} +Integer getInteger(const CVC4::Node& node) +{ + return node.getConst<Rational>().getNumerator(); +} +template <typename T> +bool checkIntegerBounds(const Integer& i) +{ + return i >= std::numeric_limits<T>::min() + && i <= std::numeric_limits<T>::max(); +} +bool checkReal32Bounds(const Rational& r) +{ + return checkIntegerBounds<std::int32_t>(r.getNumerator()) + && checkIntegerBounds<std::uint32_t>(r.getDenominator()); +} +bool checkReal64Bounds(const Rational& r) +{ + return checkIntegerBounds<std::int64_t>(r.getNumerator()) + && checkIntegerBounds<std::uint64_t>(r.getDenominator()); +} + +bool isInteger(const Node& node) +{ + return node.getKind() == CVC4::Kind::CONST_RATIONAL + && node.getConst<Rational>().isIntegral(); +} +bool isInt32(const Node& node) +{ + return isInteger(node) + && checkIntegerBounds<std::int32_t>(getInteger(node)); +} +bool isUInt32(const Node& node) +{ + return isInteger(node) + && checkIntegerBounds<std::uint32_t>(getInteger(node)); +} +bool isInt64(const Node& node) +{ + return isInteger(node) + && checkIntegerBounds<std::int64_t>(getInteger(node)); +} +bool isUInt64(const Node& node) +{ + return isInteger(node) + && checkIntegerBounds<std::uint64_t>(getInteger(node)); +} +} // namespace detail + +bool Term::isInt32() const { return detail::isInt32(*d_node); } +std::int32_t Term::getInt32() const +{ + CVC4_API_CHECK(detail::isInt32(*d_node)) + << "Term should be a Int32 when calling getInt32()"; + return detail::getInteger(*d_node).getSignedInt(); +} +bool Term::isUInt32() const { return detail::isUInt32(*d_node); } +std::uint32_t Term::getUInt32() const +{ + CVC4_API_CHECK(detail::isUInt32(*d_node)) + << "Term should be a UInt32 when calling getUInt32()"; + return detail::getInteger(*d_node).getUnsignedInt(); +} +bool Term::isInt64() const { return detail::isInt64(*d_node); } +std::int64_t Term::getInt64() const +{ + CVC4_API_CHECK(detail::isInt64(*d_node)) + << "Term should be a Int64 when calling getInt64()"; + return detail::getInteger(*d_node).getLong(); +} +bool Term::isUInt64() const { return detail::isUInt64(*d_node); } +std::uint64_t Term::getUInt64() const +{ + CVC4_API_CHECK(detail::isUInt64(*d_node)) + << "Term should be a UInt64 when calling getUInt64()"; + return detail::getInteger(*d_node).getUnsignedLong(); +} +bool Term::isInteger() const { return detail::isInteger(*d_node); } +std::string Term::getInteger() const +{ + CVC4_API_CHECK(detail::isInteger(*d_node)) + << "Term should be an Int when calling getIntString()"; + return detail::getInteger(*d_node).toString(); +} + +bool Term::isString() const +{ + return d_node->getKind() == CVC4::Kind::CONST_STRING; +} +std::wstring Term::getString() const +{ + CVC4_API_CHECK(d_node->getKind() == CVC4::Kind::CONST_STRING) + << "Term should be a String when calling getString()"; + return d_node->getConst<CVC4::String>().toWString(); +} + std::ostream& operator<<(std::ostream& out, const Term& t) { out << t.toString(); diff --git a/src/api/cvc4cpp.h b/src/api/cvc4cpp.h index eb55e4007..98752c697 100644 --- a/src/api/cvc4cpp.h +++ b/src/api/cvc4cpp.h @@ -1151,6 +1151,59 @@ class CVC4_PUBLIC Term // to the new API. !!! const CVC4::Node& getNode(void) const; + /** + * Returns true if the term is an integer that fits within std::int32_t. + */ + bool isInt32() const; + /** + * Returns the stored integer as a std::int32_t. Asserts isInt32(). + */ + std::int32_t getInt32() const; + /** + * Returns true if the term is an integer that fits within std::uint32_t. + */ + bool isUInt32() const; + /** + * Returns the stored integer as a std::uint32_t. Asserts isUInt32(). + */ + std::uint32_t getUInt32() const; + /** + * Returns true if the term is an integer that fits within std::int64_t. + */ + bool isInt64() const; + /** + * Returns the stored integer as a std::int64_t. Asserts isInt64(). + */ + std::int64_t getInt64() const; + /** + * Returns true if the term is an integer that fits within std::uint64_t. + */ + bool isUInt64() const; + /** + * Returns the stored integer as a std::uint64_t. Asserts isUInt64(). + */ + std::uint64_t getUInt64() const; + /** + * Returns true if the term is an integer. + */ + bool isInteger() const; + /** + * Returns the stored integer in (decimal) string representation. Asserts + * isInteger(). + */ + std::string getInteger() const; + + /** + * Returns true if the term is a string constant. + */ + bool isString() const; + /** + * Returns the stored string constant. This method is not to be confused with + * toString() which returns the term in some string representation, whatever + * data it may hold. Asserts isString(). + */ + std::wstring getString() const; + protected: /** * The associated solver object. diff --git a/src/util/integer_gmp_imp.cpp b/src/util/integer_gmp_imp.cpp index fec71b26b..9cc8fb773 100644 --- a/src/util/integer_gmp_imp.cpp +++ b/src/util/integer_gmp_imp.cpp @@ -403,7 +403,7 @@ unsigned int Integer::getUnsignedInt() const this, "Overflow detected in Integer::getUnsignedInt()"); CheckArgument( - fitsSignedInt(), this, "Overflow detected in Integer::getUnsignedInt()"); + fitsUnsignedInt(), this, "Overflow detected in Integer::getUnsignedInt()"); return (unsigned int)d_value.get_ui(); } diff --git a/src/util/string.cpp b/src/util/string.cpp index 0d40ebb05..e625c2199 100644 --- a/src/util/string.cpp +++ b/src/util/string.cpp @@ -291,6 +291,16 @@ std::string String::toString(bool useEscSequences) const { return str.str(); } +std::wstring String::toWString() const +{ + std::wstring res(size(), static_cast<wchar_t>(0)); + for (std::size_t i = 0; i < size(); ++i) + { + res[i] = static_cast<wchar_t>(d_str[i]); + } + return res; +} + bool String::isLeq(const String &y) const { for (unsigned i = 0; i < size(); ++i) diff --git a/src/util/string.h b/src/util/string.h index 7102ec1f2..9e503bb07 100644 --- a/src/util/string.h +++ b/src/util/string.h @@ -114,6 +114,13 @@ class CVC4_PUBLIC String { * CVC4::String( s ).toString() = s. */ std::string toString(bool useEscSequences = false) const; + /* toWString + * Converts this string to a std::wstring. + * + * Unlike toString(), this method uses no escape sequences as both this class + * and std::wstring use 32bit characters. + */ + std::wstring toWString() const; /** is this the empty string? */ bool empty() const { return d_str.empty(); } /** is less than or equal to string y */ |