Skip to content

Commit 31ab659

Browse files
committed
reduce diff
1 parent aa084e7 commit 31ab659

File tree

2 files changed

+7
-34
lines changed

2 files changed

+7
-34
lines changed

ext/bert/c/decode.c

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,9 @@ static VALUE rb_mBERT;
2727
static VALUE rb_cDecode;
2828
static VALUE rb_cTuple;
2929

30-
typedef struct bert_buf bert_buf;
31-
32-
typedef VALUE (*bert_ptr)(struct bert_buf *buf);
33-
3430
struct bert_buf {
3531
const uint8_t *data;
3632
const uint8_t *end;
37-
bert_ptr *callbacks;
3833
};
3934

4035
static VALUE bert_read_invalid(struct bert_buf *buf);
@@ -51,10 +46,10 @@ static VALUE bert_read_list(struct bert_buf *buf);
5146
static VALUE bert_read_bin(struct bert_buf *buf);
5247
static VALUE bert_read_enc_string(struct bert_buf *buf);
5348
static VALUE bert_read_unicode_string(struct bert_buf *buf);
54-
static VALUE bert_read_unicode_string(struct bert_buf *buf);
5549
static VALUE bert_read_sbignum(struct bert_buf *buf);
5650
static VALUE bert_read_lbignum(struct bert_buf *buf);
5751

52+
typedef VALUE (*bert_ptr)(struct bert_buf *buf);
5853
static bert_ptr bert_callbacks[] = {
5954
&bert_read_sint,
6055
&bert_read_int,
@@ -118,7 +113,7 @@ static VALUE bert_read(struct bert_buf *buf)
118113
if (!BERT_VALID_TYPE(type))
119114
rb_raise(rb_eRuntimeError, "Invalid tag '%d' for term", type);
120115

121-
return buf->callbacks[type - BERT_TYPE_OFFSET](buf);
116+
return bert_callbacks[type - BERT_TYPE_OFFSET](buf);
122117
}
123118

124119
static VALUE bert_read_dict(struct bert_buf *buf)
@@ -517,17 +512,11 @@ static VALUE rb_bert_decode(VALUE klass, VALUE rb_string)
517512
bert_buf_ensure(&buf, 1);
518513

519514
proto_version = bert_buf_read8(&buf);
520-
switch(proto_version) {
521-
case ERL_VERSION:
522-
buf.callbacks = bert_callbacks;
523-
break;
524-
case ERL_VERSION2:
525-
buf.callbacks = bert_callbacks;
526-
break;
527-
default:
528-
rb_raise(rb_eTypeError, "Invalid magic value for BERT string");
515+
if (proto_version == ERL_VERSION || proto_version == ERL_VERSION2) {
516+
return bert_read(&buf);
517+
} else {
518+
rb_raise(rb_eTypeError, "Invalid magic value for BERT string");
529519
}
530-
return bert_read(&buf);
531520
}
532521

533522
static VALUE rb_bert_impl(VALUE klass)

lib/bert/decode.rb

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,6 @@ class Decode
33
attr_accessor :in
44
include Types
55

6-
class V1 < Decode
7-
def read_bin
8-
fail("Invalid Type, not an erlang binary") unless read_1 == BIN
9-
length = read_4
10-
read_string(length)
11-
end
12-
13-
def read_erl_string
14-
fail("Invalid Type, not an erlang string") unless read_1 == STRING
15-
length = read_2
16-
read_string(length).unpack('C' * length)
17-
end
18-
end
19-
206
def self.impl
217
'Ruby'
228
end
@@ -26,9 +12,7 @@ def self.decode(string)
2612
io.set_encoding('binary') if io.respond_to?(:set_encoding)
2713
header = io.getbyte
2814
case header
29-
when MAGIC
30-
Decode::V1.new(io).read_any
31-
when VERSION_2
15+
when MAGIC, VERSION_2
3216
new(io).read_any
3317
else
3418
fail("Bad Magic")

0 commit comments

Comments
 (0)