Source file src/pkg/go/doc/synopsis.go
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.
4
5 package doc
6
7 import "unicode"
8
9 // firstSentenceLen returns the length of the first sentence in s.
10 // The sentence ends after the first period followed by space and
11 // not preceded by exactly one uppercase letter.
12 //
13 func firstSentenceLen(s string) int {
14 var ppp, pp, p rune
15 for i, q := range s {
16 if q == '\n' || q == '\r' || q == '\t' {
17 q = ' '
18 }
19 if q == ' ' && p == '.' && (!unicode.IsUpper(pp) || unicode.IsUpper(ppp)) {
20 return i
21 }
22 ppp, pp, p = pp, p, q
23 }
24 return len(s)
25 }
26
27 // Synopsis returns a cleaned version of the first sentence in s.
28 // That sentence ends after the first period followed by space and
29 // not preceded by exactly one uppercase letter. The result string
30 // has no \n, \r, or \t characters and uses only single spaces between
31 // words.
32 //
33 func Synopsis(s string) string {
34 n := firstSentenceLen(s)
35 var b []byte
36 p := byte(' ')
37 for i := 0; i < n; i++ {
38 q := s[i]
39 if q == '\n' || q == '\r' || q == '\t' {
40 q = ' '
41 }
42 if q != ' ' || p != ' ' {
43 b = append(b, q)
44 p = q
45 }
46 }
47 // remove trailing blank, if any
48 if n := len(b); n > 0 && p == ' ' {
49 b = b[0 : n-1]
50 }
51 return string(b)
52 }