Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/pson.rb

Instance Method Summary collapse

Instance Method Details

#to_postscriptObject

Convert this string into a PostScript string literal expression. The hex literal notation will be used if this would produce a shorter expression.

Note that PostScript strings are strictly 8-bit. Thus, this conversion is done byte to byte, not character to character. If this would be a problem, re-encode the string first using the appropriate 8-bit encoding.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/pson.rb', line 22

def to_postscript
  result = "("
  self.each_byte do |b|
    case b
    when 0x28, 0x29, 0x5C then
      result << "\\" << b.chr
    when 0x20 .. 0x7E then
      result << b.chr
    else
      result << ("\\%03o" % b)
    end
  end
  result << ")"
  # Would it be shorter as a hex string literal?
  if result.length > self.bytesize * 2 + 2 then
    return "<" + self.unpack('H*').first + ">"
  else
    return result
  end
end