mirror of
https://github.com/KevinMidboe/linguist.git
synced 2025-10-29 17:50:22 +00:00
Counts the number of lines correctly for files with certain multibyte encodings
This commit is contained in:
@@ -241,7 +241,8 @@ module Linguist
|
||||
def lines
|
||||
@lines ||=
|
||||
if viewable? && data
|
||||
data.split(/\r\n|\r|\n/, -1)
|
||||
newlines = Regexp.new("\r\n|\r|\n".encode(encoding))
|
||||
data.force_encoding(encoding).split(newlines, -1)
|
||||
else
|
||||
[]
|
||||
end
|
||||
@@ -262,7 +263,7 @@ module Linguist
|
||||
#
|
||||
# Returns Integer
|
||||
def sloc
|
||||
lines.grep(/\S/).size
|
||||
lines.grep(Regexp.new('\S'.encode(encoding))).size
|
||||
end
|
||||
|
||||
# Public: Is the blob a generated file?
|
||||
|
||||
BIN
samples/Text/utf16le-windows.txt
Normal file
BIN
samples/Text/utf16le-windows.txt
Normal file
Binary file not shown.
BIN
samples/Text/utf16le.txt
Normal file
BIN
samples/Text/utf16le.txt
Normal file
Binary file not shown.
@@ -77,12 +77,15 @@ class TestBlob < Test::Unit::TestCase
|
||||
|
||||
def test_sloc
|
||||
assert_equal 2, blob("Ruby/foo.rb").sloc
|
||||
assert_equal 3, blob("Text/utf16le-windows.txt").sloc
|
||||
end
|
||||
|
||||
def test_encoding
|
||||
assert_equal "ISO-8859-2", blob("Text/README").encoding
|
||||
assert_equal "ISO-8859-1", blob("Text/dump.sql").encoding
|
||||
assert_equal "UTF-8", blob("Text/foo.txt").encoding
|
||||
assert_equal "UTF-16LE", blob("Text/utf16le.txt").encoding
|
||||
assert_equal "UTF-16LE", blob("Text/utf16le-windows.txt").encoding
|
||||
assert_nil blob("Binary/dog.o").encoding
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user