From e99a80940f2887defb5df60033d2261a1d8c6f59 Mon Sep 17 00:00:00 2001 From: Amy Boyd Date: Sat, 25 Jun 2016 12:28:23 +0100 Subject: [PATCH] Add utility function SplitIntoLines. --- editorconfig/utils.go | 6 ++++++ editorconfig/utils_test.go | 10 ++++++++++ 2 files changed, 16 insertions(+) diff --git a/editorconfig/utils.go b/editorconfig/utils.go index 1f2e79c..219b0e7 100644 --- a/editorconfig/utils.go +++ b/editorconfig/utils.go @@ -24,6 +24,12 @@ func ContainsString(haystack []string, needle string) bool { return false } +var lineEndingsRegexp = regexp.MustCompile("(\r\n|\n|\r)") + +func SplitIntoLines(s string) []string { + return lineEndingsRegexp.Split(s, -1) +} + func ExitBecauseOfInternalError(err string) { fmt.Println(err) os.Exit(2) diff --git a/editorconfig/utils_test.go b/editorconfig/utils_test.go index 9fe0ccd..c33cfca 100644 --- a/editorconfig/utils_test.go +++ b/editorconfig/utils_test.go @@ -1,6 +1,8 @@ package editorconfig import ( + "reflect" + "strings" "testing" ) @@ -15,3 +17,11 @@ func TestGetParentDir(t *testing.T) { t.Error() } } + +func TestSplitIntoLines(t *testing.T) { + result := SplitIntoLines("Aardvark\nBunny\rCat\r\nDolphin\n") + expected := []string{"Aardvark", "Bunny", "Cat", "Dolphin", ""} + if !reflect.DeepEqual(result, expected) { + t.Error("Did not split string into lines correctly, got lines: " + strings.Join(result, ", ")) + } +}