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.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/ruby_xid.rb +5 -4
  3. data/lib/xid/base32.rb +24 -37
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 57563aac1f71f9bc79cbaf1431aee6a660499b4ccab42f87570701e585017822
4
- data.tar.gz: c04e68b3bb859fdff627fbda865ab3290ba03d197eadcd11724c0e807850f8cc
3
+ metadata.gz: a748d67186a97a60828edaab13ea379ec424111d97eb00e0caee49d6c1c95149
4
+ data.tar.gz: ad28de191e5527247e47c9a47fee102b0d23a7398962651fab7b21ea94e1da8a
5
5
  SHA512:
6
- metadata.gz: 33d37db8dd796f96005337236f2552d5a896c390e7a92378a4cc174832ddb7cf75542627ded93d8f2e51d81f5651fc982d02adcecdae7db2fc40d0995eb440e6
7
- data.tar.gz: f64ca53e6f4b0f0ca2a43827e6c8f2c5f1b6cd0dcba94b87d2c26f1c44e040f0c63e7d8ed90e9f1cf9b96571f6e313fd93bbf12ae49c1db693ad315f13fb5311
6
+ metadata.gz: cbaed30e42664c32c74822faaaa46c96f4a6ca1a99e846f7b0e3b943bb4c0e5ea5f9b7c39fa6868642f5539a45bb1dc3ccf6153c8e3ad26fc549e3968da72383
7
+ data.tar.gz: 896ed22fd8f8fa7621c906b2606d24c0ac6ff7ab6226546b2b3f5bd69635c65e41a616f513649a69224d9206cbbf1b7187d1439c81134944022154d9feef404f
@@ -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.generate_data.unpack('C12')
15
+ @value = id ? id : @@generator.generate.unpack('C12')
16
16
  end
17
17
 
18
18
  def next
19
19
  @string = nil
20
- @value = @@generator.generate_data.unpack('C12')
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.map(&:chr).join('')
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 generate_data
123
+ def generate
123
124
  # () -> str
124
125
  @mutex.synchronize do
125
126
  @rand_int += 1
@@ -20,43 +20,30 @@ class Xid::Base32
20
20
  decode(src, ENCODE_HEX)
21
21
  end
22
22
 
23
- def encode(src_str)
24
- dst = ''
25
- 3.times do |i|
26
- src_len = src_str.length
27
- next_byte = Array.new(8, 0)
28
-
29
- if src_len > 4
30
- next_byte[7] = src_str[4] & 0x1f
31
- next_byte[6] = src_str[4] >> 5
32
- end
33
- if src_len > 3
34
- next_byte[6] = next_byte[6] | (src_str[3] << 3) & 0x1f
35
- next_byte[5] = (src_str[3] >> 2) & 0x1f
36
- next_byte[4] = src_str[3] >> 7
37
- end
38
- if src_len > 2
39
- next_byte[4] = next_byte[4] | (src_str[2] << 1) & 0x1f
40
- next_byte[3] = (src_str[2] >> 4) & 0x1f
41
- end
42
- if src_len > 1
43
- next_byte[3] = next_byte[3] | (src_str[1] << 4) & 0x1f
44
- next_byte[2] = (src_str[1] >> 1) & 0x1f
45
- next_byte[1] = (src_str[1] >> 6) & 0x1f
46
- end
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.6
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-17 00:00:00.000000000 Z
11
+ date: 2019-12-18 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: a.valarpirai@gmail.com