2 // This file contains a slimmed down version of read_rational().
3 // It does not pull in all the rational number code.
9 #include "cln/integer_io.h"
16 #include "cln/input.h"
17 #include "cln/integer.h"
22 // Step forward over all digits, to the end of string or to the next non-digit.
23 static const char * skip_digits (const char * ptr, const char * string_limit, unsigned int base)
25 for ( ; ptr != string_limit; ptr++) {
27 if ((ch >= '0') && (ch <= '9'))
28 if (ch < '0' + (int)base)
35 if (((ch >= 'A') && (ch < 'A'-10+(int)base))
36 || ((ch >= 'a') && (ch < 'a'-10+(int)base))
46 #define at_end_of_parse(ptr) \
48 { *end_of_parse = (ptr); } \
50 { if ((ptr) != string_limit) { throw read_number_junk_exception((ptr),string,string_limit); } }
52 const cl_I read_integer (const cl_read_flags& flags, const char * string, const char * string_limit, const char * * end_of_parse)
54 ASSERT((flags.syntax & ~(syntax_integer|syntax_maybe_bad)) == 0);
55 // If no string_limit is given, it defaults to the end of the string.
57 string_limit = string + ::strlen(string);
58 if (flags.syntax & syntax_integer) {
59 // Check for integer syntax.
60 var unsigned int rational_base = flags.rational_base;
61 var const char * ptr = string;
62 if (flags.lsyntax & lsyntax_commonlisp) {
63 if (ptr == string_limit) goto not_integer_syntax;
65 // Check for #b, #o, #x, #nR syntax.
67 if (ptr == string_limit) goto not_integer_syntax;
70 rational_base = 2; break;
72 rational_base = 8; break;
74 rational_base = 16; break;
76 var const char * base_end_ptr =
77 skip_digits(ptr,string_limit,10);
78 if (base_end_ptr == ptr) goto not_integer_syntax;
79 if (base_end_ptr == string_limit) goto not_integer_syntax;
80 if (!((*base_end_ptr == 'r') || (*base_end_ptr == 'R')))
81 goto not_integer_syntax;
82 var cl_I base = read_integer(10,0,ptr,0,base_end_ptr-ptr);
83 if (!((base >= 2) && (base <= 36))) {
84 std::ostringstream buf;
85 fprint(buf, "Base must be an integer in the range from 2 to 36, not ");
87 throw runtime_exception(buf.str());
89 rational_base = FN_to_UV(base); ptr = base_end_ptr;
95 var const char * ptr_after_prefix = ptr;
96 var cl_signean sign = 0;
97 if (ptr == string_limit) goto not_integer_syntax;
99 case '-': sign = ~sign;
103 var const char * ptr_after_sign = ptr;
104 // Check for integer syntax: {'+'|'-'|} {digit}+ {'.'|}
105 // Allow final dot only in Common Lisp syntax if there was no #<base> prefix.
106 if ((flags.lsyntax & lsyntax_commonlisp) && (ptr_after_prefix == string)) {
107 ptr = skip_digits(ptr_after_sign,string_limit,10);
108 if (ptr != ptr_after_sign)
109 if (ptr != string_limit)
112 if ((ptr == string_limit) || !(((*ptr >= '0') && (*ptr <= '9')) || ((*ptr >= 'A') && (*ptr <= 'Z') && (*ptr != 'I')) || ((*ptr >= 'a') && (*ptr <= 'z') && (*ptr != 'i')) || (*ptr == '.') || (*ptr == '_') || (*ptr == '/'))) {
113 at_end_of_parse(ptr);
114 return read_integer(10,sign,ptr_after_sign,0,ptr-ptr_after_sign);
118 ptr = skip_digits(ptr_after_sign,string_limit,rational_base);
119 if ((ptr == string_limit) || !(((*ptr >= '0') && (*ptr <= '9')) || ((*ptr >= 'A') && (*ptr <= 'Z') && (*ptr != 'I')) || ((*ptr >= 'a') && (*ptr <= 'z') && (*ptr != 'i')) || (*ptr == '.') || (*ptr == '_') || (*ptr == '/'))) {
120 at_end_of_parse(ptr);
121 return read_integer(rational_base,sign,ptr_after_sign,0,ptr-ptr_after_sign);
125 if (flags.syntax & syntax_maybe_bad) {
126 ASSERT(end_of_parse);
127 *end_of_parse = string;
128 return 0; // dummy return
130 throw read_number_bad_syntax_exception(string,string_limit);