Skip to content

Commit

Permalink
Merge pull request #18 from jhaynie/master
Browse files Browse the repository at this point in the history
Proposed changes to support API
  • Loading branch information
boyter authored Jun 29, 2018
2 parents 646eef7 + cd26c2b commit d71db5c
Show file tree
Hide file tree
Showing 5 changed files with 183 additions and 76 deletions.
11 changes: 5 additions & 6 deletions processor/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"runtime"
"strings"
"io/ioutil"
"runtime"
"sort"
"strings"
)

// Flags set via the CLI which control how the output is displayed
Expand Down Expand Up @@ -39,9 +39,9 @@ var DirFilePaths = []string{}
var ExtensionToLanguage = map[string]string{}
var LanguageFeatures = map[string]LanguageFeature{}

// Responsible for setting up the language features based on the JSON file that is stored in constants
// ProcessConstants is responsible for setting up the language features based on the JSON file that is stored in constants
// Needs to be called at least once in order for anything to actually happen
func processConstants() {
func ProcessConstants() {
var database = loadDatabase()

startTime := makeTimestampNano()
Expand Down Expand Up @@ -121,7 +121,6 @@ func processFlags() {
}
}


func loadDatabase() map[string]Language {
var database map[string]Language
startTime := makeTimestampMilli()
Expand Down Expand Up @@ -165,7 +164,7 @@ func Process() {
return
}

processConstants()
ProcessConstants()
processFlags()

// Clean up any invalid arguments before setting everything up
Expand Down
2 changes: 1 addition & 1 deletion processor/processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
)

func TestProcessConstants(t *testing.T) {
processConstants()
ProcessConstants()

if len(ExtensionToLanguage) == 0 {
t.Error("Should not be 0")
Expand Down
7 changes: 7 additions & 0 deletions processor/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ type LanguageFeature struct {
StringChecks []OpenClose
}

// FileJobCallback is an interface that FileJobs can implement to get a per line callback with the line type
type FileJobCallback interface {
// ProcessLine should return true to continue processing or false to stop further processing and return
ProcessLine(job *FileJob, currentLine int64, lineType LineType) bool
}

type FileJob struct {
Language string
Filename string
Expand All @@ -35,6 +41,7 @@ type FileJob struct {
Complexity int64
WeightedComplexity float64
Hash []byte
Callback FileJobCallback
}

type LanguageSummary struct {
Expand Down
40 changes: 35 additions & 5 deletions processor/workers.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ const (
S_STRING int64 = 8
)

type LineType int32

const (
LINE_BLANK LineType = iota
LINE_CODE
LINE_COMMENT
)

func checkForMatch(currentByte byte, index int, endPoint int, matches [][]byte, fileJob *FileJob) bool {
potentialMatch := true
for i := 0; i < len(matches); i++ {
Expand Down Expand Up @@ -166,11 +174,12 @@ func isWhitespace(currentByte byte) bool {
return true
}

// CountStats will process the fileJob
// If the file contains anything even just a newline its line count should be >= 1.
// If the file has a size of 0 its line count should be 0.
// Newlines belong to the line they started on so a file of \n means only 1 line
// This is the 'hot' path for the application and needs to be as fast as possible
func countStats(fileJob *FileJob) {
func CountStats(fileJob *FileJob) {

// If the file has a length of 0 it is is empty then we say it has no lines
fileJob.Bytes = int64(len(fileJob.Content))
Expand Down Expand Up @@ -304,11 +313,32 @@ func countStats(fileJob *FileJob) {

switch {
case currentState == S_BLANK:
fileJob.Blank++
{
if fileJob.Callback != nil {
if !fileJob.Callback.ProcessLine(fileJob, fileJob.Lines, LINE_BLANK) {
return
}
}
fileJob.Blank++
}
case currentState == S_CODE || currentState == S_STRING || currentState == S_COMMENT_CODE || currentState == S_MULTICOMMENT_CODE:
fileJob.Code++
{
if fileJob.Callback != nil {
if !fileJob.Callback.ProcessLine(fileJob, fileJob.Lines, LINE_CODE) {
return
}
}
fileJob.Code++
}
case currentState == S_COMMENT || currentState == S_MULTICOMMENT || currentState == S_MULTICOMMENT_BLANK:
fileJob.Comment++
{
if fileJob.Callback != nil {
if !fileJob.Callback.ProcessLine(fileJob, fileJob.Lines, LINE_COMMENT) {
return
}
}
fileJob.Comment++
}
}

// If we are in a multiline comment that started after some code then we need
Expand Down Expand Up @@ -384,7 +414,7 @@ func fileProcessorWorker(input *chan *FileJob, output *chan *FileJob) {
wg.Add(1)
go func(res *FileJob) {
fileStartTime := makeTimestampNano()
countStats(res)
CountStats(res)

if Duplicates {
if duplicates.Check(res.Bytes, res.Hash) {
Expand Down
Loading

0 comments on commit d71db5c

Please sign in to comment.