Class: String

Inherits:
Object
  • Object
show all
Defined in:
(unknown)

Instance Method Summary collapse

Instance Method Details

#bencodeObject

String#bencode and String#bencode!



41
42
43
44
45
46
# File 'ext/b_encode/b_encode.c', line 41

static VALUE
rb_str_bencode(VALUE str) {
    str = rb_str_dup(str);
    rb_str_bencode_bang(str);
    return str;
}

#bencode!Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'ext/b_encode/b_encode.c', line 17

static VALUE
rb_str_bencode_bang(VALUE str) {
    int strlen = RSTRING_LEN(str);
    // length prefix + ":" + str
    int dc     = digit_count(strlen);
    int newlen = dc + 1 + strlen;

    rb_str_resize(str, newlen);

    char *ptr = RSTRING_PTR(str);

    memmove(ptr + dc + 1, ptr, strlen);
    if (strlen == 0) {
        ptr[0] = '0';
    } else {
        int n = strlen;
        for (int i = 0; n > 0; n /= 10, i++)
            ptr[dc - i - 1] = (n % 10) + '0';
    }
    ptr[dc] = ':';

    return str;
}