lib/uuid/ncname.rb in uuid-ncname-0.3.1 vs lib/uuid/ncname.rb in uuid-ncname-0.4.0
- old
+ new
@@ -59,12 +59,15 @@
out.pack 'C*'
},
58 => -> (str, _) {
variant = decode_version(str[-1]) << 4
+ # warn str
str = str.chop.tr ?_, ''
- ::Base58.base58_to_binary(str, :bitcoin) + variant.chr
+ # warn str
+ # warn ::Base58.base58_to_binary(str, :bitcoin).length
+ ::Base58.base58_to_binary(str, :bitcoin) + variant.chr.b
},
64 => -> (str, align = true) {
str = str[0, 21] + 'A=='
out = ::Base64.urlsafe_decode64(str).unpack 'C*'
out[-1] <<= 2 if align
@@ -75,10 +78,11 @@
UUF = (['%02x' * 4] + ['%02x' * 2] * 3 + ['%02x' * 6]).join '-'
FORMAT = {
str: -> bin { UUF % bin.unpack('C*') },
+ urn: -> bin { "urn:uuid:#{UUF % bin.unpack('C*')}" },
hex: -> bin { bin.unpack 'H*' },
b64: -> bin { ::Base64.strict_encode64 bin },
bin: -> bin { bin },
}
@@ -123,11 +127,14 @@
return version, list.pack('N4')
},
-> (version, data) {
version &= 0xf
+ # warn data.length
+
list = data.unpack 'N4'
+ # warn list.inspect
variant = (list[3] & 0xf0) << 24
list[3] >>= 8
list[3] |= ((list[2] & 0xff) << 24)
list[2] >>= 8
list[2] |= ((list[1] & 0xf) << 24) | variant
@@ -146,10 +153,20 @@
def self.decode_version version
(version.upcase.ord - 65) % 16
end
+ def self.assert_version version
+ version = 1 unless version
+ raise ArgumentError, "version #{version.inspect} is not an integer" unless
+ version.respond_to? :to_i
+ version = version.to_i
+ raise ArgumentError, "there is no version #{version}" unless
+ TRANSFORM[version]
+ version
+ end
+
def self.warn_version version
if version.nil?
warn 'Set an explicit :version to remove this warning. See documentation.'
version = 1
end
@@ -192,13 +209,11 @@
#
# @param radix [32, 64] either the number 32 or the number 64.
#
# @param version [0, 1] An optional formatting version, where 0 is
# the naïve original version and 1 moves the `variant` nybble out
- # to the end of the identifier. You will be warned for the time
- # being if you do not set this parameter explicitly. The default
- # version is 1.
+ # to the end of the identifier. The default version is 1.
#
# @param align [true, false] Optional directive to treat the
# terminating character as aligned to the numerical base of the
# representation. Since the version nybble is removed from the
# string and the first 120 bits divide evenly into both Base32 and
@@ -218,11 +233,12 @@
raise 'UUID must be something stringable' if uuid.nil? or
not uuid.respond_to? :to_s
align = !!align # coerce to a boolean
# XXX remove this when appropriate
- version = warn_version(version)
+ # version = warn_version(version)
+ version = assert_version version
uuid = uuid.to_s
bin = nil
if uuid.length == 16
@@ -252,13 +268,14 @@
# radix (if supplied) or is otherwise malformed.
#
# @param ncname [#to_s] an NCName-encoded UUID, either a
# 22-character (Base64) variant, or a 26-character (Base32) variant.
#
- # @param radix [nil, 32, 64] Optional radix; will use heuristic if omitted.
+ # @param radix [nil, 32, 58, 64] Optional radix; will use a heuristic
+ # if omitted.
#
- # @param format [:str, :hex, :b64, :bin] An optional formatting
+ # @param format [:str, :urn, :hex, :b64, :bin] An optional formatting
# parameter; defaults to `:str`, the canonical string representation.
#
# @param version [0, 1] See ::to_ncname. Defaults to 1.
#
# @param align [nil, true, false] See ::to_ncname for details.
@@ -277,11 +294,12 @@
raise "Invalid format #{format}" unless FORMAT[format]
raise 'Align must be true, false, or nil' unless
[true, false, nil].include? align
# XXX remove this when appropriate
- version = warn_version version
+ # version = warn_version version
+ version = assert_version version
return unless ncname and ncname.respond_to? :to_s
ncname = ncname.to_s.strip.gsub(/\s+/, '')
match = MATCH.match(ncname) or return
@@ -340,11 +358,12 @@
#
# @return [String, nil] The corresponding UUID or nil if the input
# is malformed.
#
def self.from_ncname_64 ncname, format: :str, version: nil, align: nil
- from_ncname ncname, radix: 64, format: format
+ from_ncname ncname,
+ radix: 64, format: format, version: version, align: align
end
# Shorthand for conversion to the Base58 version
#
# @param uuid [#to_s] The UUID
@@ -371,11 +390,12 @@
#
# @return [String, nil] The corresponding UUID or nil if the input
# is malformed.
#
def self.from_ncname_58 ncname, format: :str, version: nil, align: nil
- from_ncname ncname, radix: 58, format: format
+ from_ncname ncname,
+ radix: 58, format: format, version: version, align: align
end
# Shorthand for conversion to the Base32 version
#
# @param uuid [#to_s] The UUID
@@ -402,10 +422,11 @@
#
# @return [String, nil] The corresponding UUID or nil if the input
# is malformed.
#
def self.from_ncname_32 ncname, format: :str, version: nil, align: nil
- from_ncname ncname, radix: 32, format: format
+ from_ncname ncname,
+ radix: 32, format: format, version: version, align: align
end
# Test if the given token is a UUID NCName, with a hint to its
# version. This method can positively identify a token as a UUID
# NCName, but there is a small subset of UUIDs which will produce