Skip to content

Commit 9ea886b

Browse files
committed
SplitQuotedString always returns a result
If the function finds an opening quote character and does not find the closing quote character, it will return an error. Previously, in case of error, the returned split array was always empty. Now, in any case, the function will return the split array up to the point where the error occurred.
1 parent a4bd95c commit 9ea886b

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

Diff for: strings.go

+23-3
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,29 @@ import (
3939
// to use spaces in a single element of the split using quote characters.
4040
//
4141
// For example the call:
42-
// SplitQuotedString(`This 'is an' "Hello World!" example`, `'"`, false)
42+
//
43+
// SplitQuotedString(`This 'is an' "Hello World!" example`, `'"`, false)
44+
//
4345
// returns the following array:
44-
// []string{"This", "is an", "Hello World!", "example"}
46+
//
47+
// []string{"This", "is an", "Hello World!", "example"}
48+
//
49+
// The quoteChars parameter is a string containing all the characters that
50+
// are considered as quote characters. If a quote character is found, the
51+
// function will consider the text between the quote character and the next
52+
// quote character as a single element of the split.
53+
//
54+
// The acceptEmptyArguments parameter is a boolean that indicates if the
55+
// function should consider empty arguments as valid elements of the split.
56+
// If set to false, the function will ignore empty arguments.
57+
//
58+
// If the function finds an opening quote character and does not find the
59+
// closing quote character, it will return an error. In any case, the function
60+
// will return the split array up to the point where the error occurred.
61+
//
62+
// The function does not support escaping of quote characters.
63+
//
64+
// The function is UTF-8 safe.
4565
func SplitQuotedString(src string, quoteChars string, acceptEmptyArguments bool) ([]string, error) {
4666
// Make a map of valid quote runes
4767
isQuote := map[rune]bool{}
@@ -83,7 +103,7 @@ func SplitQuotedString(src string, quoteChars string, acceptEmptyArguments bool)
83103
}
84104

85105
if escapingChar != 0 {
86-
return nil, fmt.Errorf("invalid quoting, no closing `%c` char found", escapingChar)
106+
return result, fmt.Errorf("invalid quoting, no closing `%c` char found", escapingChar)
87107
}
88108

89109
return result, nil

Diff for: strings_test.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,11 @@ func TestSplitQuotedStringWithUTF8(t *testing.T) {
6464
}
6565

6666
func TestSplitQuotedStringInvalid(t *testing.T) {
67-
_, err := SplitQuotedString(`'this is' a 'test of quoting`, `"'`, true)
68-
require.Error(t, err)
69-
_, err = SplitQuotedString(`'this is' a "'test" of "quoting`, `"'`, true)
70-
require.Error(t, err)
67+
res, err := SplitQuotedString(`'this is' a 'test of quoting`, `"'`, true)
68+
require.EqualError(t, err, "invalid quoting, no closing `'` char found")
69+
require.Equal(t, res, []string{"this is", "a"})
70+
71+
res, err = SplitQuotedString(`'this is' a "'test" of "quoting`, `"'`, true)
72+
require.EqualError(t, err, "invalid quoting, no closing `\"` char found")
73+
require.Equal(t, res, []string{"this is", "a", "'test", "of"})
7174
}

0 commit comments

Comments
 (0)