class String

Public Instance Methods

to_postscript() click to toggle source

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.

# 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