edn_turbo 0.6.0 → 0.6.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -40,6 +40,7 @@ namespace edn {
40
40
  VALUE EDN_MAKE_LIST_METHOD = Qnil;
41
41
  VALUE EDN_MAKE_SET_METHOD = Qnil;
42
42
  VALUE EDN_MAKE_BIG_DECIMAL_METHOD = Qnil;
43
+ VALUE EDN_MAKE_RATIONAL_METHOD = Qnil;
43
44
  VALUE EDN_TAGGED_ELEM_METHOD = Qnil;
44
45
  VALUE EDNT_EXTENDED_VALUE_METHOD = Qnil;
45
46
 
@@ -209,7 +210,7 @@ void Init_edn_turbo(void)
209
210
  rb_define_method(rb_cParser, "read",
210
211
  reinterpret_cast<VALUE(*)(ANYARGS)>(&edn::next), 0 );
211
212
 
212
- // bind ruby methods we'll call - these should be defined in edn_turbo.rb
213
+ // bind edn-ruby methods we'll call
213
214
  edn::EDN_MAKE_SYMBOL_METHOD = rb_intern("symbol");
214
215
  edn::EDN_MAKE_LIST_METHOD = rb_intern("list");
215
216
  edn::EDN_MAKE_SET_METHOD = rb_intern("set");
@@ -218,6 +219,7 @@ void Init_edn_turbo(void)
218
219
 
219
220
  // defined in EDNT - see edn_parser.rb
220
221
  edn::EDNT_EXTENDED_VALUE_METHOD = rb_intern("extend_for_meta");
222
+ edn::EDN_MAKE_RATIONAL_METHOD = rb_intern("rational"); // should be in edn-ruby
221
223
 
222
224
  // ruby methods
223
225
  edn::RUBY_STRING_TO_I_METHOD = rb_intern("to_i");
@@ -82,6 +82,7 @@ namespace edn
82
82
  const char* parse_keyword (const char *p, const char *pe, VALUE& v);
83
83
  const char* parse_decimal (const char *p, const char *pe, VALUE& v);
84
84
  const char* parse_integer (const char *p, const char *pe, VALUE& v);
85
+ const char* parse_ratio (const char *p, const char *pe, VALUE& v);
85
86
  const char* parse_operator(const char *p, const char *pe, VALUE& v);
86
87
  const char* parse_esc_char(const char *p, const char *pe, VALUE& v);
87
88
  const char* parse_symbol (const char *p, const char *pe, VALUE& v);
@@ -152,27 +152,35 @@ namespace edn
152
152
  }
153
153
 
154
154
  //
155
- // as above.. TODO: check exponential..
155
+ // as above, but for floats
156
156
  VALUE float_to_ruby(const char* str, std::size_t len)
157
157
  {
158
- // if big decimal is needed, call into ruby side to get
159
- // the correct value
160
- if (str[len-1] == 'M' || len >= LD_max_chars)
158
+ // if big decimal is needed, call into ruby side to get the
159
+ // correct value
160
+ if (str[len-1] == 'M')
161
161
  {
162
- std::string buf(str, len);
163
- VALUE vs = edn_prot_rb_new_str(buf.c_str());
162
+ std::string f_val(str, len-1); // strip the M
163
+ VALUE vs = edn_prot_rb_new_str(f_val.c_str());
164
164
 
165
- if (str[len-1] == 'M') {
166
- return call_module_fn(rb_mEDN, EDN_MAKE_BIG_DECIMAL_METHOD, vs);
167
- }
165
+ return call_module_fn(rb_mEDN, EDN_MAKE_BIG_DECIMAL_METHOD, vs);
166
+ }
168
167
 
169
- prot_args args(vs, RUBY_STRING_TO_F_METHOD);
168
+ // really long value string or exp
169
+ if (len >= LD_max_chars ||
170
+ std::string(str, len).find("e") != std::string::npos) {
171
+ prot_args args(edn_prot_rb_new_str(str), RUBY_STRING_TO_F_METHOD);
170
172
  return edn_prot_rb_funcall( edn_wrap_funcall2, reinterpret_cast<VALUE>(&args) );
171
173
  }
172
174
 
173
175
  return rb_float_new(buftotype<double>(str, len));
174
176
  }
175
177
 
178
+ //
179
+ // returns a ruby Rational
180
+ VALUE ratio_to_ruby(const char* str, std::size_t len)
181
+ {
182
+ return call_module_fn(rb_mEDN, EDN_MAKE_RATIONAL_METHOD, edn_prot_rb_new_str(str));
183
+ }
176
184
 
177
185
  //
178
186
  // read from a StringIO - handled from ruby side
@@ -31,6 +31,7 @@ namespace edn
31
31
  extern VALUE EDN_MAKE_LIST_METHOD;
32
32
  extern VALUE EDN_MAKE_SET_METHOD;
33
33
  extern VALUE EDN_MAKE_BIG_DECIMAL_METHOD;
34
+ extern VALUE EDN_MAKE_RATIONAL_METHOD;
34
35
  extern VALUE EDN_TAGGED_ELEM_METHOD;
35
36
  extern VALUE EDN_EOF_CONST;
36
37
 
@@ -45,6 +46,7 @@ namespace edn
45
46
  // defined in edn_parser_util.cc
46
47
  VALUE integer_to_ruby(const char* str, std::size_t len);
47
48
  VALUE float_to_ruby (const char* str, std::size_t len);
49
+ VALUE ratio_to_ruby (const char* str, std::size_t len);
48
50
 
49
51
  VALUE ruby_io_read(VALUE io);
50
52
 
@@ -30,4 +30,10 @@ require 'edn_turbo/edn_turbo'
30
30
  # Replace the parser in the EDN module with the C based one.
31
31
  module EDN
32
32
  self.parser = EDNT::Parser
33
+
34
+ # makes a rational type for converting a clojure ratio
35
+ # - this should be in edn-ruby
36
+ def self.rational(value)
37
+ Rational(value)
38
+ end
33
39
  end
@@ -21,6 +21,6 @@
21
21
  # THE SOFTWARE.
22
22
 
23
23
  module EDNT
24
- VERSION = '0.6.0'.freeze
25
- RELEASE_DATE = '2019-05-13'.freeze
24
+ VERSION = '0.6.1'.freeze
25
+ RELEASE_DATE = '2019-05-20'.freeze
26
26
  end
@@ -126,7 +126,7 @@ module EDNT
126
126
  end
127
127
  end
128
128
 
129
- context 'numbers' do
129
+ context 'numeric' do
130
130
  it 'zero' do
131
131
  expect(subject.parse(' 0 ')).to eq(0)
132
132
  end
@@ -176,17 +176,43 @@ module EDNT
176
176
  expect(val).to eq(432)
177
177
  expect(val.class).to eq(Integer)
178
178
  end
179
- it 'exact precision decimal' do
179
+ it 'exact precision integer' do
180
180
  val = subject.parse(' 12M ')
181
181
  expect(val).to eq(12)
182
182
  expect(val.class).to eq(Integer)
183
183
  end
184
- # TODO: fix?
185
- # it 'exact precision scientific notation' do
186
- # val = subject.parse('45.4e+43M')
187
- # expect(val).to eq(4.54e+44)
188
- # expect(val.class).to eq(Float)
189
- # end
184
+ it 'exact precision decimal' do
185
+ val = subject.parse('3.0M')
186
+ expect(val).to eq(3.0)
187
+ expect(val.class).to eq(BigDecimal)
188
+ end
189
+ it 'exact precision positive decimal with exp' do
190
+ val = subject.parse('+3.0e44M')
191
+ expect(val).to eq(3e44)
192
+ expect(val.class).to eq(BigDecimal)
193
+ end
194
+ it 'exact precision negative decimal with exp' do
195
+ val = subject.parse('-3.0e44M')
196
+ expect(val).to eq(-3e44)
197
+ expect(val.class).to eq(BigDecimal)
198
+ end
199
+ it 'exact precision decimal with negative exp' do
200
+ val = subject.parse('3.0e-44M')
201
+ expect(val).to eq(3e-44)
202
+ expect(val.class).to eq(BigDecimal)
203
+ end
204
+ it 'ratio' do
205
+ val = subject.parse(' 2/3 ')
206
+ expect(val).to eq(Rational(2, 3))
207
+ end
208
+ it 'positive ratio' do
209
+ val = subject.parse(' +2/3 ')
210
+ expect(val).to eq(Rational(2, 3))
211
+ end
212
+ it 'negative ratio' do
213
+ val = subject.parse(' -2/3 ')
214
+ expect(val).to eq(Rational(-2, 3))
215
+ end
190
216
  end
191
217
 
192
218
  context 'operators' do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: edn_turbo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.6.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ed Porras
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-05-13 00:00:00.000000000 Z
11
+ date: 2019-05-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: edn