2 // This file contains a slimmed down version of read_real().
3 // It does not pull in all the floating-point, complex and transcendental
10 #include "cln/rational_io.h"
16 #include "cln/input.h"
17 #include "cln/integer.h"
18 #include "cln/integer_io.h"
20 #include "cln/abort.h"
24 // Step forward over all digits, to the end of string or to the next non-digit.
25 static const char * skip_digits (const char * ptr, const char * string_limit, unsigned int base)
27 for ( ; ptr != string_limit; ptr++) {
29 if ((ch >= '0') && (ch <= '9'))
30 if (ch < '0' + (int)base)
37 if (((ch >= 'A') && (ch < 'A'-10+(int)base))
38 || ((ch >= 'a') && (ch < 'a'-10+(int)base))
48 #define at_end_of_parse(ptr) \
50 { *end_of_parse = (ptr); } \
52 { if ((ptr) != string_limit) { read_number_junk((ptr),string,string_limit); } }
54 const cl_RA read_rational (const cl_read_flags& flags, const char * string, const char * string_limit, const char * * end_of_parse)
56 ASSERT((flags.syntax & ~(syntax_rational|syntax_maybe_bad)) == 0);
57 // If no string_limit is given, it defaults to the end of the string.
59 string_limit = string + ::strlen(string);
60 if (flags.syntax & syntax_rational) {
61 // Check for rational number syntax.
62 var unsigned int rational_base = flags.rational_base;
63 var const char * ptr = string;
64 if (flags.lsyntax & lsyntax_commonlisp) {
65 if (ptr == string_limit) goto not_rational_syntax;
67 // Check for #b, #o, #x, #nR syntax.
69 if (ptr == string_limit) goto not_rational_syntax;
72 rational_base = 2; break;
74 rational_base = 8; break;
76 rational_base = 16; break;
78 var const char * base_end_ptr =
79 skip_digits(ptr,string_limit,10);
80 if (base_end_ptr == ptr) goto not_rational_syntax;
81 if (base_end_ptr == string_limit) goto not_rational_syntax;
82 if (!((*base_end_ptr == 'r') || (*base_end_ptr == 'R')))
83 goto not_rational_syntax;
84 var cl_I base = read_integer(10,0,ptr,0,base_end_ptr-ptr);
85 if (!((base >= 2) && (base <= 36))) {
86 fprint(stderr, "Base must be an integer in the range from 2 to 36, not ");
91 rational_base = FN_to_UL(base); ptr = base_end_ptr;
97 var const char * ptr_after_prefix = ptr;
98 var cl_signean sign = 0;
99 if (ptr == string_limit) goto not_rational_syntax;
101 case '-': sign = ~sign;
105 var const char * ptr_after_sign = ptr;
106 if (flags.syntax & syntax_integer) {
107 // Check for integer syntax: {'+'|'-'|} {digit}+ {'.'|}
108 // Allow final dot only in Common Lisp syntax if there was no #<base> prefix.
109 if ((flags.lsyntax & lsyntax_commonlisp) && (ptr_after_prefix == string)) {
110 ptr = skip_digits(ptr_after_sign,string_limit,10);
111 if (ptr != ptr_after_sign)
112 if (ptr != string_limit)
115 if ((ptr == string_limit) || !(((*ptr >= '0') && (*ptr <= '9')) || ((*ptr >= 'A') && (*ptr <= 'Z') && (*ptr != 'I')) || ((*ptr >= 'a') && (*ptr <= 'z') && (*ptr != 'i')) || (*ptr == '.') || (*ptr == '_') || (*ptr == '/'))) {
116 at_end_of_parse(ptr);
117 return read_integer(10,sign,ptr_after_sign,0,ptr-ptr_after_sign);
121 ptr = skip_digits(ptr_after_sign,string_limit,rational_base);
122 if ((ptr == string_limit) || !(((*ptr >= '0') && (*ptr <= '9')) || ((*ptr >= 'A') && (*ptr <= 'Z') && (*ptr != 'I')) || ((*ptr >= 'a') && (*ptr <= 'z') && (*ptr != 'i')) || (*ptr == '.') || (*ptr == '_') || (*ptr == '/'))) {
123 at_end_of_parse(ptr);
124 return read_integer(rational_base,sign,ptr_after_sign,0,ptr-ptr_after_sign);
127 if (flags.syntax & syntax_ratio) {
128 // Check for ratio syntax: {'+'|'-'|} {digit}+ '/' {digit}+
129 ptr = skip_digits(ptr_after_sign,string_limit,rational_base);
130 if (ptr != ptr_after_sign)
131 if (ptr != string_limit)
133 var const char * ptr_at_slash = ptr;
134 ptr = skip_digits(ptr_at_slash+1,string_limit,rational_base);
135 if (ptr != ptr_at_slash+1)
136 if ((ptr == string_limit) || !(((*ptr >= '0') && (*ptr <= '9')) || ((*ptr >= 'A') && (*ptr <= 'Z') && (*ptr != 'I')) || ((*ptr >= 'a') && (*ptr <= 'z') && (*ptr != 'i')) || (*ptr == '.') || (*ptr == '_') || (*ptr == '/'))) {
137 at_end_of_parse(ptr);
138 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 read_number_bad_syntax(string,string_limit);