Files
linguist/lib/linguist/file_blob.rb
Ashe Connor 99eaf5faf9 Replace the tokenizer with a flex-based scanner (#3846)
* Lex everything except SGML, multiline, SHEBANG

* Prepend SHEBANG#! to tokens

* Support SGML tag/attribute extraction

* Multiline comments

* WIP cont'd; productionifying

* Compile before test

* Add extension to gemspec

* Add flex task to build lexer

* Reentrant extra data storage

* regenerate lexer

* use prefix

* rebuild lexer on linux

* Optimise a number of operations:

* Don't read and split the entire file if we only ever use the first/last n
  lines

* Only consider the first 50KiB when using heuristics/classifying.  This can
  save a *lot* of time; running a large number of regexes over 1MiB of text
  takes a while.

* Memoize File.size/read/stat; re-reading in a 500KiB file every time `data` is
  called adds up a lot.

* Use single regex for C++

* act like #lines

* [1][-2..-1] => nil, ffs

* k may not be set
2017-10-31 11:06:56 +11:00

44 lines
1.0 KiB
Ruby

require 'linguist/blob_helper'
require 'linguist/blob'
module Linguist
# A FileBlob is a wrapper around a File object to make it quack
# like a Grit::Blob. It provides the basic interface: `name`,
# `data`, `path` and `size`.
class FileBlob < Blob
include BlobHelper
# Public: Initialize a new FileBlob from a path
#
# path - A path String that exists on the file system.
# base_path - Optional base to relativize the path
#
# Returns a FileBlob.
def initialize(path, base_path = nil)
@fullpath = path
@path = base_path ? path.sub("#{base_path}/", '') : path
end
# Public: Read file permissions
#
# Returns a String like '100644'
def mode
@mode ||= File.stat(@fullpath).mode.to_s(8)
end
# Public: Read file contents.
#
# Returns a String.
def data
@data ||= File.read(@fullpath)
end
# Public: Get byte size
#
# Returns an Integer.
def size
@size ||= File.size(@fullpath)
end
end
end