aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorRuss Cox <rsc@golang.org>2009-10-06 15:28:47 -0700
committerRuss Cox <rsc@golang.org>2009-10-06 15:28:47 -0700
commit2c2e2c5d5582225632be487dba4ede8761374294 (patch)
tree7b454e7032609574c80c0e940c03c16b929c465a /src
parentdc093494f38deb14e3a8818972b8248811787272 (diff)
downloadgo-2c2e2c5d5582225632be487dba4ede8761374294.tar.xz
more comment work.
got rid of regexps. primary bug fix is that // inside /* */ do not get stripped anymore, so that the text inside /* int a; // int b; int c; */ is int a; // int b; int c; before, the "int b;" line was being uncommented too. R=gri DELTA=65 (13 added, 42 deleted, 10 changed) OCL=35334 CL=35404
Diffstat (limited to 'src')
-rw-r--r--src/pkg/go/doc/comment.go71
1 files changed, 21 insertions, 50 deletions
diff --git a/src/pkg/go/doc/comment.go b/src/pkg/go/doc/comment.go
index ea361f851f..d83857c0c4 100644
--- a/src/pkg/go/doc/comment.go
+++ b/src/pkg/go/doc/comment.go
@@ -9,36 +9,12 @@ package doc
import (
"go/ast";
"io";
- "once";
- "regexp";
"strings";
"template"; // for htmlEscape
)
// Comment extraction
-var (
- comment_markers *regexp.Regexp;
- trailing_whitespace *regexp.Regexp;
- comment_junk *regexp.Regexp;
-)
-
-func makeRex(s string) *regexp.Regexp {
- re, err := regexp.Compile(s);
- if err != nil {
- panic("MakeRegexp ", s, " ", err.String());
- }
- return re;
-}
-
-// TODO(rsc): Cannot use var initialization for regexps,
-// because Regexp constructor needs threads.
-func setupRegexps() {
- comment_markers = makeRex("^/[/*] ?");
- trailing_whitespace = makeRex("[ \t\r]+$");
- comment_junk = makeRex("^[ \t]*(/\\*|\\*/)[ \t]*$");
-}
-
// CommentText returns the text of comment,
// with the comment markers - //, /*, and */ - removed.
func CommentText(comment *ast.CommentGroup) string {
@@ -50,39 +26,34 @@ func CommentText(comment *ast.CommentGroup) string {
comments[i] = string(c.Text);
}
- once.Do(setupRegexps);
lines := make([]string, 0, 20);
for _, c := range comments {
- // split on newlines
- cl := strings.Split(c, "\n", 0);
-
- // walk lines, stripping comment markers
- w := 0;
- for _, l := range cl {
- // remove /* and */ lines
- if comment_junk.MatchString(l) {
- continue;
+ // Remove comment markers.
+ // The parser has given us exactly the comment text.
+ switch n := len(c); {
+ case n >= 4 && c[0:2] == "/*" && c[n-2:n] == "*/":
+ c = c[2:n-2];
+ case n >= 2 && c[0:2] == "//":
+ c = c[2:n];
+ // Remove leading space after //, if there is one.
+ if len(c) > 0 && c[0] == ' ' {
+ c = c[1:len(c)];
}
+ }
- // strip trailing white space
- m := trailing_whitespace.ExecuteString(l);
- if len(m) > 0 {
- l = l[0 : m[1]];
- }
+ // Split on newlines.
+ cl := strings.Split(c, "\n", 0);
- // strip leading comment markers
- m = comment_markers.ExecuteString(l);
- if len(m) > 0 {
- l = l[m[1] : len(l)];
+ // Walk lines, stripping trailing white space and adding to list.
+ for _, l := range cl {
+ // Strip trailing white space
+ m := len(l);
+ for m > 0 && (l[m-1] == ' ' || l[m-1] == '\n' || l[m-1] == '\t' || l[m-1] == '\r') {
+ m--;
}
+ l = l[0 : m];
- cl[w] = l;
- w++;
- }
- cl = cl[0:w];
-
- // Add this comment to total list.
- for _, l := range cl {
+ // Add to list.
n := len(lines);
if n+1 >= cap(lines) {
newlines := make([]string, n, 2*cap(lines));