Module: CmpFS::Compare::Text::Internal_

Defined in:
lib/cmpfs/compare/text/internal_.rb

Class Method Summary collapse

Class Method Details

.compare_text_(lhs, rhs, options) ⇒ Object

Raises:

  • (ArgumentError)


102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/cmpfs/compare/text/internal_.rb', line 102

def self.compare_text_ lhs, rhs, options

  lhs_type	=	self.determine_param_type_ lhs
  rhs_type	=	self.determine_param_type_ rhs

  raise ArgumentError, "lhs is of unsupported type '#{lhs.class}'" unless lhs_type
  raise ArgumentError, "rhs is of unsupported type '#{rhs.class}'" unless rhs_type

  if lhs_type == rhs_type

    case lhs_type
    when :io

      return self.compare_text_streams_ lhs, rhs, options
    when :path

      return self.compare_text_files_ lhs, rhs, options
    end
  else

    case lhs_type
    when :io

      if :path == rhs_type

      	File.open(rhs, 'r') do |rhs_f|

      		return self.compare_text_streams_ lhs, rhs_f, options
      	end
      end
    when :path

      if :path == lhs_type

      	File.open(lhs, 'r') do |lhs_f|

      		return self.compare_text_streams_ lhs_f, rhs, options
      	end
      end
    end
  end

  warn "#{__method__}: incompatible types (#{lhs.type}, #{rhs.type})"

  nil
end

.compare_text_files_(lhs_path, rhs_path, options) ⇒ Object



60
61
62
63
64
65
66
67
68
69
# File 'lib/cmpfs/compare/text/internal_.rb', line 60

def self.compare_text_files_ lhs_path, rhs_path, options

  File.open(lhs_path, 'r') do |lhs_stm|

    File.open(rhs_path, 'r') do |rhs_stm|

      self.compare_text_streams_ lhs_stm, rhs_stm, options.merge(no_rewind: true)
    end
  end
end

.compare_text_streams_(lhs_stm, rhs_stm, options) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/cmpfs/compare/text/internal_.rb', line 71

def self.compare_text_streams_ lhs_stm, rhs_stm, options

  lhs_en	=	lhs_stm.each_line
  rhs_en	=	rhs_stm.each_line

  unless options[:no_rewind]

    lhs_en.rewind if lhs_stm.respond_to?(:rewind)
    rhs_en.rewind if rhs_stm.respond_to?(:rewind)
  end

  lhs_ix	=	0
  rhs_ix	=	0

  loop do

    lhs_ln, lhs_nr	=	self.next_line_or_nil_ lhs_en, options
    rhs_ln, rhs_nr	=	self.next_line_or_nil_ rhs_en, options

    if lhs_ln != rhs_ln

      return false
    else

      return true if lhs_ln.nil?
    end
  end

  return true
end

.determine_param_type_(p) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/cmpfs/compare/text/internal_.rb', line 12

def self.determine_param_type_ p

  case p
  when ::IO, ::StringIO

    return :io
  when ::String

    return :path
  else

    return :path if p.respond_to?(:to_str)

    return nil
  end
end

.next_line_or_nil_(en, options) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/cmpfs/compare/text/internal_.rb', line 29

def self.next_line_or_nil_ en, options

  skipping_blanks	=	options[:skip_blank_lines]
  trimming_lines	=	options[:trim_lines]

  num_read		=	0

  begin

    loop do

      line = en.next

      num_read += 1

      line = line.strip if trimming_lines

      if line.empty? && skipping_blanks

      	next
      end

      return [ line, num_read ]
    end
  rescue StopIteration

  end

  return [ nil, num_read ]
end