Module: Mutineer::SourceBytes

Defined in:
lib/mutineer/source_bytes.rb

Overview

Bytes to write for a mutant file so they match its magic comment.

The in-process string is already in that encoding. The daemon receives the same text through JSON, which turns it into UTF-8. Writing those UTF-8 bytes under an encoding: EUC-JP comment would make Ruby misread the file. This encodes the text back to the comment's encoding. No Prism: the daemon loads on the app's Ruby (#346).

The comment forms are Ruby's, not Prism's. Ruby ignores # encoding=NAME with no space, and it ignores a coding word that is not in a comment. Prism 2 accepts the first of those.

Constant Summary collapse

HEADER_BYTES =

How much of the file can hold the comment. Real comments are short. The cap only bounds a pathological first line.

4096

Class Method Summary collapse

Class Method Details

.comment_body(line) ⇒ String? (private)

line from its #, or nil when the line is code. Leading spaces and tabs are allowed. A trailing comment on a code line is not.

Parameters:

  • line (String, nil)

Returns:

  • (String, nil) —

    binary comment text.



156
157
158
159
160
161
162
163
164
165
# File 'lib/mutineer/source_bytes.rb', line 156

def self.comment_body(line)
  return nil if line.nil?

  raw = line.to_s.b
  index = 0
  index += 1 while raw.getbyte(index) == 32 || raw.getbyte(index) == 9
  return nil unless raw.getbyte(index) == 35

  raw.byteslice(index..)
end

.encode(text, name) ⇒ String (private)

text in name's encoding, or text when that encoding is unknown or cannot hold a character.

Parameters:

  • text (String)
  • name (String, nil)

Returns:

  • (String)


94
95
96
97
98
99
100
101
102
103
# File 'lib/mutineer/source_bytes.rb', line 94

def self.encode(text, name)
  return text if name.nil? || name.empty?

  encoding = Encoding.find(name)
  return text if encoding == text.encoding

  text.encode(encoding)
rescue ArgumentError, Encoding::UndefinedConversionError
  text
end

.encoding_for(bytes) ⇒ Encoding

Encoding Ruby assigns to bytes from its magic comment, or UTF-8.

Parameters:

  • bytes (String) —

    file contents.

Returns:

  • (Encoding)


41
42
43
44
45
46
47
48
# File 'lib/mutineer/source_bytes.rb', line 41

def self.encoding_for(bytes)
  name = encoding_name(bytes)
  return Encoding::UTF_8 if name.nil?

  Encoding.find(name)
rescue ArgumentError
  Encoding::UTF_8
end

.encoding_name(code) ⇒ String? (private)

The encoding name in the first comment line, or the second when the first is a shebang. Nil when Ruby would ignore the line.

Parameters:

  • code (String)

Returns:

  • (String, nil)


55
56
57
58
59
60
61
62
# File 'lib/mutineer/source_bytes.rb', line 55

def self.encoding_name(code)
  first, second = header_lines(code)
  chosen = shebang?(first) ? second : first
  comment = comment_body(chosen)
  return nil if comment.nil?

  name_in(comment)
end

.for_file(code) ⇒ String

code as the bytes its magic comment names, or its own bytes when it names none or an unknown encoding. A snippet that is already in another encoding and has no comment gets one, so load uses that encoding. A redefine snippet is such a snippet (#346).

Parameters:

  • code (String) —

    mutant source, in its encoding or in UTF-8.

Returns:

  • (String) —

    binary string of the bytes to write.



27
28
29
30
31
32
33
34
35
# File 'lib/mutineer/source_bytes.rb', line 27

def self.for_file(code)
  text = code.to_s
  name = encoding_name(text)
  if name.nil? && foreign?(text)
    text = with_magic_comment(text)
    name = text.encoding.name
  end
  encode(text, name).b
end

.foreign?(text) ⇒ Boolean (private)

True when text holds characters UTF-8 would not already store.

Parameters:

  • text (String)

Returns:

  • (Boolean)


69
70
71
72
73
74
75
# File 'lib/mutineer/source_bytes.rb', line 69

def self.foreign?(text)
  return false if text.encoding == Encoding::UTF_8 || text.encoding == Encoding::US_ASCII

  !text.ascii_only?
rescue ArgumentError
  true
end

.header_lines(code) ⇒ Array(String, String) (private)

The first line and the second, without their endings. CRLF is one ending. Only the first HEADER_BYTES bytes are read.

Parameters:

  • code (String)

Returns:

  • (Array(String, String))


111
112
113
114
115
116
# File 'lib/mutineer/source_bytes.rb', line 111

def self.header_lines(code)
  raw = code.to_s.b.byteslice(0, HEADER_BYTES) || "".b
  first, rest = take_line(raw)
  second, = take_line(rest)
  [first, second]
end

.name_in(comment) ⇒ String? (private)

The encoding name in a comment line, or nil. A colon may have no space. An equals sign needs whitespace on both sides. A vim modeline counts only as set fileencoding=.

Parameters:

  • comment (String)

Returns:

  • (String, nil)


174
175
176
177
178
179
180
181
182
183
184
# File 'lib/mutineer/source_bytes.rb', line 174

def self.name_in(comment)
  text = comment.to_s.b
  vim = text.match(/vim:\s*set\b[^\n\r]*\bfileencoding\s*=\s*([^\s:]+)/i)
  return vim[1] if vim

  named = text.match(/\b(?:en)?coding\s*:\s*([^\s;#]+)/i) ||
          text.match(/\b(?:en)?coding\s+=\s*([^\s;#]+)/i)
  named&.[](1)
rescue ArgumentError
  nil
end

.shebang?(line) ⇒ Boolean (private)

True when line is a shebang. Ruby then reads the encoding from the next line.

Parameters:

  • line (String)

Returns:

  • (Boolean)


146
147
148
# File 'lib/mutineer/source_bytes.rb', line 146

def self.shebang?(line)
  line.to_s.b.start_with?("#!".b)
end

.take_line(raw) ⇒ Array(String, String) (private)

One line and what follows it.

Parameters:

  • raw (String) —

    binary text.

Returns:

  • (Array(String, String))


123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/mutineer/source_bytes.rb', line 123

def self.take_line(raw)
  return ["", ""] if raw.nil? || raw.empty?

  crlf = raw.index("\r\n".b)
  lf = raw.index("\n".b)
  cr = raw.index("\r".b)
  if crlf && (lf.nil? || crlf <= lf) && (cr.nil? || crlf <= cr)
    [raw.byteslice(0, crlf), raw.byteslice((crlf + 2)..)]
  elsif lf && (cr.nil? || lf <= cr)
    [raw.byteslice(0, lf), raw.byteslice((lf + 1)..)]
  elsif cr
    [raw.byteslice(0, cr), raw.byteslice((cr + 1)..)]
  else
    [raw, ""]
  end
end

.with_magic_comment(text) ⇒ String (private)

text with a magic comment naming its encoding.

Parameters:

  • text (String)

Returns:

  • (String)


82
83
84
85
# File 'lib/mutineer/source_bytes.rb', line 82

def self.with_magic_comment(text)
  comment = "# encoding: #{text.encoding.name}\n".dup.force_encoding(text.encoding)
  comment + text
end