diff options
author | Tommy Nguyen <remyabel@gmail.com> | 2018-08-19 07:20:50 -0400 |
---|---|---|
committer | Tommy Nguyen <remyabel@gmail.com> | 2018-08-19 07:20:50 -0400 |
commit | cea736e6e92bb0c37b61af15da9c261b79eb3c9b (patch) | |
tree | 8fe71cb9a7ef17671ced18cae438ae3b75736ba0 /pkg/utils | |
parent | b46d174f706a5a66307111a55eabf1b64dd39c12 (diff) |
Factor out into NormalizeLinefeeds; add tests
Diffstat (limited to 'pkg/utils')
-rw-r--r-- | pkg/utils/utils.go | 6 | ||||
-rw-r--r-- | pkg/utils/utils_test.go | 34 |
2 files changed, 40 insertions, 0 deletions
diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index 68438246a..d53ddc6dd 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -63,3 +63,9 @@ func TrimTrailingNewline(str string) string { } return str } + +func NormalizeLinefeeds(str string) string { + str = strings.Replace(str, "\r\n", "\n", -1) + str = strings.Replace(str, "\r", "\n", -1) + return str +} diff --git a/pkg/utils/utils_test.go b/pkg/utils/utils_test.go new file mode 100644 index 000000000..3a9f6817b --- /dev/null +++ b/pkg/utils/utils_test.go @@ -0,0 +1,34 @@ +package utils + +import "testing" + +var testCases = []struct { + Input []byte + Expected []byte +}{ + { + // \r\n + Input: []byte{97, 115, 100, 102, 13, 10}, + Expected: []byte{97, 115, 100, 102, 10}, + }, + { + // \r + Input: []byte{97, 115, 100, 102, 13}, + Expected: []byte{97, 115, 100, 102, 10}, + }, + { + // \n + Input: []byte{97, 115, 100, 102, 10}, + Expected: []byte{97, 115, 100, 102, 10}, + }, + +} + +func TestNormalizeLinefeeds(t *testing.T) { + + for _, tc := range testCases { + if NormalizeLinefeeds(string(tc.Input)) != string(tc.Expected) { + t.Error("Error") + } + } +} |