uuid-ncname 0.2.3 → 0.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4e77f488ca6429c6b7799b8ae23f673c2d17ae2914abaebfb8411cd4408ce498
4
- data.tar.gz: a744fad35027fd23aa64795baceeae5b940ef711d475a2e726f5c423df05997c
3
+ metadata.gz: bdbffa544b839b717c4c5d25d2602930be0576d01dcab7d282f52c21213c7b54
4
+ data.tar.gz: '08964857cf2c8908ca3258805b61aaf6adb4933b3b93f095e55db3b093be6a6a'
5
5
  SHA512:
6
- metadata.gz: 37652d6a314d6629164f0d616bafa99185cb3d044e9908492d91d5d77a87c13308f5ce894b1a3c05bb382ae9a1cb825e90aaf00152690816a1c2e737b7d20066
7
- data.tar.gz: 5740a1c557cbee7efe37ec3af6f3145a83010222cb3f91159fa9500d161c45279e9208d4cb11141197995d7cb9410e2b28dc69e4bcd8d8e30e4c3f1636c2682e
6
+ metadata.gz: 800487ec0f1fac16e67cbcdc71291a383d6dddf24e8729cbb937d87864b6f06dc3d61acd6141f0d0b4c6a6f4b3f36aa490a4f76b08b4731d86f66d65a2aa29a1
7
+ data.tar.gz: bbd9cc674f91ff9bd2605209b4ffb05a89f392aeb5ca2a3bf171baba19a81ceb1e692ff972b1b80209873ae676b04c26ee9a580880881787a6abf02b9640e9c5
@@ -127,7 +127,7 @@ module UUID::NCName
127
127
  def self.warn_version version
128
128
  if version.nil?
129
129
  warn 'Set an explicit :version to remove this warning. See documentation.'
130
- version = 0
130
+ version = 1
131
131
  end
132
132
 
133
133
  raise 'Version must be 0 or 1' unless [0, 1].include? version
@@ -149,9 +149,9 @@ module UUID::NCName
149
149
  #
150
150
  # @param version [0, 1] An optional formatting version, where 0 is
151
151
  # the naïve original version and 1 moves the `variant` nybble out
152
- # to the end of the identifier. You will be warned if you do not
153
- # set this parameter explicitly. The default is currently 0, but
154
- # will change in the next version.
152
+ # to the end of the identifier. You will be warned for the time
153
+ # being if you do not set this parameter explicitly. The default
154
+ # version is 1.
155
155
  #
156
156
  # @param align [true, false] Optional directive to treat the
157
157
  # terminating character as aligned to the numerical base of the
@@ -202,7 +202,7 @@ module UUID::NCName
202
202
 
203
203
  # Converts an NCName-encoded UUID back to its canonical
204
204
  # representation. Will return nil if the input doesn't match the
205
- # radix (if supplied) or is otherwise malformed. doesn't match
205
+ # radix (if supplied) or is otherwise malformed.
206
206
  #
207
207
  # @param ncname [#to_s] an NCName-encoded UUID, either a
208
208
  # 22-character (Base64) variant, or a 26-character (Base32) variant.
@@ -212,17 +212,20 @@ module UUID::NCName
212
212
  # @param format [:str, :hex, :b64, :bin] An optional formatting
213
213
  # parameter; defaults to `:str`, the canonical string representation.
214
214
  #
215
- # @param version [0, 1] See `to_ncname`. Defaults (for now) to 0.
215
+ # @param version [0, 1] See #to_ncname. Defaults to 1.
216
216
  #
217
- # @param align [true, false, nil] See `to_ncname` for details.
217
+ # @param align [nil, true, false] See #to_ncname for details.
218
218
  # Setting this parameter to `nil`, the default, will cause the
219
219
  # decoder to detect the alignment state from the identifier.
220
220
  #
221
+ # @param validate [false, true] Check that the ninth octet is
222
+ # correctly masked _after_ decoding.
223
+ #
221
224
  # @return [String, nil] The corresponding UUID or nil if the input
222
225
  # is malformed.
223
226
 
224
227
  def self.from_ncname ncname,
225
- radix: nil, format: :str, version: nil, align: nil
228
+ radix: nil, format: :str, version: nil, align: nil, validate: false
226
229
  raise 'Format must be symbol-able' unless format.respond_to? :to_sym
227
230
  raise "Invalid format #{format}" unless FORMAT[format]
228
231
  raise 'Align must be true, false, or nil' unless
@@ -262,6 +265,9 @@ module UUID::NCName
262
265
 
263
266
  bin = TRANSFORM[version][1].call uuidver, content
264
267
 
268
+ # double-check the variant (high-order bits have to be 10)
269
+ return if validate and bin[8].ord >> 6 != 2
270
+
265
271
  FORMAT[format].call bin
266
272
  end
267
273
 
@@ -269,9 +275,9 @@ module UUID::NCName
269
275
  #
270
276
  # @param uuid [#to_s] The UUID
271
277
  #
272
- # @param version [0, 1] See `to_ncname`.
278
+ # @param version [0, 1] See #to_ncname.
273
279
  #
274
- # @param align [true, false] See `to_ncname`.
280
+ # @param align [true, false] See #to_ncname.
275
281
  #
276
282
  # @return [String] The Base64-encoded NCName
277
283
 
@@ -285,9 +291,9 @@ module UUID::NCName
285
291
  #
286
292
  # @param format [:str, :hex, :b64, :bin] The format
287
293
  #
288
- # @param version [0, 1] See `to_ncname`.
294
+ # @param version [0, 1] See #to_ncname.
289
295
  #
290
- # @param align [true, false] See `to_ncname`.
296
+ # @param align [true, false] See #to_ncname.
291
297
  #
292
298
  # @return [String, nil] The corresponding UUID or nil if the input
293
299
  # is malformed.
@@ -300,9 +306,9 @@ module UUID::NCName
300
306
  #
301
307
  # @param uuid [#to_s] The UUID
302
308
  #
303
- # @param version [0, 1] See `to_ncname`.
309
+ # @param version [0, 1] See #to_ncname.
304
310
  #
305
- # @param align [true, false] See `to_ncname`.
311
+ # @param align [true, false] See #to_ncname.
306
312
  #
307
313
  # @return [String] The Base32-encoded NCName
308
314
 
@@ -316,9 +322,9 @@ module UUID::NCName
316
322
  #
317
323
  # @param format [:str, :hex, :b64, :bin] The format
318
324
  #
319
- # @param version [0, 1] See `to_ncname`.
325
+ # @param version [0, 1] See #to_ncname.
320
326
  #
321
- # @param align [true, false] See `to_ncname`.
327
+ # @param align [true, false] See #to_ncname.
322
328
  #
323
329
  # @return [String, nil] The corresponding UUID or nil if the input
324
330
  # is malformed.
@@ -327,21 +333,34 @@ module UUID::NCName
327
333
  from_ncname ncname, radix: 32, format: format
328
334
  end
329
335
 
330
- # Test if the given token is a UUID NCName, with a hint to its version.
336
+ # Test if the given token is a UUID NCName, with a hint to its
337
+ # version. This method can positively identify a token as a UUID
338
+ # NCName, but there is a small subset of UUIDs which will produce
339
+ # tokens which are valid in both versions. The method returns
340
+ # `false` if the token is invalid, otherwise it returns `0` or `1`
341
+ # for the guessed version.
342
+ #
343
+ # @note Version 1 tokens always end with I, J, K, or L (with base32
344
+ # being case-insensitive), so tokens that end in something else will
345
+ # be version 0.
331
346
  #
332
347
  # @param token [#to_s] The token to test
333
348
  #
334
349
  # @return [false, 0, 1]
335
350
  def self.valid? token
336
351
  token = token.to_s
337
- if /^[A-P](?:[0-9A-Za-z_-]{21}|[2-7A-Za-z]{25})$/.match token
352
+ if /^[A-Pa-p](?:[0-9A-Za-z_-]{21}|[2-7A-Za-z]{25})$/.match token
338
353
  # false is definitely version zero but true is only maybe version 1
339
- version = /[IJKLijkl]$/.match(token) ? 1 : 0
354
+ version = /^(?:.{21}[I-L]|.{25}[I-Li-l])$/.match(token) ? 1 : 0
355
+
356
+ # try decoding with validation on
357
+ uu = from_ncname token, version: version, validate: true
340
358
 
341
- if version == 1
342
- uu = from_ncname token, version: 1
343
- # lol even this isn't a guarantee
344
- /[89ab]/.match(uu[19]) ? 1 : version
359
+ if version == 1 and !uu
360
+ # try version zero
361
+ uu = from_ncname token, version: 0, validate: true
362
+ # either zero or invalid
363
+ uu ? 0 : false
345
364
  else
346
365
  version
347
366
  end
@@ -3,5 +3,5 @@ unless Module.const_defined? 'UUID'
3
3
  end
4
4
 
5
5
  module UUID::NCName
6
- VERSION = "0.2.3"
6
+ VERSION = "0.2.4"
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: uuid-ncname
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dorian Taylor
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-11-15 00:00:00.000000000 Z
11
+ date: 2019-11-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: base32