Module: B3e

Defined in:
lib/b3e.rb,
lib/b3e/version.rb

Defined Under Namespace

Classes: Invalid

Constant Summary collapse

VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

.c_decode(rString) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'ext/b3e/b3e_ext.c', line 87

static VALUE rb_b3e_decode(VALUE self, VALUE rString) {
  char* string = StringValuePtr(rString);

  u_int64_t length = RSTRING_LEN(rString);
  u_int64_t size = length * 6 / 8;
  u_int64_t index;

  char decoded[size];
  u_int64_t cursor = size;
  unsigned char character;
  int x;
  int byte = 0;
  int position = 0;
  int cb;

  for (index = 0; index < length; index++) {
    character = string[index];
    x = INDEX[character];

    if (x == -1) {
      rb_raise(cInvalid, "encountered a character that is not in the base62 alphabet");
    }

    if (index == length - 1) {
      byte |= x << position;
      cb = position % 8;
      position += (cb == 0 ? 0 : 8 - cb);
    } else if ((x & COMPACT_MASK) == COMPACT_MASK) {
      byte |= x << position;
      position += 5;
    } else {
      byte |= x << position;
      position += 6;
    }

    if (position >= 8) {
      cursor--;
      decoded[cursor] = byte;
      position %= 8;
      byte >>= 8;
    }
  }

  if (position > 0) {
    cursor--;
    decoded[cursor] = byte;
  }

  if (cursor > 0) {
    char resized[size];

    for (index = 0; index < size; index++) {
      resized[index] = decoded[cursor + index];
    }

    return rb_str_new(resized, size - cursor);
  } else {
    return rb_str_new(decoded, size);
  }
}

.c_encode(rString) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'ext/b3e/b3e_ext.c', line 28

static VALUE rb_b3e_encode(VALUE self, VALUE rString) {
  char* string = StringValuePtr(rString);
  char encoded[RSTRING_LEN(rString) * 8 / 5 + 1];

  int64_t cursor1 = RSTRING_LEN(rString) * 8;
  int64_t cursor2;
  int64_t i;

  char j;
  char blen1;
  char blen2;
  char shift;
  unsigned char byte;
  int written = 0;

  while (1) {
    j = 0;
    i = 0;

    cursor2 = cursor1 - 6;
    if (cursor2 <= 0) {
      cursor2 = 0;
      blen1 = cursor1;
    } else {
      i = cursor2 / 8;
      j = cursor2 % 8;
      blen1 = (i + 1) * 8 - cursor2;
      if (blen1 > 6) {
        blen1 = 6;
      }
    }

    shift = 8 - j - blen1;
    byte = (unsigned char)string[i] >> shift & ((1 << blen1) - 1);
    if (blen1 < 6 && cursor2 > 0) {
      blen2 = 6 - blen1;
      byte = (byte << blen2) | ((unsigned char)string[i + 1] >> (8 - blen2));
    }

    if ((byte & COMPACT_MASK) == COMPACT_MASK) {
      if (cursor2 > 0 || byte > MASK_5BITS) {
        cursor2++;
      }

      byte &= MASK_5BITS;
    }

    cursor1 = cursor2;
    encoded[written] = ALPHABET[byte];
    written++;

    if (cursor2 <= 0) {
      break;
    }
  }

  return rb_str_new(encoded, written);
}

.decode(value) ⇒ Object

public

Decode a Base62 string back to its original value.



21
22
23
24
25
26
27
# File 'lib/b3e.rb', line 21

def self.decode(value)
  if value.size > 0
    c_decode(value)
  else
    ""
  end
end

.encode(value) ⇒ Object

public

Encode a string as Base62.



11
12
13
14
15
16
17
# File 'lib/b3e.rb', line 11

def self.encode(value)
  if value.size > 0
    c_encode(value)
  else
    ""
  end
end

.versionObject



6
7
8
# File 'lib/b3e/version.rb', line 6

def self.version
  VERSION
end