Class: Pairtree::Identifier
- Inherits:
-
Object
- Object
- Pairtree::Identifier
- Defined in:
- lib/pairtree/identifier.rb
Constant Summary collapse
- ENCODE_REGEX =
Regexp.compile("[\"*+,<=>?\\\\^|]|[^\x21-\x7e]", nil, 'u')
- DECODE_REGEX =
Regexp.compile("\\^(..)", nil, 'u')
Class Method Summary collapse
-
.char2hex(c) ⇒ Object
Convert a character to its pairtree hexidecimal representation.
-
.decode(id) ⇒ Object
Decode special characters within an identifier.
-
.encode(id) ⇒ Object
Encode special characters within an identifier.
-
.hex2char(h) ⇒ Object
Convert a pairtree hexidecimal string to its character representation.
Class Method Details
.char2hex(c) ⇒ Object
Convert a character to its pairtree hexidecimal representation
23 24 25 |
# File 'lib/pairtree/identifier.rb', line 23 def self.char2hex c c.unpack('H*')[0].scan(/../).map { |x| "^#{x}"} end |
.decode(id) ⇒ Object
Decode special characters within an identifier
16 17 18 |
# File 'lib/pairtree/identifier.rb', line 16 def self.decode id id.tr('=+,', '/:.').gsub(DECODE_REGEX) { |h| hex2char(h) } end |
.encode(id) ⇒ Object
Encode special characters within an identifier
9 10 11 |
# File 'lib/pairtree/identifier.rb', line 9 def self.encode id id.gsub(ENCODE_REGEX) { |c| char2hex(c) }.tr('/:.', '=+,') end |
.hex2char(h) ⇒ Object
Convert a pairtree hexidecimal string to its character representation
30 31 32 |
# File 'lib/pairtree/identifier.rb', line 30 def self.hex2char h '' << h.delete('^').hex end |