Commit 92ac0cfb authored by Michael Hoisie's avatar Michael Hoisie Committed by Russ Cox

strings: add IndexRune, Trim, TrimLeft, TrimRight, and the generic equivalents…

strings: add IndexRune, Trim, TrimLeft, TrimRight, and the generic equivalents TrimFunc, TrimLeftFunc, TrimRightFunc

R=rsc, r
CC=golang-dev
https://golang.org/cl/799048
parent 63635426
...@@ -106,6 +106,17 @@ func LastIndex(s, sep string) int { ...@@ -106,6 +106,17 @@ func LastIndex(s, sep string) int {
return -1 return -1
} }
// IndexRune returns the index of the first instance of the Unicode code point
// rune, or -1 if rune is not present in s.
func IndexRune(s string, rune int) int {
for i, c := range s {
if c == rune {
return i
}
}
return -1
}
// IndexAny returns the index of the first instance of any Unicode code point // IndexAny returns the index of the first instance of any Unicode code point
// from chars in s, or -1 if no Unicode code point from chars is present in s. // from chars in s, or -1 if no Unicode code point from chars is present in s.
func IndexAny(s, chars string) int { func IndexAny(s, chars string) int {
...@@ -309,9 +320,9 @@ func ToTitleSpecial(_case unicode.SpecialCase, s string) string { ...@@ -309,9 +320,9 @@ func ToTitleSpecial(_case unicode.SpecialCase, s string) string {
return Map(func(r int) int { return _case.ToTitle(r) }, s) return Map(func(r int) int { return _case.ToTitle(r) }, s)
} }
// Trim returns a slice of the string s, with all leading and trailing white space // TrimLeftFunc returns a slice of the string s with all leading
// removed, as defined by Unicode. // Unicode code points c satisfying f(c) removed.
func TrimSpace(s string) string { func TrimLeftFunc(s string, f func(r int) bool) string {
start, end := 0, len(s) start, end := 0, len(s)
for start < end { for start < end {
wid := 1 wid := 1
...@@ -319,16 +330,23 @@ func TrimSpace(s string) string { ...@@ -319,16 +330,23 @@ func TrimSpace(s string) string {
if rune >= utf8.RuneSelf { if rune >= utf8.RuneSelf {
rune, wid = utf8.DecodeRuneInString(s[start:end]) rune, wid = utf8.DecodeRuneInString(s[start:end])
} }
if !unicode.IsSpace(rune) { if !f(rune) {
break return s[start:]
} }
start += wid start += wid
} }
return s[start:]
}
// TrimRightFunc returns a slice of the string s with all trailing
// Unicode code points c satisfying f(c) removed.
func TrimRightFunc(s string, f func(r int) bool) string {
start, end := 0, len(s)
for start < end { for start < end {
wid := 1 wid := 1
rune := int(s[end-1]) rune := int(s[end-wid])
if rune >= utf8.RuneSelf { if rune >= utf8.RuneSelf {
// Back up carefully looking for beginning of rune. Mustn't pass start. // Back up & look for beginning of rune. Mustn't pass start.
for wid = 2; start <= end-wid && !utf8.RuneStart(s[end-wid]); wid++ { for wid = 2; start <= end-wid && !utf8.RuneStart(s[end-wid]); wid++ {
} }
if start > end-wid { // invalid UTF-8 sequence; stop processing if start > end-wid { // invalid UTF-8 sequence; stop processing
...@@ -336,10 +354,53 @@ func TrimSpace(s string) string { ...@@ -336,10 +354,53 @@ func TrimSpace(s string) string {
} }
rune, wid = utf8.DecodeRuneInString(s[end-wid : end]) rune, wid = utf8.DecodeRuneInString(s[end-wid : end])
} }
if !unicode.IsSpace(rune) { if !f(rune) {
break return s[0:end]
} }
end -= wid end -= wid
} }
return s[start:end] return s[0:end]
}
// TrimFunc returns a slice of the string s with all leading
// and trailing Unicode code points c satisfying f(c) removed.
func TrimFunc(s string, f func(r int) bool) string {
return TrimRightFunc(TrimLeftFunc(s, f), f)
}
func makeCutsetFunc(cutset string) func(rune int) bool {
return func(rune int) bool { return IndexRune(cutset, rune) != -1 }
}
// Trim returns a slice of the string s with all leading and
// trailing Unicode code points contained in cutset removed.
func Trim(s string, cutset string) string {
if s == "" || cutset == "" {
return s
}
return TrimFunc(s, makeCutsetFunc(cutset))
}
// TrimLeft returns a slice of the string s with all leading
// Unicode code points contained in cutset removed.
func TrimLeft(s string, cutset string) string {
if s == "" || cutset == "" {
return s
}
return TrimLeftFunc(s, makeCutsetFunc(cutset))
}
// TrimRight returns a slice of the string s, with all trailing
// Unicode code points contained in cutset removed.
func TrimRight(s string, cutset string) string {
if s == "" || cutset == "" {
return s
}
return TrimRightFunc(s, makeCutsetFunc(cutset))
}
// TrimSpace returns a slice of the string s, with all leading
// and trailing white space removed, as defined by Unicode.
func TrimSpace(s string) string {
return TrimFunc(s, unicode.IsSpace)
} }
...@@ -362,9 +362,76 @@ func TestSpecialCase(t *testing.T) { ...@@ -362,9 +362,76 @@ func TestSpecialCase(t *testing.T) {
} }
} }
func TestTrimSpace(t *testing.T) { runStringTests(t, TrimSpace, "TrimSpace", trimSpaceTests) } func TestTrimSpace(t *testing.T) { runStringTests(t, TrimSpace, "TrimSpace", trimSpaceTests) }
type TrimTest struct {
f func(string, string) string
in, cutset, out string
}
var trimTests = []TrimTest{
TrimTest{Trim, "abba", "a", "bb"},
TrimTest{Trim, "abba", "ab", ""},
TrimTest{TrimLeft, "abba", "ab", ""},
TrimTest{TrimRight, "abba", "ab", ""},
TrimTest{TrimLeft, "abba", "a", "bba"},
TrimTest{TrimRight, "abba", "a", "abb"},
TrimTest{Trim, "<tag>", "<>", "tag"},
TrimTest{Trim, "* listitem", " *", "listitem"},
TrimTest{Trim, `"quote"`, `"`, "quote"},
TrimTest{Trim, "\u2C6F\u2C6F\u0250\u0250\u2C6F\u2C6F", "\u2C6F", "\u0250\u0250"},
//empty string tests
TrimTest{Trim, "abba", "", "abba"},
TrimTest{Trim, "", "123", ""},
TrimTest{Trim, "", "", ""},
TrimTest{TrimLeft, "abba", "", "abba"},
TrimTest{TrimLeft, "", "123", ""},
TrimTest{TrimLeft, "", "", ""},
TrimTest{TrimRight, "abba", "", "abba"},
TrimTest{TrimRight, "", "123", ""},
TrimTest{TrimRight, "", "", ""},
}
func TestTrim(t *testing.T) {
for _, tc := range trimTests {
actual := tc.f(tc.in, tc.cutset)
var name string
switch tc.f {
case Trim:
name = "Trim"
case TrimLeft:
name = "TrimLeft"
case TrimRight:
name = "TrimRight"
default:
t.Error("Undefined trim function")
}
if actual != tc.out {
t.Errorf("%s(%q, %q) = %q; want %q", name, tc.in, tc.cutset, actual, tc.out)
}
}
}
type TrimFuncTest struct {
f func(r int) bool
name, in, out string
}
var trimFuncTests = []TrimFuncTest{
TrimFuncTest{unicode.IsSpace, "IsSpace", space + " hello " + space, "hello"},
TrimFuncTest{unicode.IsDigit, "IsDigit", "\u0e50\u0e5212hello34\u0e50\u0e51", "hello"},
TrimFuncTest{unicode.IsUpper, "IsUpper", "\u2C6F\u2C6F\u2C6F\u2C6FABCDhelloEF\u2C6F\u2C6FGH\u2C6F\u2C6F", "hello"},
}
func TestTrimFunc(t *testing.T) {
for _, tc := range trimFuncTests {
actual := TrimFunc(tc.in, tc.f)
if actual != tc.out {
t.Errorf("TrimFunc(%q, %q) = %q; want %q", tc.in, tc.name, actual, tc.out)
}
}
}
func equal(m string, s1, s2 string, t *testing.T) bool { func equal(m string, s1, s2 string, t *testing.T) bool {
if s1 == s2 { if s1 == s2 {
return true return true
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment