From 97e406678c8d695ee6eaefce41b83e9e27e239c7 Mon Sep 17 00:00:00 2001 From: Chao Wang Date: Mon, 13 Nov 2017 10:01:11 +0800 Subject: [PATCH] Split and remove pkg/stringutils Signed-off-by: Chao Wang --- pkg/stringutils/README.md | 1 - pkg/stringutils/stringutils.go | 77 ----------------------- pkg/stringutils/stringutils_test.go | 96 ----------------------------- 3 files changed, 174 deletions(-) delete mode 100644 pkg/stringutils/README.md delete mode 100644 pkg/stringutils/stringutils.go delete mode 100644 pkg/stringutils/stringutils_test.go diff --git a/pkg/stringutils/README.md b/pkg/stringutils/README.md deleted file mode 100644 index b3e454573c..0000000000 --- a/pkg/stringutils/README.md +++ /dev/null @@ -1 +0,0 @@ -This package provides helper functions for dealing with strings diff --git a/pkg/stringutils/stringutils.go b/pkg/stringutils/stringutils.go deleted file mode 100644 index 794a225190..0000000000 --- a/pkg/stringutils/stringutils.go +++ /dev/null @@ -1,77 +0,0 @@ -// Package stringutils provides helper functions for dealing with strings. -package stringutils - -import ( - "bytes" - "math/rand" - "strings" -) - -// GenerateRandomASCIIString generates an ASCII random string with length n. -func GenerateRandomASCIIString(n int) string { - chars := "abcdefghijklmnopqrstuvwxyz" + - "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + - "~!@#$%^&*()-_+={}[]\\|<,>.?/\"';:` " - res := make([]byte, n) - for i := 0; i < n; i++ { - res[i] = chars[rand.Intn(len(chars))] - } - return string(res) -} - -// Ellipsis truncates a string to fit within maxlen, and appends ellipsis (...). -// For maxlen of 3 and lower, no ellipsis is appended. -func Ellipsis(s string, maxlen int) string { - r := []rune(s) - if len(r) <= maxlen { - return s - } - if maxlen <= 3 { - return string(r[:maxlen]) - } - return string(r[:maxlen-3]) + "..." -} - -// Truncate truncates a string to maxlen. -func Truncate(s string, maxlen int) string { - r := []rune(s) - if len(r) <= maxlen { - return s - } - return string(r[:maxlen]) -} - -func quote(word string, buf *bytes.Buffer) { - // Bail out early for "simple" strings - if word != "" && !strings.ContainsAny(word, "\\'\"`${[|&;<>()~*?! \t\n") { - buf.WriteString(word) - return - } - - buf.WriteString("'") - - for i := 0; i < len(word); i++ { - b := word[i] - if b == '\'' { - // Replace literal ' with a close ', a \', and an open ' - buf.WriteString("'\\''") - } else { - buf.WriteByte(b) - } - } - - buf.WriteString("'") -} - -// ShellQuoteArguments takes a list of strings and escapes them so they will be -// handled right when passed as arguments to a program via a shell -func ShellQuoteArguments(args []string) string { - var buf bytes.Buffer - for i, arg := range args { - if i != 0 { - buf.WriteByte(' ') - } - quote(arg, &buf) - } - return buf.String() -} diff --git a/pkg/stringutils/stringutils_test.go b/pkg/stringutils/stringutils_test.go deleted file mode 100644 index 6a8cab7224..0000000000 --- a/pkg/stringutils/stringutils_test.go +++ /dev/null @@ -1,96 +0,0 @@ -package stringutils - -import "testing" - -func testLengthHelper(generator func(int) string, t *testing.T) { - expectedLength := 20 - s := generator(expectedLength) - if len(s) != expectedLength { - t.Fatalf("Length of %s was %d but expected length %d", s, len(s), expectedLength) - } -} - -func testUniquenessHelper(generator func(int) string, t *testing.T) { - repeats := 25 - set := make(map[string]struct{}, repeats) - for i := 0; i < repeats; i = i + 1 { - str := generator(64) - if len(str) != 64 { - t.Fatalf("Id returned is incorrect: %s", str) - } - if _, ok := set[str]; ok { - t.Fatalf("Random number is repeated") - } - set[str] = struct{}{} - } -} - -func isASCII(s string) bool { - for _, c := range s { - if c > 127 { - return false - } - } - return true -} - -func TestGenerateRandomAsciiStringLength(t *testing.T) { - testLengthHelper(GenerateRandomASCIIString, t) -} - -func TestGenerateRandomAsciiStringUniqueness(t *testing.T) { - testUniquenessHelper(GenerateRandomASCIIString, t) -} - -func TestGenerateRandomAsciiStringIsAscii(t *testing.T) { - str := GenerateRandomASCIIString(64) - if !isASCII(str) { - t.Fatalf("%s contained non-ascii characters", str) - } -} - -func TestEllipsis(t *testing.T) { - str := "t🐳ststring" - newstr := Ellipsis(str, 3) - if newstr != "t🐳s" { - t.Fatalf("Expected t🐳s, got %s", newstr) - } - newstr = Ellipsis(str, 8) - if newstr != "t🐳sts..." { - t.Fatalf("Expected tests..., got %s", newstr) - } - newstr = Ellipsis(str, 20) - if newstr != "t🐳ststring" { - t.Fatalf("Expected t🐳ststring, got %s", newstr) - } -} - -func TestTruncate(t *testing.T) { - str := "t🐳ststring" - newstr := Truncate(str, 4) - if newstr != "t🐳st" { - t.Fatalf("Expected t🐳st, got %s", newstr) - } - newstr = Truncate(str, 20) - if newstr != "t🐳ststring" { - t.Fatalf("Expected t🐳ststring, got %s", newstr) - } -} - -func TestShellQuoteArgumentsEmpty(t *testing.T) { - actual := ShellQuoteArguments([]string{}) - expected := "" - if actual != expected { - t.Fatalf("Expected an empty string") - } -} - -func TestShellQuoteArguments(t *testing.T) { - simpleString := "simpleString" - complexString := "This is a 'more' complex $tring with some special char *" - actual := ShellQuoteArguments([]string{simpleString, complexString}) - expected := "simpleString 'This is a '\\''more'\\'' complex $tring with some special char *'" - if actual != expected { - t.Fatalf("Expected \"%v\", got \"%v\"", expected, actual) - } -}