Reverting merge from trunk
[official-gcc.git] / libgo / go / go / doc / synopsis.go
blobd1ad86c74166edf238ec798f3140373eb4b4a277
1 // Copyright 2012 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 package doc
7 import (
8 "strings"
9 "unicode"
12 // firstSentenceLen returns the length of the first sentence in s.
13 // The sentence ends after the first period followed by space and
14 // not preceded by exactly one uppercase letter.
16 func firstSentenceLen(s string) int {
17 var ppp, pp, p rune
18 for i, q := range s {
19 if q == '\n' || q == '\r' || q == '\t' {
20 q = ' '
22 if q == ' ' && p == '.' && (!unicode.IsUpper(pp) || unicode.IsUpper(ppp)) {
23 return i
25 ppp, pp, p = pp, p, q
27 return len(s)
30 const (
31 keepNL = 1 << iota
34 // clean replaces each sequence of space, \n, \r, or \t characters
35 // with a single space and removes any trailing and leading spaces.
36 // If the keepNL flag is set, newline characters are passed through
37 // instead of being change to spaces.
38 func clean(s string, flags int) string {
39 var b []byte
40 p := byte(' ')
41 for i := 0; i < len(s); i++ {
42 q := s[i]
43 if (flags&keepNL) == 0 && q == '\n' || q == '\r' || q == '\t' {
44 q = ' '
46 if q != ' ' || p != ' ' {
47 b = append(b, q)
48 p = q
51 // remove trailing blank, if any
52 if n := len(b); n > 0 && p == ' ' {
53 b = b[0 : n-1]
55 return string(b)
58 // Synopsis returns a cleaned version of the first sentence in s.
59 // That sentence ends after the first period followed by space and
60 // not preceded by exactly one uppercase letter. The result string
61 // has no \n, \r, or \t characters and uses only single spaces between
62 // words. If s starts with any of the IllegalPrefixes, the result
63 // is the empty string.
65 func Synopsis(s string) string {
66 s = clean(s[0:firstSentenceLen(s)], 0)
67 for _, prefix := range IllegalPrefixes {
68 if strings.HasPrefix(strings.ToLower(s), prefix) {
69 return ""
72 return s
75 var IllegalPrefixes = []string{
76 "copyright",
77 "all rights",
78 "author",