/cln-1.3.2/src/rational/input/cl_RA_read.cc
C++ | 153 lines | 129 code | 10 blank | 14 comment | 112 complexity | 12b8b7669d710c4cd8177de6311191d7 MD5 | raw file
Possible License(s): GPL-2.0
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 "base/cl_sysdep.h"
8
9// Specification.
10#include "cln/rational_io.h"
11
12
13// Implementation.
14
15#include <cstring>
16#include <sstream>
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"
22
23namespace cln {
24
25// Step forward over all digits, to the end of string or to the next non-digit.
26static const char * skip_digits (const char * ptr, const char * string_limit, unsigned int base)
27{
28 for ( ; ptr != string_limit; ptr++) {
29 var char ch = *ptr;
30 if ((ch >= '0') && (ch <= '9'))
31 if (ch < '0' + (int)base)
32 continue;
33 else
34 break;
35 else {
36 if (base <= 10)
37 break;
38 if (((ch >= 'A') && (ch < 'A'-10+(int)base))
39 || ((ch >= 'a') && (ch < 'a'-10+(int)base))
40 )
41 continue;
42 else
43 break;
44 }
45 }
46 return ptr;
47}
48
49#define at_end_of_parse(ptr) \
50 if (end_of_parse) \
51 { *end_of_parse = (ptr); } \
52 else \
53 { if ((ptr) != string_limit) { throw read_number_junk_exception((ptr),string,string_limit); } }
54
55const cl_RA read_rational (const cl_read_flags& flags, const char * string, const char * string_limit, const char * * end_of_parse)
56{
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.
59 if (!string_limit)
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;
67 if (*ptr == '#') {
68 // Check for #b, #o, #x, #nR syntax.
69 ptr++;
70 if (ptr == string_limit) goto not_rational_syntax;
71 switch (*ptr) {
72 case 'b': case 'B':
73 rational_base = 2; break;
74 case 'o': case 'O':
75 rational_base = 8; break;
76 case 'x': case 'X':
77 rational_base = 16; break;
78 default:
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 ");
89 fprint(buf, base);
90 throw runtime_exception(buf.str());
91 }
92 rational_base = FN_to_UV(base); ptr = base_end_ptr;
93 break;
94 }
95 ptr++;
96 }
97 }
98 var const char * ptr_after_prefix = ptr;
99 var cl_signean sign = 0;
100 if (ptr == string_limit) goto not_rational_syntax;
101 switch (*ptr) {
102 case '-': sign = ~sign;
103 case '+': ptr++;
104 default: break;
105 }
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)
114 if (*ptr == '.') {
115 ptr++;
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);
119 }
120 }
121 }
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);
126 }
127 }
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)
133 if (*ptr == '/') {
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);
140 }
141 }
142 }
143 }
144not_rational_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 throw read_number_bad_syntax_exception(string,string_limit);
151}
152
153} // namespace cln