Skip to content

Commit af1234d

Browse files
authored
Merge pull request #29 from cgag/lua-fix
fix lua counting
2 parents 5cd37d1 + 236607e commit af1234d

File tree

4 files changed

+66
-12
lines changed

4 files changed

+66
-12
lines changed

Cargo.lock

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/lib.rs

+12-9
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ extern crate memmap;
55
extern crate memchr;
66

77
use std::path::Path;
8-
use std::cmp;
8+
use std::cmp::{min, max};
99
use std::fmt;
1010

1111
use memmap::{Mmap, Protection};
@@ -40,9 +40,7 @@ pub enum LineConfig<'a> {
4040
multi_start: &'a str,
4141
multi_end: &'a str,
4242
},
43-
SingleOnly {
44-
single_start: &'a str,
45-
},
43+
SingleOnly { single_start: &'a str },
4644
MultiOnly {
4745
multi_start: &'a str,
4846
multi_end: &'a str,
@@ -584,6 +582,7 @@ pub fn count_single(filepath: &str, single_start: &str) -> Count {
584582
c
585583
}
586584

585+
// TODO(cgag): don't forget to update this when fixing the lua bug
587586
pub fn count_multi(filepath: &str, multi_start: &str, multi_end: &str) -> Count {
588587
// this is a duplicate of count_single_multi without the check for single comment.
589588
// Basically removes one branch. Probably pointless: benchmark.
@@ -630,7 +629,7 @@ pub fn count_multi(filepath: &str, multi_start: &str, multi_end: &str) -> Count
630629
let mut found_code = false;
631630
'outer: while pos < trimmed.len() {
632631
// TODO(cgag): must be a less stupid way to do this
633-
for i in pos..(pos + cmp::max(start_len, end_len) + 1) {
632+
for i in pos..pos + min(max(start_len, end_len) + 1, trimmed.len() - pos) {
634633
if !trimmed.is_char_boundary(i) {
635634
pos += 1;
636635
continue 'outer;
@@ -642,7 +641,7 @@ pub fn count_multi(filepath: &str, multi_start: &str, multi_end: &str) -> Count
642641
pos += start_len;
643642
in_comment = true;
644643
} else if in_comment && pos + end_len <= trimmed.len() &&
645-
&trimmed[pos..(pos + end_len)] == multi_end {
644+
&trimmed[pos..(pos + end_len)] == multi_end {
646645
pos += end_len;
647646
in_comment = false;
648647
// TODO(cgag): should we bother handling whitespace here?
@@ -718,13 +717,17 @@ pub fn count_single_multi(filepath: &str,
718717
};
719718
c.lines += 1;
720719

720+
721721
let trimmed = line.trim_left();
722722
if trimmed.is_empty() {
723723
c.blank += 1;
724724
continue;
725725
};
726726

727-
if !in_comment && trimmed.starts_with(single_start) {
727+
// TODO(cgag): Could be more efficient by only doing this third check when
728+
// multi_start starts with the same chars as single_start, such as with lua (--, --[, ]]).
729+
// Not sure it's necessary
730+
if !in_comment && trimmed.starts_with(single_start) && !trimmed.starts_with(multi_start) {
728731
c.comment += 1;
729732
continue;
730733
}
@@ -748,7 +751,7 @@ pub fn count_single_multi(filepath: &str,
748751
// TODO(cgag): must be a less stupid way to do this. At the
749752
// very least don't recalculate max over and over. LLVM probably
750753
// optimizes this but it seems dumb to depend on it?
751-
for i in pos..(pos + cmp::max(start_len, end_len) + 1) {
754+
for i in pos..pos + min(max(start_len, end_len) + 1, trimmed.len() - pos) {
752755
if !trimmed.is_char_boundary(i) {
753756
pos += 1;
754757
continue 'outer;
@@ -760,7 +763,7 @@ pub fn count_single_multi(filepath: &str,
760763
pos += start_len;
761764
in_comment = true;
762765
} else if in_comment && pos + end_len <= trimmed_len &&
763-
&trimmed[pos..(pos + end_len)] == multi_end {
766+
&trimmed[pos..(pos + end_len)] == multi_end {
764767
pos += end_len;
765768
in_comment = false;
766769
// TODO(cgag): should we bother handling whitespace here?

tests/count.rs

+35
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const PLASMA_EXPECTED: Count = Count {
1111
};
1212

1313

14+
1415
#[test]
1516
fn test_plasma_count() {
1617
assert_eq!(PLASMA_EXPECTED, count(PLASMA));
@@ -172,3 +173,37 @@ fn test_ipl_blank() {
172173
fn test_ipl_lines() {
173174
assert_eq!(IPL_EXPECTED.lines, count(IPL).lines);
174175
}
176+
177+
// TODO(cgag): find or make a better testing tool? Or add some simple macros?
178+
const LUA: &'static str = "tests/data/lua.lua";
179+
const LUA_EXPECTED: Count = Count {
180+
code: 7,
181+
blank: 1,
182+
comment: 8,
183+
lines: 7 + 8 + 1,
184+
};
185+
186+
#[test]
187+
fn test_lua_count() {
188+
assert_eq!(LUA_EXPECTED, count(LUA));
189+
}
190+
191+
#[test]
192+
fn test_lua_code() {
193+
assert_eq!(LUA_EXPECTED.code, count(LUA).code);
194+
}
195+
196+
#[test]
197+
fn test_lua_comment() {
198+
assert_eq!(LUA_EXPECTED.comment, count(LUA).comment);
199+
}
200+
201+
#[test]
202+
fn test_lua_blank() {
203+
assert_eq!(LUA_EXPECTED.blank, count(LUA).blank);
204+
}
205+
206+
#[test]
207+
fn test_lua_lines() {
208+
assert_eq!(LUA_EXPECTED.lines, count(LUA).lines);
209+
}

tests/data/lua.lua

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
--[[ This
2+
is
3+
a
4+
multi-line
5+
comment,
6+
not
7+
code. ]]
8+
9+
-- build table
10+
statetab = {}
11+
local w1, w2 = NOWORD, NOWORD
12+
for w in allwords() do
13+
insert(prefix(w1, w2), w)
14+
w1 = w2; w2 = w;
15+
end
16+
insert(prefix(w1, w2), NOWORD)

0 commit comments

Comments
 (0)