Class: RSpec::CapturingFormatter::Sanitizer

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/capturing_formatter/sanitizer.rb

Overview

Incrementally decodes and sanitizes captured bytes into safe UTF-8 report text.

Constant Summary collapse

MAX_ESCAPE_BYTES =
128
UTF8_LEADERS =
{
  (0xC2..0xDF) => 2,
  (0xE0..0xEF) => 3,
  (0xF0..0xF4) => 4
}.freeze

Instance Method Summary collapse

Constructor Details

#initializeSanitizer

Returns a new instance of Sanitizer.



14
15
16
17
18
19
20
21
22
# File 'lib/rspec/capturing_formatter/sanitizer.rb', line 14

def initialize
  @source_encoding = nil
  @bom_encoding = nil
  @converter = nil
  @converter_encoding = nil
  @encoding_carry = +""
  @escape_carry = +""
  @pending_cr = false
end

Instance Method Details

#finishObject



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rspec/capturing_formatter/sanitizer.rb', line 44

def finish
  result = parse("", final: true)
  result << flush_encoding_carry
  result << finish_converter
  @escape_carry = +""
  @pending_cr = false
  @source_encoding = nil
  @bom_encoding = nil
  @converter = nil
  @converter_encoding = nil
  result
end

#process(value, encoding = nil) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rspec/capturing_formatter/sanitizer.rb', line 24

def process(value, encoding = nil)
  source = resolve_encoding(value, encoding)
  source_changed = @source_encoding && @source_encoding != source
  prefix = +""
  if source_changed
    # Parser carry precedes decoder carry because it came from earlier, complete source bytes.
    prefix << parse("", final: true) if !@escape_carry.empty?
    prefix << flush_encoding_carry
    prefix << finish_converter
  end
  @bom_encoding = nil if source_changed
  @source_encoding = source

  bytes = @encoding_carry + value.to_s.b
  @encoding_carry = +""
  converted, @encoding_carry = decode(bytes, source)

  parse(prefix + converted)
end