/
strings.go
72 lines (64 loc) · 1.76 KB
/
strings.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package goline
/*
* Filename: strings.go
* Package: goline
* Author: Bryan Matsuo <bmatsuo@soe.ucsc.edu>
* Created: Wed Aug 24 22:56:19 PDT 2011
* Description:
*/
import (
"reflect"
"strings"
)
// Returns the index i of the longest terminal substring s[i:] such that f
// returns true for all runes in s[i:]. Returns -1 if there is no such i.
func stringSuffixIndexFunc(s string, f func(c rune) bool) (i int) {
var hasSuffix bool
i = strings.LastIndexFunc(s, func(c rune) (done bool) {
if done = !f(c); !hasSuffix {
hasSuffix = !done
}
return
})
if i++; !hasSuffix {
i = -1
}
return
}
// Return the suffix string corresponding to the same call to
// stringSuffixIndexFunc.
func stringSuffixFunc(s string, f func(c rune) bool) (suff string) {
if i := stringSuffixIndexFunc(s, f); i >= 0 {
suff = s[i:]
}
return
}
// A string type that implements Stringer.
type simpleString string
// The empty string.
var zeroSimpleString simpleString
// Simply recast the simpleString.
func (s simpleString) String() string { return string(s) }
// An interface that Menu items must implement..
type Stringer interface {
String() string
}
var (
// The zero Stringer value.
zeroStringer Stringer
// The Stringer reflect type.
typeStringer = reflect.TypeOf(Stringer(zeroSimpleString))
)
// Return a Stringer object given either a string or an object that implements
// Stringer.
func makeStringer(s interface{}) Stringer {
switch s.(type) {
case string:
return simpleString(s.(string))
case Stringer:
return s.(Stringer)
default:
panic("Value must be type 'string' or 'Stringer'")
}
return zeroStringer
}