-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsurvey.go
931 lines (836 loc) · 24.7 KB
/
survey.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
package main
import (
"archive/tar"
"archive/zip"
"bufio"
"bytes"
"compress/bzip2"
"compress/gzip"
"flag"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"sort"
"strings"
"time"
"github.com/MichaelTJones/lex"
"github.com/cavaliercoder/go-cpio"
"github.com/klauspost/compress/zstd"
)
/*
Survey any number of Go source code files, where surveying means passing each through a
Go-language lexical analysis and tabulating the various tokens found: the number of each
keyword and pre-defined identifier, the number and type of comments and strings, the
number and value of identifiers, the frequency of operators, and so on. The result is a
series of tables detailing counts and values.
Some of the counts have structural meaning: the number of "package" statements is also the
number of Go-language source files and the numbers of left and right parenthesis,
brackets, and braces must match in valid Go code. When surveying the Go source tree, for
example, the count of "./go/src" shows these values to balance as expected, while the full
survey of "./go" -- which includes "./go/test" -- does not match because of tests that
have invalid syntax as the purpose of their test. These files are easily identified with
the "-v" verbose option. In verbose mode, the report and any log file show the out of
balance files. Here is the result for the Go 1.13 source tree:
(2:1) [0:0] {1:0} go/test/fixedbugs/bug435.go
(2:1) [0:0] {1:1} go/test/fixedbugs/issue13248.go
(1:1) [0:0] {1:0} go/test/fixedbugs/issue13274.go
(6:4) [0:0] {2:2} go/test/fixedbugs/issue13319.go
(1:0) [0:0] {0:0} go/test/fixedbugs/issue15611.go
(1:2) [0:0] {2:2} go/test/fixedbugs/issue17328.go
(1:1) [0:0] {3:2} go/test/fixedbugs/issue18092.go
(2:1) [0:0] {1:1} go/test/fixedbugs/issue19667.go
(1:1) [2:0] {1:0} go/test/fixedbugs/issue20789.go
(8:6) [1:1] {5:4} go/test/fixedbugs/issue22164.go
(7:10) [0:3] {7:7} go/test/fixedbugs/issue22581.go
(1:1) [0:0] {2:0} go/test/syntax/semi1.go
(1:1) [0:0] {2:0} go/test/syntax/semi2.go
(1:1) [0:0] {2:0} go/test/syntax/semi3.go
(1:1) [0:0] {2:0} go/test/syntax/semi4.go
(1:1) [0:0] {1:0} go/test/syntax/semi5.go
(1:1) [1:1] {2:1} go/test/syntax/vareq.go
The meaning of the first line is that "go/test/fixedbugs/bug435.go" has 2 "(" and 1 ")",
no "[" or "]", and 1 "{" and 0 "}". These mismatches, result in an overall summary
mismatch:
Count Percent Token subtype
981939 18.8815% ,
602038 11.5765% (
602034 11.5764% )
601369 11.5636% .
463464 8.9119% =
422346 8.1212% :
380508 7.3167% {
380493 7.3164% }
152008 2.9229% ]
152007 2.9229% [
*/
func doSurvey() {
if *flagVerbose {
detailCPU() // useful in benchmark analysis
}
println("survey begins")
s := NewSurvey()
surveyed := false
// survey files in the file of filenames indicated by the "-list" option.
if *flagList != "" {
println("processing files listed in the -list option")
s.List(*flagList)
surveyed = true
}
// survey files named on command line.
if flag.NArg() != 0 {
println("processing files listed on command line")
for _, v := range flag.Args() {
s.File(v)
}
surveyed = true
}
// survey files named in standard input if nothing surveyed yet.
if !surveyed {
println("processing files listed in standard input")
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
s.File(scanner.Text())
}
}
s.Complete()
println("survey ends")
// generate output report
if surveyed {
println("report begins")
s.Report()
println("report ends")
}
}
type Survey struct {
complete bool // post-survey data adjustments have been made
start time.Time
elapsed float64 // wall-clock time for survey
user float64 // user time in process, sum of all threads
system float64 // system time on process' behalf
size uint64 // process RSS in bytes
good []string // go files that had no parsing issues
bad []string // go files that were strange in some way
files int
lines int
bytes int
tokens int
extensions map[string]int // file extensions processed: .go, .zip, .gz, ...
ascii map[string]int
bases map[string]int
comments map[string]int
identifiers map[string]int
keywords map[string]int
operators map[string]int
others map[string]int
packages map[string]int
runes map[string]int
strings map[string]int
types map[string]int
unicode map[string]int
countComments [3]int // count directly rather than via strings-in-map
countIdentifiers [3]int
countStrings [3]int
countBases [6]int
}
func NewSurvey() *Survey {
return &Survey{
extensions: make(map[string]int),
packages: make(map[string]int),
operators: make(map[string]int),
runes: make(map[string]int),
ascii: make(map[string]int),
unicode: make(map[string]int),
keywords: make(map[string]int),
types: make(map[string]int),
others: make(map[string]int),
comments: make(map[string]int),
identifiers: make(map[string]int),
bases: make(map[string]int),
strings: make(map[string]int),
}
}
func visible(name string) bool {
if *flagVisible {
for _, s := range strings.Split(name, string(os.PathSeparator)) {
if s != "" && s != "." && s != ".." && s[0] == '.' {
return false
}
}
}
return true
}
func isCompressed(name string) bool {
ext := filepath.Ext(name)
return ext == ".bz2" || ext == ".gz" || ext == ".zst"
}
func decompress(oldName string, oldData []byte) (newName string, newData []byte, err error) {
ext := filepath.Ext(oldName)
if (ext == ".go" && len(oldData) > 0) || (ext == ".zip") {
return oldName, oldData, nil // nothing to do
}
var oldSize int64
var encoded, decoder io.Reader
// Select source of encoded data
switch {
case len(oldData) == 0:
// Read from named file
file, err := os.Open(oldName)
if err != nil {
println(err)
return oldName, nil, err
}
defer file.Close()
info, err := file.Stat()
if err != nil {
println(err)
return oldName, nil, err
}
oldSize = info.Size()
encoded = file
default:
// Use provided data (likely reading from an archive)
oldSize = int64(len(oldData))
encoded = bytes.NewReader(oldData)
}
// Select decompression algorithm based on file extension
switch {
case ext == ".bz2":
decoder, err = bzip2.NewReader(encoded), nil
case ext == ".gz":
decoder, err = gzip.NewReader(encoded)
case ext == ".zst":
decoder, err = zstd.NewReader(encoded)
default:
decoder, err = encoded, nil // "just reading" is minimal compression
}
if err != nil {
println(err) // error creating the decoder
return oldName, nil, err
}
// Decompress the data
if newData, err = ioutil.ReadAll(decoder); err != nil {
println(err) // error using the decoder
return oldName, nil, err
}
if ext != ".go" {
// Decompress the name ("sample.go.zst" → "sample.go")
newName = strings.TrimSuffix(oldName, ext)
printf(" %8d → %8d bytes (%6.3f×) decompress and survey %s",
oldSize, len(newData), float64(len(newData))/float64(oldSize), oldName)
} else {
newName = oldName
printf(" %8d bytes survey %s", len(newData), oldName)
}
return newName, newData, nil
}
func isArchive(name string) bool {
if isCompressed(name) {
ext := filepath.Ext(name)
name = strings.TrimSuffix(name, ext) // unwrap the compression suffix
}
ext := filepath.Ext(name)
return ext == ".cpio" || ext == ".tar" || ext == ".zip"
}
func isGo(name string) bool {
if !*flagGo {
return true
}
if isCompressed(name) {
ext := filepath.Ext(name)
name = strings.TrimSuffix(name, ext) // unwrap the compression suffix
}
return filepath.Ext(name) == ".go"
}
func (s *Survey) List(name string) {
file, err := os.Open(name)
if err != nil {
println(err)
return
}
println("surveying list of files:", name)
scanner := bufio.NewScanner(file)
for scanner.Scan() {
s.File(scanner.Text())
}
file.Close()
}
func (s *Survey) File(name string) {
if !visible(name) {
return
}
info, err := os.Lstat(name)
if err != nil {
println(err)
return
}
// process plain files
if info.Mode().IsRegular() {
var err error
var data []byte
if isArchive(name) && isCompressed(name) {
s.extensions[filepath.Ext(name)]++
name, data, err = decompress(name, nil)
if err != nil {
println(err)
return
}
}
var archive io.Reader
switch {
case len(data) == 0:
f, err := os.Open(name)
if err != nil {
println(err)
return
}
defer f.Close()
archive = f
default:
archive = bytes.NewReader(data)
}
ext := strings.ToLower(filepath.Ext(name))
switch {
case ext == ".cpio":
println("processing cpio archive", name)
s.extensions[filepath.Ext(name)]++
r := cpio.NewReader(archive)
for {
hdr, err := r.Next()
if err == io.EOF {
break // End of archive
}
if err != nil {
println(err)
return
}
memberName := name + "::" + hdr.Name // "archive.cpio::file.go"
if !isGo(hdr.Name) {
println("skipping file with unrecognized extension:", memberName)
continue
}
bytes, err := ioutil.ReadAll(r)
if err != nil {
println(err)
return
}
s.Survey(memberName, bytes)
}
case ext == ".tar":
println("processing tar archive", name)
s.extensions[filepath.Ext(name)]++
tr := tar.NewReader(archive)
for {
hdr, err := tr.Next()
if err == io.EOF {
break // End of archive
}
if err != nil {
println(err)
return
}
memberName := name + "::" + hdr.Name // "archive.tar::file.go"
if !isGo(hdr.Name) {
println("skipping file with unrecognized extension:", memberName)
continue
}
bytes, err := ioutil.ReadAll(tr)
if err != nil {
println(err)
return
}
s.Survey(memberName, bytes)
}
case ext == ".zip":
println("processing zip archive:", name)
s.extensions[filepath.Ext(name)]++
r, err := zip.OpenReader(name)
if err != nil {
println(err)
return
}
defer r.Close()
for _, f := range r.File {
fullName := name + "::" + f.Name // "archive.zip::file.go"
if !isGo(f.Name) {
println("skipping file with unrecognized extension:", fullName)
continue
}
rc, err := f.Open()
if err != nil {
println(err)
return
}
bytes, err := ioutil.ReadAll(rc)
rc.Close()
if err != nil {
println(err)
return
}
s.Survey(fullName, bytes)
}
case isGo(name):
s.Survey(name, nil)
default:
println("skipping file with unrecognized extension:", name)
}
} else if info.Mode().IsDir() { // process directories
switch *flagRecursive {
case false:
// process files in this directory
println("processing Go files in directory", name)
bases, err := ioutil.ReadDir(name)
if err != nil {
println(err)
return
}
for _, base := range bases {
fullName := filepath.Join(name, base.Name())
if visible(fullName) && isGo(fullName) {
s.Survey(fullName, nil)
}
}
case true:
// process files in this directory hierarchy
println("processing Go files in and under directory", name)
walker := func(path string, info os.FileInfo, err error) error {
if err != nil {
println(err)
return err
}
name := info.Name()
if info.IsDir() {
if !visible(name) {
println("skipping hidden directory", name)
return filepath.SkipDir
}
} else {
if visible(path) && isGo(path) {
s.Survey(path, nil)
}
}
return nil
}
err = filepath.Walk(name, walker) // standard library walker
if err != nil {
println(err)
}
}
}
}
type Work struct {
name string
source []byte
}
var first bool = true
var workers int
var work chan Work
var result chan *Survey
func worker() {
s := NewSurvey()
for w := range work {
s.survey(w.name, w.source)
}
result <- s
}
func (s *Survey) Survey(name string, source []byte) {
if first {
s.start = time.Now()
if *flagCPUs != 1 {
workers = *flagCPUs
work = make(chan Work, 32*workers)
result = make(chan *Survey)
for i := 0; i < workers; i++ {
go worker()
}
}
first = false
}
switch {
case name != "": // another file to survey
switch *flagCPUs {
case 1:
s.survey(name, source) // synchronous...wait for survey to complete
default:
work <- Work{name: name, source: source} // enqueue survey request
}
case name == "": // end of survey
if *flagCPUs != 1 && workers != 0 {
close(work) // request results
for i := 0; i < workers; i++ {
s.Combine(<-result) // combine results
}
close(result)
workers = 0
}
}
}
func (s *Survey) survey(name string, source []byte) {
var err error
var newName string
if isCompressed(name) {
s.extensions[filepath.Ext(name)]++
}
newName, source, err = decompress(name, source)
if err != nil {
return
}
// if newName != name ||{
s.extensions[filepath.Ext(newName)]++
// }
lexer := &lex.Lexer{Input: source, Mode: lex.ScanGo | lex.SkipSpace}
var c [256]int // used to count operator characters to detect imbalanced () {} []
badChars := ""
expectPackageName := false
// Perform the survey by tabulating token types, subtypes, and values
for tok, text := lexer.Scan(); tok != lex.EOF; tok, text = lexer.Scan() {
s.tokens++
// go mini-parser: expect package name after "package" keyword
if expectPackageName && tok == lex.Identifier {
s.packages[string(text)]++
expectPackageName = false
continue
}
if tok == lex.Keyword && bytes.Compare(text, []byte("package")) == 0 {
expectPackageName = true // set expectations
}
switch tok {
case lex.Comment:
s.countComments[lexer.Subtype]++
case lex.String:
s.countStrings[lexer.Subtype]++
case lex.Operator:
s.operators[string(text)]++
c[text[0]]++ // count () [] {} (and every other single character)
case lex.Rune:
s.runes[string(text)]++
case lex.Identifier:
s.countIdentifiers[lexer.Subtype]++ // ASCII-only or Unicode
switch lexer.Subtype {
case lex.ASCII:
s.ascii[string(text)]++
case lex.Unicode:
s.unicode[string(text)]++
}
case lex.Number:
// note: safe because lex.Octal means len(text) >= 2 ("00"..."07" are the shortest)
if lexer.Subtype == lex.Octal && (text[1] != 'o' && text[1] != 'O') {
s.countBases[5]++
} else {
s.countBases[lexer.Subtype]++
}
case lex.Keyword:
s.keywords[string(text)]++
case lex.Type:
s.types[string(text)]++
case lex.Other:
s.others[string(text)]++
case lex.Character:
badChars += string(text) // only happens if go code won't compile because junk characters in file
}
}
s.files++
s.lines += bytes.Count(source, []byte{'\n'})
s.bytes += len(source)
good := true
if c['('] != c[')'] || c['['] != c[']'] || c['{'] != c['}'] { // counts match except in compiler failure tests
name += fmt.Sprintf(" «balance (%d:%d) [%d:%d] {%d:%d}»", c['('], c[')'], c['['], c[']'], c['{'], c['}'])
good = false
}
if badChars != "" {
name += fmt.Sprintf(" «unrecognized %q»", badChars)
good = false
}
switch good {
case true:
s.good = append(s.good, name)
case false:
s.bad = append(s.bad, name)
}
}
func (s *Survey) Combine(c *Survey) {
s.elapsed += c.elapsed
s.files += c.files
s.lines += c.lines
s.tokens += c.tokens
s.bytes += c.bytes
s.good = append(s.good, c.good...)
s.bad = append(s.bad, c.bad...)
// go2 dream: s.packages += c.packages
combineMap(s.extensions, c.extensions)
combineMap(s.packages, c.packages)
combineMap(s.operators, c.operators)
combineMap(s.runes, c.runes)
combineMap(s.ascii, c.ascii)
combineMap(s.unicode, c.unicode)
combineMap(s.keywords, c.keywords)
combineMap(s.types, c.types)
combineMap(s.others, c.others)
combineMap(s.comments, c.comments)
combineMap(s.identifiers, c.identifiers)
combineMap(s.bases, c.bases)
combineMap(s.strings, c.strings)
// go2 dream: s.countComments += c.countComments
for i, v := range c.countComments {
s.countComments[i] += v
}
for i, v := range c.countIdentifiers {
s.countIdentifiers[i] += v
}
for i, v := range c.countStrings {
s.countStrings[i] += v
}
for i, v := range c.countBases {
s.countBases[i] += v
}
}
func combineMap(s, c map[string]int) {
for k, v := range c {
s[k] += v
}
}
// Complete a survey
func (s *Survey) Complete() {
// Completion is a one-time operation. if already done, it must not be done again.
if s.complete {
return
}
// Signal end of survey, await asynchronous completion, and combine all results.
s.Survey("", nil)
// Populate map-from-counts as needed
s.comments[`general (/*…*/)`] += s.countComments[1]
s.comments[`line (//…'\n')`] += s.countComments[2]
s.identifiers[`ASCII-only`] += s.countIdentifiers[1]
s.identifiers[`Unicode`] += s.countIdentifiers[2]
s.bases[`prefixed binary (/0[bB][0-1]+/)`] += s.countBases[1]
s.bases[`prefixed octal (/0[oO][0-7]+/)`] += s.countBases[2]
s.bases[`decimal (/0|([1-9][0-9]*)/)`] += s.countBases[3]
s.bases[`prefixed hexadecimal (/0[xX][0-9a-fA-F]+/)`] += s.countBases[4]
s.bases[`legacy octal (/0[0-7]+/)`] += s.countBases[5]
s.strings[`quoted ("…")`] += s.countStrings[1]
s.strings["raw (`…`)"] += s.countStrings[2]
// remove package name references from the general identifier list
for p := range s.packages {
delete(s.ascii, p)
delete(s.unicode, p)
}
// stop the timer: don't charge future logging and reporting against survey speed
s.elapsed = time.Since(s.start).Seconds()
s.user, s.system, s.size = getResourceUsage()
var exts []string
for key := range s.extensions {
exts = append(exts, `"`+key+`"`)
}
sort.Strings(exts)
if len(exts) > 0 {
println("filetypes processed:", strings.Join(exts, ", "))
}
sort.Strings(s.good)
sort.Strings(s.bad)
if *flagVerbose {
// if len(s.good) > 0 {
// detail("")
// detail("files that passed the Go lexical scan:")
// for _, v := range s.good {
// detail(" good", v)
// }
// }
if len(s.bad) > 0 {
println("")
println("files that failed the Go lexical scan:")
for _, v := range s.bad {
println(" bad", v)
}
}
}
// detail("survey complete")
println("processed", len(s.good), "good and", len(s.bad), "bad files in", s.elapsed, "seconds")
s.complete = true
}
func (s *Survey) Report() {
// complete survey (if not already the case)
if !s.complete {
s.Complete()
}
file := os.Stdout
switch lower := strings.ToLower(*flagOutput); {
case lower == "[stdout]":
file = os.Stdout
case lower == "[stderr]":
file = os.Stderr
case lower != "":
var err error
file, err = os.Create(*flagOutput)
if err != nil {
println(err)
return
}
defer file.Close()
}
s.reportProcessing(file, "Processing summary")
if s.files == 0 || s.lines == 0 || s.bytes == 0 {
return
}
if *flagVerbose && len(s.bad) > 0 {
s.reportImbalance(file, "Problem files")
}
// usage of language features (0 means show all values)
reportSurvey(file, "Comment style popularity", s.comments, 0)
reportSurvey(file, "String style popularity", s.strings, 0)
reportSurvey(file, "Numeric base popularity", s.bases, 0)
reportSurvey(file, "Reserved keyword popularity", s.keywords, 0)
reportSurvey(file, "Predefined type popularity", s.types, 0)
reportSurvey(file, "Other predefined popularity", s.others, 0)
reportSurvey(file, "Operator popularity", s.operators, 0)
reportSurvey(file, "Identifier subtype", s.identifiers, 0)
// developer chosen symbols (report limited to no more than *flagLines lines)
reportSurvey(file, "Package name popularity", s.packages, *flagLines)
reportSurvey(file, "ASCII Identifier popularity", s.ascii, *flagLines)
reportSurvey(file, "Unicode Identifier popularity", s.unicode, *flagLines)
reportSurvey(file, "Rune constant popularity", s.runes, *flagLines)
}
func (s *Survey) reportProcessing(file *os.File, title string) {
if *flagStyle == "markdown" {
fmt.Fprintf(file, "\n")
fmt.Fprintf(file, "# Go survey \n")
fmt.Fprintf(file, "\n")
fmt.Fprintf(file, "## *%s* \n", title)
fmt.Fprintf(file, "\n")
fmt.Fprintf(file, "Count | Units | Detail \n")
fmt.Fprintf(file, "---:|---|--- \n")
fmt.Fprintf(file, "%d | file%s | source code groups \n", s.files, plural(s.files, ""))
fmt.Fprintf(file, "%d | file%s | original source code files \n", s.keywords["package"], plural(s.keywords["package"], ""))
fmt.Fprintf(file, "%d | %s | %.0f per sec \n", s.lines, "lines", float64(s.lines)/s.elapsed)
fmt.Fprintf(file, "%d | %s | %.0f per sec \n", s.tokens, "tokens", float64(s.tokens)/s.elapsed)
fmt.Fprintf(file, "%d | %s | %.0f per sec \n", s.bytes, "bytes", float64(s.bytes)/s.elapsed)
fmt.Fprintf(file, "%.6f | seconds | read/lex/tabulate/sort/select \n", s.elapsed)
if s.elapsed > 0 && s.bytes != 0 && *flagCPUs > 1 {
fmt.Fprintf(file, "%d | worker%s | (parallel speedup = %.2f x with SMT) \n",
*flagCPUs, plural(*flagCPUs, ""), (s.user+s.system)/s.elapsed)
}
} else {
fmt.Fprintf(file, " %s %s %s\n", strings.Repeat("━", 70-len(title)-6), title, strings.Repeat("━", 2))
fmt.Fprintf(file, " %10d %-7s (%s)\n", s.files,
fmt.Sprintf("file%s", plural(s.files, "")),
"source code groups")
fmt.Fprintf(file, " %10d %-7s (%s)\n", s.keywords["package"],
fmt.Sprintf("file%s", plural(s.keywords["package"], "")),
"original source code files")
fmt.Fprintf(file, " %10d %-7s (%10.0f per sec)\n", s.lines, "lines", float64(s.lines)/s.elapsed)
fmt.Fprintf(file, " %10d %-7s (%10.0f per sec)\n", s.tokens, "tokens", float64(s.tokens)/s.elapsed)
fmt.Fprintf(file, " %10d %-7s (%10.0f per sec)\n", s.bytes, "bytes", float64(s.bytes)/s.elapsed)
fmt.Fprintf(file, " %10.6f seconds to read/lex/tabulate/sort/adjust \n", s.elapsed)
if s.elapsed > 0 && s.bytes != 0 && *flagCPUs > 1 {
fmt.Fprintf(file, " %10d worker%s (parallel speedup = %.2f x with SMT)\n",
*flagCPUs, plural(*flagCPUs, ""), (s.user+s.system)/s.elapsed)
}
fmt.Fprintf(file, " %s\n", strings.Repeat("─", 70))
fmt.Fprintf(file, "\n")
}
}
func (s *Survey) reportImbalance(file *os.File, title string) {
if len(s.bad) == 0 {
return
}
if *flagStyle == "markdown" {
fmt.Fprintf(file, "\n")
fmt.Fprintf(file, "## *%s* \n", title)
fmt.Fprintf(file, "\n")
fmt.Fprintf(file, "Problem | File \n")
fmt.Fprintf(file, "---:|--- \n")
for _, v := range s.bad {
part := strings.SplitN(v, " ", 2)
part[1] = strings.TrimLeft(part[1], "«")
part[1] = strings.TrimRight(part[1], "»")
t := strings.SplitN(part[1], " ", 2)
fmt.Fprintf(file, "%s | %s \n", t[1], part[0])
}
} else {
fmt.Fprintf(file, " %s %s %s\n", strings.Repeat("━", 70-len(title)-6), title, strings.Repeat("━", 2))
for _, v := range s.bad {
part := strings.SplitN(v, " ", 2)
part[1] = strings.TrimLeft(part[1], "«")
part[1] = strings.TrimRight(part[1], "»")
t := strings.SplitN(part[1], " ", 2)
fmt.Fprintf(file, " %20s %s \n", t[1], part[0])
}
fmt.Fprintf(file, " %s\n", strings.Repeat("─", 70))
fmt.Fprintf(file, "\n")
}
}
func reportSurvey(file *os.File, title string, m map[string]int, n int) {
// skip empty surveys
if len(m) == 0 {
return
}
type Pair struct {
n int
s string
}
// order data by usage
p := make([]Pair, len(m))
i := 0
t := 0
for s, n := range m {
p[i] = Pair{n: n, s: s}
i++
t += n
}
sort.Slice(p, func(i int, j int) bool {
if p[i].n != p[j].n {
return p[i].n > p[j].n
}
return p[i].s < p[j].s
})
if *flagStyle == "markdown" {
fmt.Fprintf(file, "\n")
fmt.Fprintf(file, "## *%s* \n", title)
fmt.Fprintf(file, "\n")
fmt.Fprintf(file, "Count | Frequency | Detail\n")
fmt.Fprintf(file, "---:|---:|---\n")
unique := 0
subtotal := 0
for i, v := range p {
if v.n == 0 {
continue
}
if n == 0 || i < n {
escaped := v.s
escaped = strings.ReplaceAll(escaped, "|", "|") // protect '|'
escaped = strings.ReplaceAll(escaped, "`", "`") // protect '`'
fmt.Fprintf(file, " %d | %.4f%% | %s \n", v.n, (100*float64(v.n))/float64(t), escaped)
} else {
unique++
subtotal += v.n
}
}
if subtotal > 0 {
fmt.Fprintf(file, " %d | %.4f%% | (%d more with %d unique values) \n", subtotal, (100*float64(subtotal))/float64(t), subtotal, unique)
}
fmt.Fprintf(file, " %d | %.4f%% | %s \n", t, 100.0, "total")
} else {
fmt.Fprintf(file, " %s %s %s\n", strings.Repeat("━", 70-len(title)-6), title, strings.Repeat("━", 2))
fmt.Fprintf(file, " %9s %9s %s\n", "Count", "Percent", "Token subtype")
fmt.Fprintf(file, " %s\n", strings.Repeat("─", 70))
unique := 0
subtotal := 0
for i, v := range p {
if v.n == 0 {
continue
}
if n == 0 || i < n {
fmt.Fprintf(file, " %9d %8.4f%% %s\n", v.n, (100*float64(v.n))/float64(t), v.s)
} else {
unique++
subtotal += v.n
}
}
if subtotal > 0 {
fmt.Fprintf(file, " %9d %8.4f%% (%d more with %d unique values)\n", subtotal, (100*float64(subtotal))/float64(t), subtotal, unique)
}
fmt.Fprintf(file, " %s\n", strings.Repeat("─", 70))
fmt.Fprintf(file, " %9d %8.4f%% %s\n", t, 100.0, "total")
fmt.Fprintf(file, "\n\n")
}
}