line-detector 0.2 → 0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/bin/line-detector +5 -2
- data/lib/line-detector.rb +70 -0
- data/lib/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c363e46237d48d94fe3081fbfc6541f90febaabb
|
4
|
+
data.tar.gz: 487364d2c3cd7c916fea9d71a12043034d15b53f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6fbac38b0ae97d7b874185df209847f45c1ecf574cad889358ad48c20111c4bdcbd3afc867a6e5c47648f4cecc4c760d21cef33e70e0b105f22a9bdf94704763
|
7
|
+
data.tar.gz: 0c2edf9190fc65496a7f7296ac4eb59395b3e056f7cf156597e095c98bbfec42c81299df7e36a1b3f64ede57da0dc58f4bc0b680f35d02681dd1c5ef42fa7062
|
data/bin/line-detector
CHANGED
@@ -28,11 +28,14 @@ def main
|
|
28
28
|
filename = ARGV.first unless ARGV.empty?
|
29
29
|
|
30
30
|
if filename == '-'
|
31
|
-
|
31
|
+
text = STDIN.read
|
32
|
+
report = LineDetector.report_of_text(text)
|
33
|
+
puts report
|
32
34
|
elsif !File.exists?(filename)
|
33
35
|
puts "#{filename} does not exist"
|
34
36
|
else
|
35
|
-
|
37
|
+
report = LineDetector.report_of_file(filename)
|
38
|
+
puts "#{filename}: #{report}"
|
36
39
|
end
|
37
40
|
end
|
38
41
|
|
data/lib/line-detector.rb
CHANGED
@@ -53,6 +53,28 @@ module LineDetector
|
|
53
53
|
end
|
54
54
|
end
|
55
55
|
|
56
|
+
#
|
57
|
+
# Detects presence of final end of line in arbitrary text
|
58
|
+
#
|
59
|
+
# "abc\ndef\n" => true
|
60
|
+
# "abc\r\ndef\r\n" => true
|
61
|
+
# "abc\ndef" => false
|
62
|
+
# "abc\r\ndef\r" => false
|
63
|
+
# "abcdef" => false
|
64
|
+
# "" => false
|
65
|
+
#
|
66
|
+
def self.detect_final_eol_of_text(text)
|
67
|
+
line_ending = detect_line_ending_of_text(text)
|
68
|
+
|
69
|
+
if line_ending == :none
|
70
|
+
false
|
71
|
+
elsif line_ending == :mix
|
72
|
+
false
|
73
|
+
else
|
74
|
+
text.end_with?(NAME2EOL[line_ending])
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
56
78
|
#
|
57
79
|
# Detect line ending format of a file
|
58
80
|
#
|
@@ -62,6 +84,54 @@ module LineDetector
|
|
62
84
|
detect_line_ending_of_text(open(filename).read)
|
63
85
|
end
|
64
86
|
|
87
|
+
#
|
88
|
+
# Detect presence of final end of line in a file
|
89
|
+
#
|
90
|
+
# Assumes file is a text file.
|
91
|
+
#
|
92
|
+
def self.detect_final_eol_of_file(filename)
|
93
|
+
detect_final_eol_of_text(open(filename).read)
|
94
|
+
end
|
95
|
+
|
96
|
+
class Report
|
97
|
+
attr_accessor :line_ending, :final_eol
|
98
|
+
|
99
|
+
def initialize(line_ending, final_eol)
|
100
|
+
@line_ending = line_ending
|
101
|
+
@final_eol = final_eol
|
102
|
+
end
|
103
|
+
|
104
|
+
def to_s
|
105
|
+
final_eol_piece =
|
106
|
+
if @final_eol
|
107
|
+
', with final eol'
|
108
|
+
else
|
109
|
+
', without final eol'
|
110
|
+
end
|
111
|
+
|
112
|
+
"#{@line_ending.to_s}#{final_eol_piece}"
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
#
|
117
|
+
# Report line ending and presence of final line ending of arbitrary text
|
118
|
+
#
|
119
|
+
def self.report_of_text(text)
|
120
|
+
Report.new(
|
121
|
+
detect_line_ending_of_text(text),
|
122
|
+
detect_final_eol_of_text(text)
|
123
|
+
)
|
124
|
+
end
|
125
|
+
|
126
|
+
#
|
127
|
+
# Detect presence of final end of line in a file
|
128
|
+
#
|
129
|
+
# Assumes file is a text file.
|
130
|
+
#
|
131
|
+
def self.report_of_file(filename)
|
132
|
+
report_of_text(open(filename).read)
|
133
|
+
end
|
134
|
+
|
65
135
|
#
|
66
136
|
# A more capable version of String#lines,
|
67
137
|
# that handles some of the more obscure line ending formats.
|
data/lib/version.rb
CHANGED