Skip to content

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
...
  • 2 commits
  • 4 files changed
  • 0 commit comments
  • 1 contributor
Showing with 27 additions and 70 deletions.
  1. +21 −64 ext/dump.c
  2. +2 −2 ext/dump.h
  3. +1 −1 ext/parse.c
  4. +3 −3 spec/tnetstring_spec.rb
View
85 ext/dump.c
@@ -32,7 +32,7 @@ VALUE ltns_dump(VALUE module __attribute__ ((unused)), VALUE val)
ret = ltns_dump_string(val);
break;
case T_FLOAT:
- ret = ltns_dump_float(val);
+ ret = ltns_dump_by_to_s(val, LTNS_FLOAT);
break;
case T_HASH:
ret = ltns_dump_hash(val);
@@ -42,7 +42,7 @@ VALUE ltns_dump(VALUE module __attribute__ ((unused)), VALUE val)
break;
case T_BIGNUM:
case T_FIXNUM:
- ret = ltns_dump_num(val);
+ ret = ltns_dump_by_to_s(val, LTNS_INTEGER);
break;
case T_FALSE:
case T_TRUE:
@@ -70,6 +70,25 @@ VALUE ltns_dump(VALUE module __attribute__ ((unused)), VALUE val)
return ret;
}
+VALUE ltns_dump_by_to_s(VALUE val, LTNSType type)
+{
+ VALUE str = rb_funcall(val, rb_intern("to_s"), 0);
+
+ LTNSTerm *term = NULL;
+ LTNSError error = LTNSTermCreate(&term, RSTRING_PTR(str), RSTRING_LEN(str), type);
+ if (error)
+ return Qnil;
+ char* tnetstring = NULL;
+ size_t length;
+ error = LTNSTermGetTNetstring(term, &tnetstring, &length);
+ if (error)
+ return Qnil;
+ VALUE ret = rb_str_new(tnetstring, length);
+ LTNSTermDestroy(term);
+
+ return ret;
+}
+
VALUE ltns_dump_string(VALUE val)
{
LTNSTerm *term = NULL;
@@ -116,68 +135,6 @@ VALUE ltns_dump_nil(VALUE val __attribute__ ((unused)))
return rb_str_new( nil, strlen(nil));
}
-VALUE ltns_dump_float(VALUE val)
-{
- double db = RFLOAT_VALUE(val);
-
- size_t digits = count_digits( floor(fabs(db)) );
-
- // negative sign?
- if( db < 0)
- digits ++;
-
- // place for dot and fixed precision
- digits += 1 + FLOAT_DECIMAL_PRECISION;
-
- char *payload = (char*)malloc( digits + 1 );
- snprintf( payload, digits + 1, "%.3f", db );
-
- LTNSTerm *term = NULL;
- LTNSError error = LTNSTermCreate(&term, payload, digits, LTNS_FLOAT);
- if (error)
- return Qnil;
- char* tnetstring = NULL;
- size_t length;
- error = LTNSTermGetTNetstring(term, &tnetstring, &length);
- if (error)
- return Qnil;
- VALUE ret = rb_str_new(tnetstring, length);
- LTNSTermDestroy(term);
- free(payload);
-
- return ret;
-}
-
-VALUE ltns_dump_num(VALUE val)
-{
- long long num = 0ll;
- num = NUM2LL(val);
-
- size_t digits = count_digits( llabs( num) );
-
- // negative sign?
- if( num < 0)
- digits++;
-
- char *payload = (char*)malloc( digits + 1 );
- snprintf( payload, digits + 1, "%lld", num );
-
- LTNSTerm *term = NULL;
- LTNSError error = LTNSTermCreate(&term, payload, digits, LTNS_INTEGER);
- if (error)
- return Qnil;
- char* tnetstring = NULL;
- size_t length;
- error = LTNSTermGetTNetstring(term, &tnetstring, &length);
- if (error)
- return Qnil;
- VALUE ret = rb_str_new(tnetstring, length);
- LTNSTermDestroy(term);
- free(payload);
-
- return ret;
-}
-
VALUE ltns_dump_array(VALUE val)
{
size_t length = RARRAY_LEN(val);
View
4 ext/dump.h
@@ -5,10 +5,10 @@
VALUE ltns_dump(VALUE module, VALUE val);
-VALUE ltns_dump_num(VALUE val);
+VALUE ltns_dump_by_to_s(VALUE val, LTNSType type);
+
VALUE ltns_dump_bool(VALUE val);
VALUE ltns_dump_string(VALUE val);
-VALUE ltns_dump_float(VALUE val);
VALUE ltns_dump_array(VALUE val);
VALUE ltns_dump_hash(VALUE val);
VALUE ltns_dump_nil(VALUE val);
View
2 ext/parse.c
@@ -91,7 +91,7 @@ int ltns_parse_string(const char* payload, size_t length, VALUE* out)
int ltns_parse_num(const char* payload, size_t length, VALUE* out)
{
char *end;
- long parsed_val = strtol(payload, &end, 10);
+ long long parsed_val = strtoll(payload, &end, 10);
if (end != (payload + length) || *end != LTNS_INTEGER)
return FALSE;
*out = LL2NUM(parsed_val);
View
6 spec/tnetstring_spec.rb
@@ -97,15 +97,15 @@
context "floats" do
it "dumps a positive float" do
- LazyTNetstring.dump(12.3).should == "6:12.300^"
+ LazyTNetstring.dump(12.3).should == "4:12.3^"
end
it "dumps a negative float" do
- LazyTNetstring.dump(-2.3).should == "6:-2.300^"
+ LazyTNetstring.dump(-2.3).should == "4:-2.3^"
end
it "dumps a float with integral value" do
- LazyTNetstring.dump(-42.0).should == "7:-42.000^"
+ LazyTNetstring.dump(-42.0).should == "5:-42.0^"
end
end

No commit comments for this range

Something went wrong with that request. Please try again.