class Git::EscapedPath


Git::GitPath.new(‘302265’).unescape # => “µ”
@example
larger than 0x80 (e.g. octal 302265 for “micro” in UTF-8).
characters (e.g. t for TAB, n for LF, \ for backslash) or bytes with values
characters in the path with backslashes in the same way C escapes control
Git commands that output paths (e.g. ls-files, diff), will escape unusual
Represents an escaped Git path string

def escaped_path_to_bytes(path)

def escaped_path_to_bytes(path)
  index = 0
  [].tap do |bytes|
    while index < path.length
      byte, chars_used = next_byte(path, index)
      bytes << byte
      index += chars_used
    end
  end
end

def extract_escape(path, index)

def extract_escape(path, index)
  [UNESCAPES[path[index + 1]], 2]
end

def extract_octal(path, index)

def extract_octal(path, index)
  [path[index + 1..index + 3].to_i(8), 4]
end

def extract_single_char(path, index)

def extract_single_char(path, index)
  [path[index].ord, 1]
end

def initialize(path)

def initialize(path)
  @path = path
end

def next_byte(path, index)

def next_byte(path, index)
  if path[index] == '\\' && path[index + 1] >= '0' && path[index + 1] <= '7'
    extract_octal(path, index)
  elsif path[index] == '\\' && UNESCAPES.include?(path[index + 1])
    extract_escape(path, index)
  else
    extract_single_char(path, index)
  end
end

def unescape

Convert an escaped path to an unescaped path
def unescape
  bytes = escaped_path_to_bytes(path)
  str = bytes.pack('C*')
  str.force_encoding(Encoding::UTF_8)
end