class Git::EscapedPath
Represents an escaped Git
path string
Git
commands that output paths (e.g. ls-files, diff), will escape unusual characters in the path with backslashes in the same way C escapes control characters (e.g. t for TAB, n for LF, \ for backslash) or bytes with values larger than 0x80 (e.g. octal 302265 for “micro” in UTF-8).
@example
Git::GitPath.new('\302\265').unescape # => "µ"
Constants
- UNESCAPES
Attributes
Public Class Methods
Public Instance Methods
Source
# File lib/git/escaped_path.rb, line 36 def unescape bytes = escaped_path_to_bytes(path) str = bytes.pack('C*') str.force_encoding(Encoding::UTF_8) end
Convert an escaped path to an unescaped path
Private Instance Methods
Source
# File lib/git/escaped_path.rb, line 66 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
Source
# File lib/git/escaped_path.rb, line 48 def extract_escape(path, index) [UNESCAPES[path[index + 1]], 2] end
Source
# File lib/git/escaped_path.rb, line 44 def extract_octal(path, index) [path[(index + 1)..(index + 3)].to_i(8), 4] end
Source
# File lib/git/escaped_path.rb, line 52 def extract_single_char(path, index) [path[index].ord, 1] end
Source
# File lib/git/escaped_path.rb, line 56 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