class Origami::LiteralString
def self.parse(stream, _parser = nil) #:nodoc:
def self.parse(stream, _parser = nil) #:nodoc: scanner = Parser.init_scanner(stream) offset = scanner.pos unless scanner.skip(@@regexp_open) raise InvalidLiteralStringObjectError, "No literal string start token found" end result = ::String.new depth = 0 while depth != 0 or scanner.peek(1) != TOKENS.last do raise InvalidLiteralStringObjectError, "Non-terminated string" if scanner.eos? c = scanner.get_byte case c when "\\" if scanner.match?(/\d{1,3}/) oct = scanner.peek(3).oct.chr scanner.pos += 3 result << oct elsif scanner.match?(/((\r?\n)|(\r\n?))/) scanner.skip(/((\r?\n)|(\r\n?))/) next else flag = scanner.get_byte case flag when "n" then result << "\n" when "r" then result << "\r" when "t" then result << "\t" when "b" then result << "\b" when "f" then result << "\f" else result << flag end end when "(" then depth = depth + 1 result << c when ")" then depth = depth - 1 result << c else result << c end end unless scanner.skip(@@regexp_close) raise InvalidLiteralStringObjectError, "Byte string shall be terminated with '#{TOKENS.last}'" end # Try to cast as a Date object if possible. if result[0, 2] == 'D:' begin date = Date.parse(result) date.file_offset = offset return date rescue InvalidDateError end end bytestr = self.new(result) bytestr.file_offset = offset bytestr end
def expand #:nodoc:
def expand #:nodoc: self.gsub(/[\n\r\t\b\f()\\]/, "\n" => "\\n", "\r" => "\\r", "\t" => "\\t", "\b" => "\\b", "\f" => "\\f", "\\" => "\\\\", "(" => "\\(", ")" => "\\)") end
def initialize(str = ::String.new)
_str_:: The string value.
Creates a new PDF String.
def initialize(str = ::String.new) unless str.is_a?(::String) raise TypeError, "Expected type String, received #{str.class}." end super(str) end
def to_hex
Converts self to HexaString
def to_hex HexaString.new(self.value) end
def to_s(eol: $/) #:nodoc:
def to_s(eol: $/) #:nodoc: super(TOKENS.first + expand + TOKENS.last, eol: eol) end
def value
Returns a standard String representation.
def value self.decrypt! if self.is_a?(Encryption::EncryptedString) and not @decrypted to_str end