Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions processor/workers.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func stringState(fileJob *FileJob, index int, endPoint int, endString []byte, cu

// This is a special state check pretty much only ever used by Python codebases
// but potentially it could be expanded to deal with other types
func docStringState(fileJob *FileJob, index int, endPoint int, stringTrie *Trie, endString []byte, currentState int64) (int, int64) {
func docStringState(fileJob *FileJob, index int, endPoint int, endString []byte, currentState int64) (int, int64) {
// It's not possible to enter this state without checking at least 1 byte so it is safe to check -1 here
// without checking if it is out of bounds first
for i := index; i < endPoint; i++ {
Expand All @@ -187,7 +187,7 @@ func docStringState(fileJob *FileJob, index int, endPoint int, stringTrie *Trie,
}

if fileJob.Content[i-1] != '\\' {
if ok, _, _ := stringTrie.Match(fileJob.Content[i:]); ok != 0 {
if checkForMatchSingle(fileJob.Content[i], index, endPoint, endString, fileJob) {
// So we have hit end of docstring at this point in which case check if only whitespace characters till the next
// newline and if so we change to a comment otherwise to code
// need to start the loop after ending definition of docstring, therefore adding the length of the string to
Expand Down Expand Up @@ -515,7 +515,7 @@ func CountStats(fileJob *FileJob) {
case SDocString:
// For a docstring we can either move into blank in which case we count it as a docstring
// or back into code in which case it should be counted as code
index, currentState = docStringState(fileJob, index, endPoint, langFeatures.Strings, endString, currentState)
index, currentState = docStringState(fileJob, index, endPoint, endString, currentState)
case SMulticomment, SMulticommentCode:
index, currentState, endString, endComments = commentState(
fileJob,
Expand Down
49 changes: 43 additions & 6 deletions processor/workers_regression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,23 +134,60 @@ hello there! how's it going?

CountStats(&fileJob)

if fileJob.Lines != 2 {
t.Errorf("Expected 2 lines got %d", fileJob.Lines)
if fileJob.Lines != 3 {
t.Errorf("Expected 3 lines got %d", fileJob.Lines)
}

if fileJob.Code != 1 {
t.Errorf("Expected 1 lines got %d", fileJob.Code)
if fileJob.Code != 0 {
t.Errorf("Expected 0 lines got %d", fileJob.Code)
}

if fileJob.Comment != 1 {
t.Errorf("Expected 1 lines got %d", fileJob.Comment)
if fileJob.Comment != 3 {
t.Errorf("Expected 3 lines got %d", fileJob.Comment)
}

if fileJob.Blank != 0 {
t.Errorf("Expected 0 lines got %d", fileJob.Blank)
}
}

func TestCountStatsIssue246(t *testing.T) {
ProcessConstants()
fileJob := FileJob{
Language: "Python",
}

// Apostrophes inside triple-double-quoted docstrings should not
// break docstring parsing (issue #246).
fileJob.SetContent(`#!/usr/bin/env python3

"""
Docstrings containing an apostrophe (') are handled incorrectly
"""
# A comment
if __name__ == '__main__':
print('Hello, World!')
`)

CountStats(&fileJob)

if fileJob.Lines != 8 {
t.Errorf("Expected 8 lines got %d", fileJob.Lines)
}

if fileJob.Code != 2 {
t.Errorf("Expected 2 code lines got %d", fileJob.Code)
}

if fileJob.Comment != 5 {
t.Errorf("Expected 5 comment lines got %d", fileJob.Comment)
}

if fileJob.Blank != 1 {
t.Errorf("Expected 1 blank line got %d", fileJob.Blank)
}
}

func TestCountStatsIssue230(t *testing.T) {
ProcessConstants()
fileJob := FileJob{
Expand Down
Loading