Skip to content

Commit

Permalink
Merge pull request #250 from github/mac-format
Browse files Browse the repository at this point in the history
Handle Mac Format when splitting lines
  • Loading branch information
rtomayko committed Sep 11, 2012
2 parents ae13784 + 887a050 commit 567cd6e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
26 changes: 25 additions & 1 deletion lib/linguist/blob_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,31 @@ def vendored?
#
# 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[0, 4096].index("\r")
data[pos + 1] != ?\n
end
end

# Public: Get number of lines of code
Expand Down
1 change: 1 addition & 0 deletions samples/Text/mac.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
line 1line 2
Expand Down
8 changes: 8 additions & 0 deletions test/test_blob.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ def test_lines
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
Expand Down

0 comments on commit 567cd6e

Please sign in to comment.