Class: String

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

Overview

Extensions for Ruby’s String class.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#quote_style:dquote, :squote

Record quote style used when parsing



89
90
91
# File 'lib/sxp/extensions.rb', line 89

def quote_style
  @quote_style
end

Instance Method Details

#as_dquote?Boolean

Render string using single quotes



95
# File 'lib/sxp/extensions.rb', line 95

def as_dquote?; quote_style != :squote; end

#as_squote?Boolean

Render string using double quotes



92
# File 'lib/sxp/extensions.rb', line 92

def as_squote?; quote_style == :squote; end

#to_sxp(**options) ⇒ String

Returns the SXP representation of this object. Uses SPARQL-like escaping. Uses any recorded quote style from an originally parsed string.



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

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 as_dquote? ? '\"' : '"'
    when (0x27)       then as_squote? ? "\'" : "'"
    when (0x5C)       then '\\\\'
    when (0x7F)       then sprintf("\\u%04X", u.ord)
    else u.chr
    end
  end
  if as_dquote?
    '"' + buffer + '"'
  else
    "'" + buffer + "'"
  end
end