Skip to content

Instantly share code, notes, and snippets.

@crhntr
Created May 30, 2016 19:18
Show Gist options
  • Save crhntr/be692f49e3977f4005427a2b564034bc to your computer and use it in GitHub Desktop.
Save crhntr/be692f49e3977f4005427a2b564034bc to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"go/scanner"
"go/token"
"math"
"sync"
"time"
)
func main() {
fmt.Println("START | END | PASSES | FAILS | TOTAL | TIME ")
var wg sync.WaitGroup
h := 0
limit := int(math.Pow(2, 16))
size := limit / int(math.Pow(2, 10))
for ; h+size <= limit; h += size {
wg.Add(1)
go func(a, b int) {
defer wg.Done()
passCount, failCount := 0, 0
var r rune
r += rune(a)
//ri := r
var i int
start := time.Now()
var fset *token.FileSet
var s scanner.Scanner
var src string
var file *token.File
for i = a; i < b; i++ {
src = fmt.Sprintf("%c := 0\n", r)
fset = token.NewFileSet()
file = fset.AddFile("", fset.Base(), len(src))
s.Init(file, []byte(src), nil /* no error handler */, scanner.ScanComments)
for {
_, tok, _ := s.Scan()
if tok == token.EOF {
break
}
if tok == token.ILLEGAL {
failCount++
} else if tok == token.IDENT {
passCount++
}
}
r++
}
fmt.Printf(" %8d | %8d | %8d | %8d | %8d | %s \n", a, b, passCount, failCount, b-a, time.Since(start))
}(h, h+size-1)
}
wg.Wait()
}
@crhntr
Copy link
Author

crhntr commented May 30, 2016

I wrote this after finding that some Unicode characters did not work as valid character tokens. I wanted to use U+2203, the Existential 'THERE EXISTS' character in a private method and go complained. Also I used it to practice using goroutines.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment