ruby_xid 1.0.6 → 1.0.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/ruby_xid.rb +5 -4
- data/lib/xid/base32.rb +24 -37
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a748d67186a97a60828edaab13ea379ec424111d97eb00e0caee49d6c1c95149
|
4
|
+
data.tar.gz: ad28de191e5527247e47c9a47fee102b0d23a7398962651fab7b21ea94e1da8a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cbaed30e42664c32c74822faaaa46c96f4a6ca1a99e846f7b0e3b943bb4c0e5ea5f9b7c39fa6868642f5539a45bb1dc3ccf6153c8e3ad26fc549e3968da72383
|
7
|
+
data.tar.gz: 896ed22fd8f8fa7621c906b2606d24c0ac6ff7ab6226546b2b3f5bd69635c65e41a616f513649a69224d9206cbbf1b7187d1439c81134944022154d9feef404f
|
data/lib/ruby_xid.rb
CHANGED
@@ -12,12 +12,12 @@ class Xid
|
|
12
12
|
|
13
13
|
def initialize(id = nil)
|
14
14
|
@@generator ||= Generator.new(init_rand_int, real_machine_id)
|
15
|
-
@value = id ? id : @@generator.
|
15
|
+
@value = id ? id : @@generator.generate.unpack('C12')
|
16
16
|
end
|
17
17
|
|
18
18
|
def next
|
19
19
|
@string = nil
|
20
|
-
@value = @@generator.
|
20
|
+
@value = @@generator.generate.unpack('C12')
|
21
21
|
string
|
22
22
|
end
|
23
23
|
|
@@ -59,7 +59,7 @@ class Xid
|
|
59
59
|
|
60
60
|
def bytes
|
61
61
|
# type: () -> str
|
62
|
-
@value.
|
62
|
+
@value.pack('c12')
|
63
63
|
end
|
64
64
|
|
65
65
|
def ==(other_xid)
|
@@ -78,6 +78,7 @@ class Xid
|
|
78
78
|
end
|
79
79
|
|
80
80
|
def self.from_string(str)
|
81
|
+
raise 'Invalid Xid' if str.nil? || !str.match(/^[a-v0-9]{20}$/i)
|
81
82
|
val = Base32.b32decode(str)
|
82
83
|
value_check = val.select { |x| x >= 0 && x <= 255 }
|
83
84
|
|
@@ -119,7 +120,7 @@ class Xid
|
|
119
120
|
@machine_id = machine_id
|
120
121
|
end
|
121
122
|
|
122
|
-
def
|
123
|
+
def generate
|
123
124
|
# () -> str
|
124
125
|
@mutex.synchronize do
|
125
126
|
@rand_int += 1
|
data/lib/xid/base32.rb
CHANGED
@@ -20,43 +20,30 @@ class Xid::Base32
|
|
20
20
|
decode(src, ENCODE_HEX)
|
21
21
|
end
|
22
22
|
|
23
|
-
def encode(
|
24
|
-
dst =
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
if src_len > 0
|
48
|
-
next_byte[1] = next_byte[1] | (src_str[0] << 2) & 0x1f
|
49
|
-
next_byte[0] = src_str[0] >> 3
|
50
|
-
end
|
51
|
-
|
52
|
-
src_str = src_str[5..src_str.length]
|
53
|
-
next_byte = next_byte[0..3] if i == 2
|
54
|
-
next_byte.each do |nb|
|
55
|
-
dst += ENCODE_HEX[nb]
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
dst
|
23
|
+
def encode(id)
|
24
|
+
dst = []
|
25
|
+
dst[19] = ENCODE_HEX[(id[11] << 4) & 0x1f]
|
26
|
+
dst[18] = ENCODE_HEX[(id[11] >> 1) & 0x1f]
|
27
|
+
dst[17] = ENCODE_HEX[(id[11] >> 6) & 0x1f | (id[10] << 2) & 0x1f]
|
28
|
+
dst[16] = ENCODE_HEX[id[10] >> 3]
|
29
|
+
dst[15] = ENCODE_HEX[id[9] & 0x1f]
|
30
|
+
dst[14] = ENCODE_HEX[(id[9] >> 5) | (id[8] << 3) & 0x1f]
|
31
|
+
dst[13] = ENCODE_HEX[(id[8] >> 2) & 0x1f]
|
32
|
+
dst[12] = ENCODE_HEX[id[8] >> 7 | (id[7] << 1) & 0x1f]
|
33
|
+
dst[11] = ENCODE_HEX[(id[7] >> 4) & 0x1f | (id[6] << 4) & 0x1f]
|
34
|
+
dst[10] = ENCODE_HEX[(id[6] >> 1) & 0x1f]
|
35
|
+
dst[9] = ENCODE_HEX[(id[6] >> 6) & 0x1f | (id[5] << 2) & 0x1f]
|
36
|
+
dst[8] = ENCODE_HEX[id[5] >> 3]
|
37
|
+
dst[7] = ENCODE_HEX[id[4] & 0x1f]
|
38
|
+
dst[6] = ENCODE_HEX[id[4] >> 5 | (id[3] << 3) & 0x1f]
|
39
|
+
dst[5] = ENCODE_HEX[(id[3] >> 2) & 0x1f]
|
40
|
+
dst[4] = ENCODE_HEX[id[3] >> 7 | (id[2] << 1) & 0x1f]
|
41
|
+
dst[3] = ENCODE_HEX[(id[2] >> 4) & 0x1f | (id[1] << 4) & 0x1f]
|
42
|
+
dst[2] = ENCODE_HEX[(id[1] >> 1) & 0x1f]
|
43
|
+
dst[1] = ENCODE_HEX[(id[1] >> 6) & 0x1f | (id[0] << 2) & 0x1f]
|
44
|
+
dst[0] = ENCODE_HEX[id[0] >> 3]
|
45
|
+
|
46
|
+
dst.join('')
|
60
47
|
end
|
61
48
|
|
62
49
|
def decode(src, str_map)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby_xid
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Valarpiraichandran
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-12-
|
11
|
+
date: 2019-12-18 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description:
|
14
14
|
email: a.valarpirai@gmail.com
|