Class: String

Inherits:
Object show all
Defined in:
lib/sxp/extensions.rb

Overview

Extensions for Ruby’s ‘String` class.

Instance Method Summary collapse

Instance Method Details

#to_sxp(**options) ⇒ String

Returns the SXP representation of this object. Uses SPARQL-like escaping.

Returns:



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/sxp/extensions.rb', line 61

def to_sxp(**options)
  buffer = ""
  each_char do |u|
    buffer << case u.ord
    when (0x00..0x07) then sprintf("\\u%04X", u.ord)
    when (0x08)       then '\b'
    when (0x09)       then '\t'
    when (0x0A)       then '\n'
    when (0x0C)       then '\f'
    when (0x0D)       then '\r'
    when (0x0E..0x1F) then sprintf("\\u%04X", u.ord)
    when (0x22)       then '\"'
    when (0x5C)       then '\\\\'
    when (0x7F)       then sprintf("\\u%04X", u.ord)
    else u.chr
    end
  end
  '"' + buffer + '"'
end