Class: Pairtree::Identifier

Inherits:
Object
  • Object
show all
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

Class Method Details

.char2hex(c) ⇒ Object

Convert a character to its pairtree hexidecimal representation

Parameters:

  • c (Char)

    The character to convert



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

Parameters:

  • id (String)

    The 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

Parameters:

  • id (String)

    The 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

Parameters:

  • h (String)

    The hexidecimal string to convert



30
31
32
# File 'lib/pairtree/identifier.rb', line 30

def self.hex2char h
   '' << h.delete('^').hex
end