module Nanaimo::Unicode
def quotify_string(string)
def quotify_string(string) string.gsub(QUOTE_REGEXP, QUOTE_MAP) end
def unquotify_string(string)
Licensed under https://raw.githubusercontent.com/samdmarshall/pbPlist/blob/346c29f91f913d35d0e24f6722ec19edb24e5707/LICENSE
Taken from https://github.com/samdmarshall/pbPlist/blob/346c29f91f913d35d0e24f6722ec19edb24e5707/pbPlist/StrParse.py#L197
Credit to Samantha Marshall
def unquotify_string(string) formatted_string = ::String.new extracted_string = string string_length = string.size index = 0 while index < string_length if escape_index = extracted_string.index('\\', index) formatted_string << extracted_string[index...escape_index] unless index == escape_index index = escape_index + 1 next_char = extracted_string[index] if ESCAPE_PREFIXES.include?(next_char) index += 1 if unquoted = UNQUOTE_MAP[next_char] formatted_string << unquoted elsif next_char == 'U' length = 4 unicode_numbers = extracted_string[index, length] unless unicode_numbers =~ /\A\h{4}\z/ raise InvalidEscapeSequenceError, "Unicode '\\U' escape sequence terminated without 4 following hex characters" end index += length formatted_string << [unicode_numbers.to_i(16)].pack('U') elsif OCTAL_DIGITS.include?(next_char) # https://twitter.com/Catfish_Man/status/658014170055507968 octal_string = extracted_string[index - 1, 3] if octal_string =~ /\A[0-7]{3}\z/ index += 2 code_point = octal_string.to_i(8) unless code_point <= 0x80 || converted = NEXT_STEP_MAPPING[code_point] raise InvalidEscapeSequenceError, "Invalid octal escape sequence #{octal_string}" end formatted_string << [converted].pack('U') else formatted_string << next_char end else raise UnsupportedEscapeSequenceError, "Failed to handle #{next_char} which is in the list of possible escapes" end else index += 1 formatted_string << next_char end else formatted_string << extracted_string[index..-1] index = string_length end end formatted_string end
def xml_escape_string(string)
def xml_escape_string(string) string.to_s.gsub(XML_STRING_ESCAPE_REGEXP, XML_STRING_ESCAPES) end