Class: Pairtree::Identifier
- Inherits:
-
Object
- Object
- Pairtree::Identifier
- Defined in:
- lib/pairtree/identifier.rb
Constant Summary collapse
- ENCODE_REGEX =
Regexp.compile("[\"*+,<=>?\\\\^|]|[^\x21-\x7e]", nil)
- DECODE_REGEX =
Regexp.compile("\\^(..)", nil)
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
40 41 42 |
# File 'lib/pairtree/identifier.rb', line 40 def self.char2hex c c.unpack1("H*").scan(/../).map { |x| "^#{x}" }.join("") end |
.decode(id) ⇒ Object
Decode special characters within an identifier
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/pairtree/identifier.rb', line 16 def self.decode id input = id.tr("=+,", "/:.").bytes.to_a intermediate = [] while input.first if input.first == 94 h = [] input.shift h << input.shift h << input.shift intermediate << h.pack("c*").hex else intermediate << input.shift end end result = intermediate.pack("c*") if result.respond_to? :force_encoding result.force_encoding("UTF-8") end result 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
47 48 49 |
# File 'lib/pairtree/identifier.rb', line 47 def self.hex2char h "" << h.delete("^").hex end |