@@ -65,8 +65,8 @@ func parseKeyValue[K any](target ottl.StringGetter[K], d ottl.Optional[string],
65
65
if len (pair ) != 2 {
66
66
return nil , fmt .Errorf ("cannot split '%s' into 2 items, got %d" , p , len (pair ))
67
67
}
68
- key := strings .TrimSpace (strings . Trim ( pair [0 ], " \" '" ) )
69
- value := strings .TrimSpace (strings . Trim ( pair [1 ], " \" '" ) )
68
+ key := strings .TrimSpace (pair [0 ])
69
+ value := strings .TrimSpace (pair [1 ])
70
70
parsed [key ] = value
71
71
}
72
72
@@ -84,36 +84,34 @@ func splitPairs(input, pairDelimiter string) ([]string, error) {
84
84
delimiterLength := len (pairDelimiter )
85
85
quoteChar := "" // "" means we are not in quotes
86
86
87
- i := 0
88
- for i < len (input ) {
89
- if quoteChar == "" && i + delimiterLength <= len (input ) && input [i :i + delimiterLength ] == pairDelimiter {
90
- if currentPair == "" {
91
- i ++
87
+ for i := 0 ; i < len (input ); i ++ {
88
+ if quoteChar == "" && i + delimiterLength <= len (input ) && input [i :i + delimiterLength ] == pairDelimiter { // delimiter
89
+ if currentPair == "" { // leading || trailing delimiter; ignore
92
90
continue
93
91
}
94
92
result = append (result , currentPair )
95
93
currentPair = ""
96
- i += delimiterLength
94
+ i += delimiterLength - 1
95
+ continue
96
+ }
97
+
98
+ if quoteChar == "" && (input [i ] == '"' || input [i ] == '\'' ) { // start of quote
99
+ quoteChar = string (input [i ])
97
100
continue
98
- } else if input [i ] == '"' || input [i ] == '\'' {
99
- if quoteChar != "" {
100
- if quoteChar == string (input [i ]) {
101
- quoteChar = ""
102
- }
103
- } else {
104
- quoteChar = string (input [i ])
105
- }
106
101
}
102
+ if string (input [i ]) == quoteChar { // end of quote
103
+ quoteChar = ""
104
+ continue
105
+ }
106
+
107
107
currentPair += string (input [i ])
108
- i ++
109
108
}
110
109
111
- if quoteChar != "" {
110
+ if quoteChar != "" { // check for closed quotes
112
111
return nil , fmt .Errorf ("never reached end of a quoted value" )
113
112
}
114
-
115
- if currentPair != "" {
116
- result = append (result , currentPair )
113
+ if currentPair != "" { // avoid adding empty value bc of a trailing delimiter
114
+ return append (result , currentPair ), nil
117
115
}
118
116
119
117
return result , nil
0 commit comments