From 2e49c06f47d63ac3de03de59ecd98466c83497a7 Mon Sep 17 00:00:00 2001 From: Ryan Tomayko Date: Mon, 10 Sep 2012 01:05:48 -0700 Subject: [PATCH 1/3] Handle :sparkles:Mac Format:sparkles: when splitting lines --- lib/linguist/blob_helper.rb | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/lib/linguist/blob_helper.rb b/lib/linguist/blob_helper.rb index 04c3f0a9..de75dbc9 100644 --- a/lib/linguist/blob_helper.rb +++ b/lib/linguist/blob_helper.rb @@ -204,7 +204,31 @@ module Linguist # # Returns an Array of lines def lines - @lines ||= (viewable? && data) ? data.split("\n", -1) : [] + @lines ||= + if viewable? && data + data.split(line_split_character, -1) + else + [] + end + end + + # Character used to split lines. This is almost always "\n" except when Mac + # Format is detected in which case it's "\r". + # + # Returns a split pattern string. + def line_split_character + @line_split_character ||= (mac_format?? "\r" : "\n") + end + + # Public: Is the data in ** Mac Format **. This format uses \r (0x0d) characters + # for line ends and does not include a \n (0x0a). + # + # Returns true when mac format is detected. + def mac_format? + return if !viewable? + if pos = data.index("\r") + data[pos + 1] != ?\n + end end # Public: Get number of lines of code From bda895eaae1b06943daf006631f3ffb584d3dab9 Mon Sep 17 00:00:00 2001 From: Ryan Tomayko Date: Mon, 10 Sep 2012 01:52:30 -0700 Subject: [PATCH 2/3] Test Mac Format detection and line splitting --- samples/Text/mac.txt | 1 + test/test_blob.rb | 8 ++++++++ 2 files changed, 9 insertions(+) create mode 100644 samples/Text/mac.txt diff --git a/samples/Text/mac.txt b/samples/Text/mac.txt new file mode 100644 index 00000000..c22612b9 --- /dev/null +++ b/samples/Text/mac.txt @@ -0,0 +1 @@ +line 1 line 2 \ No newline at end of file diff --git a/test/test_blob.rb b/test/test_blob.rb index 17e9ef8c..333b0cec 100644 --- a/test/test_blob.rb +++ b/test/test_blob.rb @@ -65,6 +65,14 @@ class TestBlob < Test::Unit::TestCase assert_equal ["module Foo", "end", ""], blob("Ruby/foo.rb").lines end + def test_mac_format + assert blob("Text/mac.txt").mac_format? + end + + def test_lines_mac_format + assert_equal ["line 1", "line 2", ""], blob("Text/mac.txt").lines + end + def test_size assert_equal 15, blob("Ruby/foo.rb").size end From 887a050db9a0c945d06af86ca550d18f46cf176a Mon Sep 17 00:00:00 2001 From: Ryan Tomayko Date: Mon, 10 Sep 2012 01:56:08 -0700 Subject: [PATCH 3/3] Only search the first 4K chars for \r --- lib/linguist/blob_helper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/linguist/blob_helper.rb b/lib/linguist/blob_helper.rb index de75dbc9..b0a20626 100644 --- a/lib/linguist/blob_helper.rb +++ b/lib/linguist/blob_helper.rb @@ -226,7 +226,7 @@ module Linguist # Returns true when mac format is detected. def mac_format? return if !viewable? - if pos = data.index("\r") + if pos = data[0, 4096].index("\r") data[pos + 1] != ?\n end end