Skip to content

Commit

Permalink
version 5: optimised stats, language generation
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron Power committed Jan 3, 2017
1 parent 5e11c48 commit c08f113
Show file tree
Hide file tree
Showing 18 changed files with 296 additions and 317 deletions.
1 change: 1 addition & 0 deletions .gitattributes
@@ -0,0 +1 @@
tests/data/* linguist-documentation
24 changes: 13 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 29 additions & 26 deletions Cargo.toml
Expand Up @@ -8,67 +8,70 @@ license = "MIT/Apache-2.0"
name = "tokei"
readme = "README.md"
repository = "https://github.com/Aaronepower/tokei.git"
version = "4.5.4"
version = "5.0.3"

[profile.release]
panic="abort"

[build-dependencies]
serde = "~0.8.19"
serde_json = "~0.8.4"
errln = "0.1.0"
serde = "0.8"
serde_json = "0.8"
errln = "0.1"

[build-dependencies.handlebars]
features = ["serde_type"]
version = "0.21.1"
version = "~0.24"

[build-dependencies.serde_codegen]
optional = true
version = "0.8.19"
version = "0.8"

[dependencies]
encoding = "~0.2.33"
errln = "0.1.0"
ignore = "~0.1.5"
lazy_static = "~0.2.1"
log = "~0.3.6"
maplit = "~0.1.3"
rayon = "=0.4.2"
regex = "~0.1.80"
encoding = "0.2"
errln = "0.1"
ignore = "0.1"
lazy_static = "0.2"
log = "0.3"
maplit = "0.1"
rayon = "0.6"
regex = "0.1"

[dependencies.clap]
features = ["yaml"]
version = "~2.19.1"
version = "2.19"

[dependencies.env_logger]
features = []
version = "~0.3.5"
version = "0.3"

[dependencies.hex]
version = "0.2"
optional = true

[dependencies.serde]
optional = true
version = "~0.8.19"
version = "0.8"

[dependencies.serde_cbor]
optional = true
version = "~0.4.0"
version = "0.4"

[dependencies.serde_json]
optional = true
version = "~0.8.4"
version = "0.8"

[dependencies.serde_yaml]
optional = true
version = "~0.4.0"
version = "0.4"

[dependencies.toml]
default-features = false
features = ["serde"]
optional = true
version = "~0.2.1"
version = "0.2"

[dev-dependencies]
tempdir = "~0.3.5"

[dependencies.hex]
version = "~0.2.0"
optional = true
tempdir = "0.3"

[features]
all = ["json", "cbor", "toml-io", "yaml"]
Expand Down
5 changes: 2 additions & 3 deletions build.rs
Expand Up @@ -5,7 +5,7 @@ extern crate handlebars;
#[macro_use] extern crate errln;

use serde_json::Value;
use handlebars::{Context, Handlebars};
use handlebars::Handlebars;
use std::fs::File;
use std::env;
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -62,11 +62,10 @@ fn render_handlebars(out_dir: &OsString) -> PathBuf {
let mut handlebars = Handlebars::new();
handlebars.register_escape_fn(handlebars::no_escape);

let raw_data: Value = serde_json::from_reader(
let data: Value = serde_json::from_reader(
File::open(&"languages.json").expect("Can't open JSON")
).expect("Can't parse JSON");

let data = Context::wraps(&raw_data);
let out = Path::new(&out_dir).join("language_type.rs");
let mut source_template = File::open(&"src/language/language_type.hbs.rs")
.expect("Can't find Template");
Expand Down
2 changes: 1 addition & 1 deletion cli.yml
Expand Up @@ -5,7 +5,7 @@ about: Count Code, Quickly.
author: Aaron P. <theaaronepower@gmail.com>
bin_name: Tokei
name: Tokei
version: 4.5.4
version: '5'
args:
- exclude:
help: Ignore all files & directories containing the word.
Expand Down
9 changes: 4 additions & 5 deletions languages.json
Expand Up @@ -85,7 +85,7 @@
]
},
"Sh":{
"name":"sh",
"name":"Shell",
"base":"hash",
"quotes":[
[
Expand Down Expand Up @@ -452,10 +452,9 @@
"single":[
"//"
],
"multi":[[
"(*",
"*)"
]],
"multi":[
["(*", "*)"]
],
"extensions":[
"fs",
"fsi",
Expand Down
51 changes: 20 additions & 31 deletions src/language/language.rs
@@ -1,6 +1,7 @@
use std::borrow::Cow;
use std::ops::AddAssign;
use std::path::PathBuf;
use std::mem;

use regex::{self, Regex};

Expand All @@ -24,10 +25,15 @@ fn generate_regex(multi_line: &[(&'static str, &'static str)]) -> Cow<'static, R
Cow::Owned(Regex::new(&raw_regex).unwrap())
}

lazy_static! {
static ref C_REGEX: Regex = Regex::new(r"/\*").unwrap();
fn get_c_regex() -> &'static Regex {
lazy_static! {
static ref C_REGEX: Regex = Regex::new(r"/\*").unwrap();
}

&*C_REGEX
}


impl Language {
/// Constructs a new empty Language with the comments provided.
///
Expand All @@ -43,6 +49,7 @@ impl Language {
line_comment: line_comment,
regex: Some(generate_regex(&multi_line)),
multi_line: multi_line,
quotes: vec![("\"", "\"")],
..Self::default()
}
}
Expand Down Expand Up @@ -76,7 +83,7 @@ impl Language {
line_comment: vec!["//"],
multi_line: vec![("/*", "*/")],
quotes: vec![("\"", "\"")],
regex: Some(Cow::Borrowed(&*C_REGEX)),
regex: Some(Cow::Borrowed(get_c_regex())),
..Self::default()
}
}
Expand All @@ -94,7 +101,7 @@ impl Language {
/// ```
pub fn new_func() -> Self {
lazy_static! {
static ref FUNC_REGEX: Regex = Regex::new(r"\(\*").unwrap();
static ref FUNC_REGEX: Regex = Regex::new(&regex::quote(r"\(\*")).unwrap();
}
Language {
multi_line: vec![("(*", "*)")],
Expand Down Expand Up @@ -155,7 +162,7 @@ impl Language {
/// ```
pub fn new_haskell() -> Self {
lazy_static! {
static ref HASKELL_REGEX: Regex = Regex::new(r"\{-").unwrap();
static ref HASKELL_REGEX: Regex = Regex::new(&regex::quote(r"\{-")).unwrap();
}

Language {
Expand Down Expand Up @@ -199,7 +206,7 @@ impl Language {
line_comment: vec!["%"],
multi_line: vec![("/*", "*/")],
quotes: vec![("\"", "\"")],
regex: Some(Cow::Borrowed(&*C_REGEX)),
regex: Some(Cow::Borrowed(get_c_regex())),
..Self::default()
}
}
Expand Down Expand Up @@ -302,10 +309,12 @@ impl Language {
/// panic!'s if given the wrong category.
///
/// ```
/// # use tokei::*;
/// use tokei::{Language, Stats, Sort};
/// use std::path::PathBuf;
///
/// let mut rust = Language::new_c();
/// let mut foo_stats = Stats::new("foo");
/// let mut bar_stats = Stats::new("bar");
/// let mut foo_stats = Stats::new(PathBuf::from("foo"));
/// let mut bar_stats = Stats::new(PathBuf::from("bar"));
///
/// foo_stats.code += 20;
/// bar_stats.code += 10;
Expand All @@ -332,32 +341,12 @@ impl Language {
}

impl AddAssign for Language {
fn add_assign(&mut self, rhs: Self) {
self.lines += rhs.lines;
self.comments += rhs.comments;
self.blanks += rhs.blanks;
self.code += rhs.code;
self.stats.extend_from_slice(&*rhs.stats);
}
}

impl<'a> AddAssign<&'a Language> for Language {
fn add_assign(&mut self, rhs: &'a Self) {
self.lines += rhs.lines;
self.comments += rhs.comments;
self.blanks += rhs.blanks;
self.code += rhs.code;
self.stats.extend_from_slice(&*rhs.stats);
}
}

impl<'a> AddAssign<&'a mut Language> for Language {
fn add_assign(&mut self, rhs: &mut Self) {
fn add_assign(&mut self, mut rhs: Self) {
self.lines += rhs.lines;
self.comments += rhs.comments;
self.blanks += rhs.blanks;
self.code += rhs.code;
self.stats.extend_from_slice(&*rhs.stats);
self.stats.extend(mem::replace(&mut rhs.stats, Vec::new()));
}
}

Expand Down

0 comments on commit c08f113

Please sign in to comment.