2 // This file contains a slimmed down version of read_real().
3 // It does not pull in all the floating-point, complex and transcendental
7 #include "base/cl_sysdep.h"
10 #include "cln/rational_io.h"
17 #include "cln/input.h"
18 #include "cln/integer.h"
19 #include "cln/integer_io.h"
20 #include "integer/cl_I.h"
21 #include "cln/exception.h"
25 // Step forward over all digits, to the end of string or to the next non-digit.
26 static const char * skip_digits (const char * ptr, const char * string_limit, unsigned int base)
28 for ( ; ptr != string_limit; ptr++) {
30 if ((ch >= '0') && (ch <= '9'))
31 if (ch < '0' + (int)base)
38 if (((ch >= 'A') && (ch < 'A'-10+(int)base))
39 || ((ch >= 'a') && (ch < 'a'-10+(int)base))
49 #define at_end_of_parse(ptr) \
51 { *end_of_parse = (ptr); } \
53 { if ((ptr) != string_limit) { throw read_number_junk_exception((ptr),string,string_limit); } }
55 const cl_RA read_rational (const cl_read_flags& flags, const char * string, const char * string_limit, const char * * end_of_parse)
57 ASSERT((flags.syntax & ~(syntax_rational|syntax_maybe_bad)) == 0);
58 // If no string_limit is given, it defaults to the end of the string.
60 string_limit = string + ::strlen(string);
61 if (flags.syntax & syntax_rational) {
62 // Check for rational number syntax.
63 var unsigned int rational_base = flags.rational_base;
64 var const char * ptr = string;
65 if (flags.lsyntax & lsyntax_commonlisp) {
66 if (ptr == string_limit) goto not_rational_syntax;
68 // Check for #b, #o, #x, #nR syntax.
70 if (ptr == string_limit) goto not_rational_syntax;
73 rational_base = 2; break;
75 rational_base = 8; break;
77 rational_base = 16; break;
79 var const char * base_end_ptr =
80 skip_digits(ptr,string_limit,10);
81 if (base_end_ptr == ptr) goto not_rational_syntax;
82 if (base_end_ptr == string_limit) goto not_rational_syntax;
83 if (!((*base_end_ptr == 'r') || (*base_end_ptr == 'R')))
84 goto not_rational_syntax;
85 var cl_I base = read_integer(10,0,ptr,0,base_end_ptr-ptr);
86 if (!((base >= 2) && (base <= 36))) {
87 std::ostringstream buf;
88 fprint(buf, "Base must be an integer in the range from 2 to 36, not ");
90 throw runtime_exception(buf.str());
92 rational_base = FN_to_UV(base); ptr = base_end_ptr;
98 var const char * ptr_after_prefix = ptr;
99 var cl_signean sign = 0;
100 if (ptr == string_limit) goto not_rational_syntax;
102 case '-': sign = ~sign;
106 var const char * ptr_after_sign = ptr;
107 if (flags.syntax & syntax_integer) {
108 // Check for integer syntax: {'+'|'-'|} {digit}+ {'.'|}
109 // Allow final dot only in Common Lisp syntax if there was no #<base> prefix.
110 if ((flags.lsyntax & lsyntax_commonlisp) && (ptr_after_prefix == string)) {
111 ptr = skip_digits(ptr_after_sign,string_limit,10);
112 if (ptr != ptr_after_sign)
113 if (ptr != string_limit)
116 if ((ptr == string_limit) || !(((*ptr >= '0') && (*ptr <= '9')) || ((*ptr >= 'A') && (*ptr <= 'Z') && (*ptr != 'I')) || ((*ptr >= 'a') && (*ptr <= 'z') && (*ptr != 'i')) || (*ptr == '.') || (*ptr == '_') || (*ptr == '/'))) {
117 at_end_of_parse(ptr);
118 return read_integer(10,sign,ptr_after_sign,0,ptr-ptr_after_sign);
122 ptr = skip_digits(ptr_after_sign,string_limit,rational_base);
123 if ((ptr == string_limit) || !(((*ptr >= '0') && (*ptr <= '9')) || ((*ptr >= 'A') && (*ptr <= 'Z') && (*ptr != 'I')) || ((*ptr >= 'a') && (*ptr <= 'z') && (*ptr != 'i')) || (*ptr == '.') || (*ptr == '_') || (*ptr == '/'))) {
124 at_end_of_parse(ptr);
125 return read_integer(rational_base,sign,ptr_after_sign,0,ptr-ptr_after_sign);
128 if (flags.syntax & syntax_ratio) {
129 // Check for ratio syntax: {'+'|'-'|} {digit}+ '/' {digit}+
130 ptr = skip_digits(ptr_after_sign,string_limit,rational_base);
131 if (ptr != ptr_after_sign)
132 if (ptr != string_limit)
134 var const char * ptr_at_slash = ptr;
135 ptr = skip_digits(ptr_at_slash+1,string_limit,rational_base);
136 if (ptr != ptr_at_slash+1)
137 if ((ptr == string_limit) || !(((*ptr >= '0') && (*ptr <= '9')) || ((*ptr >= 'A') && (*ptr <= 'Z') && (*ptr != 'I')) || ((*ptr >= 'a') && (*ptr <= 'z') && (*ptr != 'i')) || (*ptr == '.') || (*ptr == '_') || (*ptr == '/'))) {
138 at_end_of_parse(ptr);
139 return read_rational(rational_base,sign,ptr_after_sign,0,ptr_at_slash-ptr_after_sign,ptr-ptr_after_sign);
145 if (flags.syntax & syntax_maybe_bad) {
146 ASSERT(end_of_parse);
147 *end_of_parse = string;
148 return 0; // dummy return
150 throw read_number_bad_syntax_exception(string,string_limit);