diff options
author | Morgan Deters <mdeters@gmail.com> | 2012-08-14 20:08:29 +0000 |
---|---|---|
committer | Morgan Deters <mdeters@gmail.com> | 2012-08-14 20:08:29 +0000 |
commit | d8fd737fb9ca867f53a287de1c4a87f16a6834f7 (patch) | |
tree | 4f1e4195a97162e0888fcf9c18c1a4d13f422f2f /src | |
parent | 87dbe20de92232279cf1d48ebfe7113194985208 (diff) |
Fixes to integer wrapper classes:
* more uniform interface between the CLN and GMP wrappers
* support base inference (base == 0) on parsing strings with the CLN wrapper; this was a difference from the GMP wrapper (resolves bug #372)
Diffstat (limited to 'src')
-rw-r--r-- | src/util/integer_cln_imp.h | 64 | ||||
-rw-r--r-- | src/util/integer_gmp_imp.h | 4 |
2 files changed, 42 insertions, 26 deletions
diff --git a/src/util/integer_cln_imp.h b/src/util/integer_cln_imp.h index 71d6d5b6d..8f7daf4f5 100644 --- a/src/util/integer_cln_imp.h +++ b/src/util/integer_cln_imp.h @@ -58,6 +58,42 @@ private: //Integer(const mpz_class& val) : d_value(val) {} Integer(const cln::cl_I& val) : d_value(val) {} + void readInt(const cln::cl_read_flags& flags, const std::string& s, unsigned base) throw(std::invalid_argument) { + try { + d_value = read_integer(flags, s.c_str(), NULL, NULL); + } catch(...) { + std::stringstream ss; + ss << "Integer() failed to parse value \"" << s << "\" in base " << base; + throw std::invalid_argument(ss.str()); + } + } + + void parseInt(const std::string& s, unsigned base) throw(std::invalid_argument) { + cln::cl_read_flags flags; + flags.syntax = cln::syntax_integer; + flags.lsyntax = cln::lsyntax_standard; + flags.rational_base = base; + if(base == 0) { + // infer base in a manner consistent with GMP + if(s[0] == '0') { + flags.lsyntax = cln::lsyntax_commonlisp; + std::string st = s; + if(s[1] == 'X' || s[1] == 'x') { + st.replace(0, 2, "#x"); + } else if(s[1] == 'B' || s[1] == 'b') { + st.replace(0, 2, "#b"); + } else { + st.replace(0, 1, "#o"); + } + readInt(flags, st, base); + return; + } else { + flags.rational_base = 10; + } + } + readInt(flags, s, base); + } + public: /** Constructs a rational with the value 0. */ @@ -69,32 +105,12 @@ public: * For more information about what is a valid rational string, * see GMP's documentation for mpq_set_str(). */ - explicit Integer(const char * s, int base = 10) throw (std::invalid_argument){ - cln::cl_read_flags flags; - flags.syntax = cln::syntax_integer; - flags.lsyntax = cln::lsyntax_standard; - flags.rational_base = base; - try{ - d_value = read_integer(flags, s, NULL, NULL); - }catch(...){ - std::stringstream ss; - ss << "Integer() failed to parse value \"" <<s << "\" in base=" <<base; - throw std::invalid_argument(ss.str()); - } + explicit Integer(const char* sp, unsigned base = 10) throw (std::invalid_argument) { + parseInt(std::string(sp), base); } - Integer(const std::string& s, int base = 10) throw (std::invalid_argument) { - cln::cl_read_flags flags; - flags.syntax = cln::syntax_integer; - flags.lsyntax = cln::lsyntax_standard; - flags.rational_base = base; - try{ - d_value = read_integer(flags, s.c_str(), NULL, NULL); - }catch(...){ - std::stringstream ss; - ss << "Integer() failed to parse value \"" <<s << "\" in base=" <<base; - throw std::invalid_argument(ss.str()); - } + explicit Integer(const std::string& s, unsigned base = 10) throw (std::invalid_argument) { + parseInt(s, base); } Integer(const Integer& q) : d_value(q.d_value) {} diff --git a/src/util/integer_gmp_imp.h b/src/util/integer_gmp_imp.h index ea7967023..c553baff9 100644 --- a/src/util/integer_gmp_imp.h +++ b/src/util/integer_gmp_imp.h @@ -62,8 +62,8 @@ public: * For more information about what is a valid rational string, * see GMP's documentation for mpq_set_str(). */ - explicit Integer(const char * s, int base = 10): d_value(s,base) {} - Integer(const std::string& s, unsigned base = 10) : d_value(s, base) {} + explicit Integer(const char* s, unsigned base = 10): d_value(s, base) {} + explicit Integer(const std::string& s, unsigned base = 10) : d_value(s, base) {} Integer(const Integer& q) : d_value(q.d_value) {} |