Class: PreprocessinatorCommentStripper

Inherits:
Object
  • Object
show all
Defined in:
lib/ceedling/preprocess/preprocessinator_comment_stripper.rb

Instance Method Summary collapse

Instance Method Details

#strip_file(filepath) ⇒ Object

Strip all C comments from a file. The file is unchanged if no comments are found. The routine returns true if changes are made, false otherwise.

Every single-line comment is replaced by a single space. Every multi-line comment is replaced by an equivalent number of newlines.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ceedling/preprocess/preprocessinator_comment_stripper.rb', line 21

def strip_file(filepath)
  stripped = nil

  begin
    # Open in binary mode to avoid locale-dependent encoding failures.
    # GCC preprocessor output may contain localized strings (e.g. <組み込み> under ja_JP locale).
    # CCommentScanner already operates byte-accurately internally.
    File.open(filepath, 'rb') do |buffer|
      stripped = strip(buffer)
    end
  rescue => e
    raise CeedlingException.new("Failed to read '#{filepath}' for comment stripping ⏩️ #{e}")
  end

  # No change
  return false if stripped.nil?

  begin
    # Write in binary mode to match binary read — preserves original line endings exactly
    File.write(filepath, stripped, mode: 'wb')
  rescue => e
    raise CeedlingException.new("Failed to rewrite '#{filepath}' after comment stripping ⏩️ #{e}")
  end

  return true
end

#strip_string(content) ⇒ Object

Strip all C comments from a string content and return the cleaned content as a String. The string is unchanged if no comments are found.

Every single-line comment is replaced by a single space. Every multi-line comment is replaced by an equivalent number of newlines.



53
54
55
56
57
58
59
60
# File 'lib/ceedling/preprocess/preprocessinator_comment_stripper.rb', line 53

def strip_string(content)
  buffer = StringIO.new(content)
  stripped = strip(buffer)

  return content if stripped.nil?

  return stripped
end