From 92ac0cfbb2b3244784b5ab52d1dd31598975a76d Mon Sep 17 00:00:00 2001 From: Michael Hoisie Date: Fri, 9 Apr 2010 18:57:03 -0700 Subject: [PATCH] strings: add IndexRune, Trim, TrimLeft, TrimRight, and the generic equivalents TrimFunc, TrimLeftFunc, TrimRightFunc R=rsc, r CC=golang-dev https://golang.org/cl/799048 --- src/pkg/strings/strings.go | 81 +++++++++++++++++++++++++++++---- src/pkg/strings/strings_test.go | 69 +++++++++++++++++++++++++++- 2 files changed, 139 insertions(+), 11 deletions(-) diff --git a/src/pkg/strings/strings.go b/src/pkg/strings/strings.go index 4268551374b..0a9f64ca0ca 100644 --- a/src/pkg/strings/strings.go +++ b/src/pkg/strings/strings.go @@ -106,6 +106,17 @@ func LastIndex(s, sep string) int { 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 // from chars in s, or -1 if no Unicode code point from chars is present in s. func IndexAny(s, chars string) int { @@ -309,9 +320,9 @@ func ToTitleSpecial(_case unicode.SpecialCase, s string) string { 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 -// removed, as defined by Unicode. -func TrimSpace(s string) string { +// TrimLeftFunc returns a slice of the string s with all leading +// Unicode code points c satisfying f(c) removed. +func TrimLeftFunc(s string, f func(r int) bool) string { start, end := 0, len(s) for start < end { wid := 1 @@ -319,16 +330,23 @@ func TrimSpace(s string) string { if rune >= utf8.RuneSelf { rune, wid = utf8.DecodeRuneInString(s[start:end]) } - if !unicode.IsSpace(rune) { - break + if !f(rune) { + return s[start:] } 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 { wid := 1 - rune := int(s[end-1]) + rune := int(s[end-wid]) 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++ { } if start > end-wid { // invalid UTF-8 sequence; stop processing @@ -336,10 +354,53 @@ func TrimSpace(s string) string { } rune, wid = utf8.DecodeRuneInString(s[end-wid : end]) } - if !unicode.IsSpace(rune) { - break + if !f(rune) { + return s[0:end] } 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) } diff --git a/src/pkg/strings/strings_test.go b/src/pkg/strings/strings_test.go index eeb64f1e8ee..6c2bd727d76 100644 --- a/src/pkg/strings/strings_test.go +++ b/src/pkg/strings/strings_test.go @@ -362,9 +362,76 @@ func TestSpecialCase(t *testing.T) { } } - 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"}, + 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 { if s1 == s2 { return true