Commit a8246510 authored by Rob Pike's avatar Rob Pike

use the new routine regexp.MustCompile to clean up some code that uses global regexps.

R=rsc, gri
CC=go-dev
http://go/go-review/1016025
parent 9c132158
...@@ -245,28 +245,15 @@ func copyCommentList(list []*ast.Comment) []*ast.Comment { ...@@ -245,28 +245,15 @@ func copyCommentList(list []*ast.Comment) []*ast.Comment {
var ( var (
// Regexp constructor needs threads - cannot use init expressions bug_markers = regexp.MustCompile("^/[/*][ \t]*BUG\\(.*\\):[ \t]*"); // BUG(uid):
bug_markers *regexp.Regexp; bug_content = regexp.MustCompile("[^ \n\r\t]+"); // at least one non-whitespace char
bug_content *regexp.Regexp;
) )
func makeRex(s string) *regexp.Regexp {
re, err := regexp.Compile(s);
if err != nil {
panic("MakeRegexp ", s, " ", err.String());
}
return re;
}
// addFile adds the AST for a source file to the docReader. // addFile adds the AST for a source file to the docReader.
// Adding the same AST multiple times is a no-op. // Adding the same AST multiple times is a no-op.
// //
func (doc *docReader) addFile(src *ast.File) { func (doc *docReader) addFile(src *ast.File) {
if bug_markers == nil {
bug_markers = makeRex("^/[/*][ \t]*BUG\\(.*\\):[ \t]*"); // BUG(uid):
bug_content = makeRex("[^ \n\r\t]+"); // at least one non-whitespace char
}
// add package documentation // add package documentation
if src.Doc != nil { if src.Doc != nil {
// TODO(gri) This won't do the right thing if there is more // TODO(gri) This won't do the right thing if there is more
......
...@@ -36,22 +36,22 @@ var url = flag.String("url", ...@@ -36,22 +36,22 @@ var url = flag.String("url",
"URL of Unicode database directory") "URL of Unicode database directory")
var tablelist = flag.String("tables", var tablelist = flag.String("tables",
"all", "all",
"comma-separated list of which tables to generate; can be letter"); "comma-separated list of which tables to generate; can be letter")
var scriptlist = flag.String("scripts", var scriptlist = flag.String("scripts",
"all", "all",
"comma-separated list of which script tables to generate"); "comma-separated list of which script tables to generate")
var proplist = flag.String("props", var proplist = flag.String("props",
"all", "all",
"comma-separated list of which property tables to generate"); "comma-separated list of which property tables to generate")
var cases = flag.Bool("cases", var cases = flag.Bool("cases",
true, true,
"generate case tables"); "generate case tables")
var test = flag.Bool("test", var test = flag.Bool("test",
false, false,
"test existing tables; can be used to compare web data with package data"); "test existing tables; can be used to compare web data with package data")
var scriptRe *regexp.Regexp
var die = log.New(os.Stderr, nil, "", log.Lexit|log.Lshortfile); var scriptRe = regexp.MustCompile(`([0-9A-F]+)(\.\.[0-9A-F]+)? *; ([A-Za-z_]+)`)
var die = log.New(os.Stderr, nil, "", log.Lexit|log.Lshortfile)
var category = map[string] bool{ "letter":true } // Nd Lu etc. letter is a special case var category = map[string] bool{ "letter":true } // Nd Lu etc. letter is a special case
...@@ -125,8 +125,6 @@ var props = make(map[string] []Script) // a property looks like a script; can sh ...@@ -125,8 +125,6 @@ var props = make(map[string] []Script) // a property looks like a script; can sh
var lastChar uint32 = 0 var lastChar uint32 = 0
const scriptParseExpression = `([0-9A-F]+)(\.\.[0-9A-F]+)? *; ([A-Za-z_]+)`
// In UnicodeData.txt, some ranges are marked like this: // In UnicodeData.txt, some ranges are marked like this:
// 3400;<CJK Ideograph Extension A, First>;Lo;0;L;;;;;N;;;;; // 3400;<CJK Ideograph Extension A, First>;Lo;0;L;;;;;N;;;;;
// 4DB5;<CJK Ideograph Extension A, Last>;Lo;0;L;;;;;N;;;;; // 4DB5;<CJK Ideograph Extension A, Last>;Lo;0;L;;;;;N;;;;;
...@@ -166,7 +164,7 @@ func parseCategory(line string) (state State) { ...@@ -166,7 +164,7 @@ func parseCategory(line string) (state State) {
switch char.category { switch char.category {
case "Nd": case "Nd":
// Decimal digit // Decimal digit
v, err := strconv.Atoi(field[FNumericValue]); _, err := strconv.Atoi(field[FNumericValue]);
if err != nil { if err != nil {
die.Log("U+%04x: bad numeric field: %s", point, err); die.Log("U+%04x: bad numeric field: %s", point, err);
} }
...@@ -460,7 +458,7 @@ func fullCategoryTest(list []string) { ...@@ -460,7 +458,7 @@ func fullCategoryTest(list []string) {
} }
func verifyRange(name string, inCategory Op, table []unicode.Range) { func verifyRange(name string, inCategory Op, table []unicode.Range) {
for i, c := range chars { for i := range chars {
web := inCategory(i); web := inCategory(i);
pkg := unicode.Is(table, i); pkg := unicode.Is(table, i);
if web != pkg { if web != pkg {
...@@ -499,7 +497,7 @@ func parseScript(line string, scripts map[string] []Script) { ...@@ -499,7 +497,7 @@ func parseScript(line string, scripts map[string] []Script) {
} }
name := matches[3]; name := matches[3];
s, ok := scripts[name]; s, ok := scripts[name];
if len(s) == cap(s) { if !ok || len(s) == cap(s) {
ns := make([]Script, len(s), len(s)+100); ns := make([]Script, len(s), len(s)+100);
for i, sc := range s { for i, sc := range s {
ns[i] = sc ns[i] = sc
...@@ -532,7 +530,7 @@ func fullScriptTest(list []string, installed map[string] []unicode.Range, script ...@@ -532,7 +530,7 @@ func fullScriptTest(list []string, installed map[string] []unicode.Range, script
if _, ok := scripts[name]; !ok { if _, ok := scripts[name]; !ok {
die.Log("unknown script", name); die.Log("unknown script", name);
} }
r, ok := installed[name]; _, ok := installed[name];
if !ok { if !ok {
die.Log("unknown table", name); die.Log("unknown table", name);
} }
...@@ -564,10 +562,6 @@ func printScriptOrProperty(doProps bool) { ...@@ -564,10 +562,6 @@ func printScriptOrProperty(doProps bool) {
return return
} }
var err os.Error; var err os.Error;
scriptRe, err = regexp.Compile(scriptParseExpression);
if err != nil {
die.Log("re error:", err)
}
resp, _, err := http.Get(*url + file); resp, _, err := http.Get(*url + file);
if err != nil { if err != nil {
die.Log(err); die.Log(err);
...@@ -801,7 +795,7 @@ func printCases() { ...@@ -801,7 +795,7 @@ func printCases() {
var startState *caseState; // the start of a run; nil for not active var startState *caseState; // the start of a run; nil for not active
var prevState = &caseState{}; // the state of the previous character var prevState = &caseState{}; // the state of the previous character
for i, c := range chars { for i := range chars {
state := getCaseState(i); state := getCaseState(i);
if state.adjacent(prevState) { if state.adjacent(prevState) {
prevState = state; prevState = state;
......
...@@ -43,15 +43,6 @@ import ( ...@@ -43,15 +43,6 @@ import (
"strings"; "strings";
) )
func compile(s string) *regexp.Regexp {
r, err := regexp.Compile(s);
if err != nil {
fmt.Fprintf(os.Stderr, "can't compile pattern %q: %s\n", s, err);
os.Exit(2);
}
return r;
}
var variants = []string { var variants = []string {
"agggtaaa|tttaccct", "agggtaaa|tttaccct",
"[cgt]gggtaaa|tttaccc[acg]", "[cgt]gggtaaa|tttaccc[acg]",
...@@ -83,7 +74,7 @@ var substs = [] Subst { ...@@ -83,7 +74,7 @@ var substs = [] Subst {
} }
func countMatches(pat string, bytes []byte) int { func countMatches(pat string, bytes []byte) int {
re := compile(pat); re := regexp.MustCompile(pat);
n := 0; n := 0;
for { for {
e := re.Execute(bytes); e := re.Execute(bytes);
...@@ -104,13 +95,13 @@ func main() { ...@@ -104,13 +95,13 @@ func main() {
} }
ilen := len(bytes); ilen := len(bytes);
// Delete the comment lines and newlines // Delete the comment lines and newlines
bytes = compile("(>[^\n]+)?\n").ReplaceAll(bytes, []byte{}); bytes = regexp.MustCompile("(>[^\n]+)?\n").ReplaceAll(bytes, []byte{});
clen := len(bytes); clen := len(bytes);
for _, s := range variants { for _, s := range variants {
fmt.Printf("%s %d\n", s, countMatches(s, bytes)); fmt.Printf("%s %d\n", s, countMatches(s, bytes));
} }
for _, sub := range substs { for _, sub := range substs {
bytes = compile(sub.pat).ReplaceAll(bytes, strings.Bytes(sub.repl)); bytes = regexp.MustCompile(sub.pat).ReplaceAll(bytes, strings.Bytes(sub.repl));
} }
fmt.Printf("\n%d\n%d\n%d\n", ilen, clen, len(bytes)); fmt.Printf("\n%d\n%d\n%d\n", ilen, clen, len(bytes));
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment