]> www.ginac.de Git - cln.git/blob - src/rational/input/cl_RA_read.cc
db0a5446425c1db6511282deec1105f23cd506e7
[cln.git] / src / rational / input / cl_RA_read.cc
1 // read_rational().
2 // This file contains a slimmed down version of read_real().
3 // It does not pull in all the floating-point, complex and transcendental
4 // function code.
5
6 // General includes.
7 #include "cl_sysdep.h"
8
9 // Specification.
10 #include "cln/rational_io.h"
11
12
13 // Implementation.
14
15 #include <cstring>
16 #include "cln/input.h"
17 #include "cln/integer.h"
18 #include "cln/integer_io.h"
19 #include "cl_I.h"
20 #include "cln/abort.h"
21
22 namespace cln {
23
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)
26 {
27         for ( ; ptr != string_limit; ptr++) {
28                 var char ch = *ptr;
29                 if ((ch >= '0') && (ch <= '9'))
30                         if (ch < '0' + (int)base)
31                                 continue;
32                         else
33                                 break;
34                 else {
35                         if (base <= 10)
36                                 break;
37                         if (((ch >= 'A') && (ch < 'A'-10+(int)base))
38                             || ((ch >= 'a') && (ch < 'a'-10+(int)base))
39                            )
40                                 continue;
41                         else
42                                 break;
43                 }
44         }
45         return ptr;
46 }
47
48 #define at_end_of_parse(ptr)  \
49   if (end_of_parse)                                                     \
50     { *end_of_parse = (ptr); }                                          \
51   else                                                                  \
52     { if ((ptr) != string_limit) { read_number_junk((ptr),string,string_limit); } }
53
54 const cl_RA read_rational (const cl_read_flags& flags, const char * string, const char * string_limit, const char * * end_of_parse)
55 {
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.
58         if (!string_limit)
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;
66                         if (*ptr == '#') {
67                                 // Check for #b, #o, #x, #nR syntax.
68                                 ptr++;
69                                 if (ptr == string_limit) goto not_rational_syntax;
70                                 switch (*ptr) {
71                                 case 'b': case 'B':
72                                         rational_base = 2; break;
73                                 case 'o': case 'O':
74                                         rational_base = 8; break;
75                                 case 'x': case 'X':
76                                         rational_base = 16; break;
77                                 default:
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(std::cerr, "Base must be an integer in the range from 2 to 36, not ");
87                                                 fprint(std::cerr, base);
88                                                 fprint(std::cerr, "\n");
89                                                 cl_abort();
90                                         }
91                                         rational_base = FN_to_UL(base); ptr = base_end_ptr;
92                                         break;
93                                 }
94                                 ptr++;
95                         }
96                 }
97                 var const char * ptr_after_prefix = ptr;
98                 var cl_signean sign = 0;
99                 if (ptr == string_limit) goto not_rational_syntax;
100                 switch (*ptr) {
101                         case '-': sign = ~sign;
102                         case '+': ptr++;
103                         default: break;
104                 }
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)
113                                     if (*ptr == '.') {
114                                         ptr++;
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);
118                                         }
119                                 }
120                         }
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);
125                         }
126                 }
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)
132                             if (*ptr == '/') {
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);
139                                 }
140                         }
141                 }
142         }
143 not_rational_syntax:
144 bad_syntax:
145         if (flags.syntax & syntax_maybe_bad) {
146                 ASSERT(end_of_parse);
147                 *end_of_parse = string;
148                 return 0; // dummy return
149         }
150         read_number_bad_syntax(string,string_limit);
151 }
152
153 }  // namespace cln