Skip to content

Commit

Permalink
code review (#478)
Browse files Browse the repository at this point in the history
* chore: remove unused parameters

* chore: fix Go comments

* chore: use simpler syntax

* chore: remove useless method

* chore: avoid type juggling

* chore: reduce complexity
  • Loading branch information
ccoVeille committed Jun 13, 2024
1 parent 39f08a3 commit 2002edc
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 29 deletions.
6 changes: 3 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import (

//go:generate go run scripts/include.go
func main() {
//f, _ := os.Create("scc.pprof")
//pprof.StartCPUProfile(f)
//defer pprof.StopCPUProfile()
// f, _ := os.Create("scc.pprof")
// pprof.StartCPUProfile(f)
// defer pprof.StopCPUProfile()

if len(os.Args) == 2 && strings.HasPrefix(os.Args[1], "@") {
// handle "scc @flags.txt" syntax
Expand Down
6 changes: 3 additions & 3 deletions processor/detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,9 @@ func DetermineLanguage(filename string, fallbackLanguage string, possibleLanguag
return toSort[i].Count > toSort[j].Count
})

//fmt.Println(toSort)
//fmt.Println(possibleLanguages)
//fmt.Println(primary, toSort[0].Name, toSort[0].Count)
// fmt.Println(toSort)
// fmt.Println(possibleLanguages)
// fmt.Println(primary, toSort[0].Name, toSort[0].Count)

if primary != "" && len(toSort) != 0 {
// OK at this point we have a primary, which means we want 3 or more matches to count as something else
Expand Down
6 changes: 3 additions & 3 deletions processor/formatters.go
Original file line number Diff line number Diff line change
Expand Up @@ -1247,9 +1247,9 @@ func fileSummarizeShort(input chan *FileJob) string {
str.WriteString(fmt.Sprintf(tabularShortFormatFileNoComplexity, tmp, res.Lines, res.Blank, res.Comment, res.Code))
}

//if MaxMean {
// str.WriteString(fmt.Sprintf(tabularShortFormatFileMaxMean, maxIn(res.LineLength), meanIn(res.LineLength)))
//}
// if MaxMean {
// str.WriteString(fmt.Sprintf(tabularShortFormatFileMaxMean, maxIn(res.LineLength), meanIn(res.LineLength)))
// }
}
}

Expand Down
4 changes: 2 additions & 2 deletions processor/processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ func TestConfigureGc(t *testing.T) {

func TestConfigureLazy(t *testing.T) {
ConfigureLazy(true)
if isLazy != true {
if !isLazy {
t.Error("isLazy should be true")
}

ConfigureLazy(false)
if isLazy != false {
if isLazy {
t.Error("isLazy should be false")
}
}
Expand Down
15 changes: 6 additions & 9 deletions processor/workers.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func resetState(currentState int64) int64 {
return currentState
}

func stringState(fileJob *FileJob, index int, endPoint int, stringTrie *Trie, endString []byte, currentState int64, ignoreEscape bool) (int, int64) {
func stringState(fileJob *FileJob, index int, endPoint int, endString []byte, currentState int64, ignoreEscape bool) (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 @@ -134,12 +134,11 @@ func stringState(fileJob *FileJob, index int, endPoint int, stringTrie *Trie, en
// if there is an escape symbol before us, investigate
if fileJob.Content[i-1] == '\\' {
num_escapes := 0
for j := i - 1; j > 0; j -= 1 {
if fileJob.Content[j] == '\\' {
num_escapes += 1
} else {
for j := i - 1; j > 0; j-- {
if fileJob.Content[j] != '\\' {
break
}
num_escapes++
}

// if number of escapes is even, all escapes are themselves escaped
Expand Down Expand Up @@ -324,7 +323,6 @@ func commentState(fileJob *FileJob, index int, endPoint int, currentState int64,
func blankState(
fileJob *FileJob,
index int,
endPoint int,
currentState int64,
endComments [][]byte,
endString []byte,
Expand Down Expand Up @@ -465,7 +463,7 @@ func CountStats(fileJob *FileJob) {
&fileJob.Hash,
)
case SString:
index, currentState = stringState(fileJob, index, endPoint, langFeatures.Strings, endString, currentState, ignoreEscape)
index, currentState = stringState(fileJob, index, endPoint, endString, currentState, ignoreEscape)
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
Expand All @@ -486,7 +484,6 @@ func CountStats(fileJob *FileJob) {
index, currentState, endString, endComments, ignoreEscape = blankState(
fileJob,
index,
endPoint,
currentState,
endComments,
endString,
Expand Down Expand Up @@ -737,7 +734,7 @@ func processFile(job *FileJob) bool {
}

// if we didn't remap we then want to see if it's a #! map
if remapped == false {
if !remapped {
cutoff := 200

// To avoid runtime panic check if the content we are cutting is smaller than 200
Expand Down
13 changes: 4 additions & 9 deletions scripts/include.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ import (

const constantsFile = "./processor/constants.go"

func fatalf(f string, v ...interface{}) {
fmt.Fprintf(os.Stderr, f+"\n", v...)
os.Exit(1)
}

// Reads all .json files in the current folder
// and encodes them as strings literals in constants.go
func generateConstants() error {
Expand All @@ -30,7 +25,7 @@ func generateConstants() error {
defer os.Remove(out.Name())

// Open constants
out.Write([]byte("package processor \n\nconst (\n"))
out.WriteString("package processor \n\nconst (\n")

for _, f := range files {
if strings.HasPrefix(f.Name(), "languages") && strings.HasSuffix(f.Name(), ".json") {
Expand All @@ -48,7 +43,7 @@ func generateConstants() error {
f.Seek(0, io.SeekStart)

// The constant variable name
out.Write([]byte(strings.TrimSuffix(f.Name(), ".json") + " = `"))
out.WriteString(strings.TrimSuffix(f.Name(), ".json") + " = `")

enc := base64.NewEncoder(base64.StdEncoding, out)
gz, _ := gzip.NewWriterLevel(enc, gzip.BestSpeed)
Expand All @@ -58,12 +53,12 @@ func generateConstants() error {
gz.Close()
enc.Close()

out.Write([]byte("`\n"))
out.WriteString("`\n")
}
}

// Close out constants
out.Write([]byte(")\n"))
out.WriteString(")\n")
out.Close()

if err := os.Rename(out.Name(), constantsFile); err != nil {
Expand Down

0 comments on commit 2002edc

Please sign in to comment.