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.
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 {
19 if q
== '\n' || q
== '\r' || q
== '\t' {
22 if q
== ' ' && p
== '.' && (!unicode
.IsUpper(pp
) || unicode
.IsUpper(ppp
)) {
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 {
41 for i
:= 0; i
< len(s
); i
++ {
43 if (flags
&keepNL
) == 0 && q
== '\n' || q
== '\r' || q
== '\t' {
46 if q
!= ' ' || p
!= ' ' {
51 // remove trailing blank, if any
52 if n
:= len(b
); n
> 0 && p
== ' ' {
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
) {
75 var IllegalPrefixes
= []string{