@@ -190,18 +190,26 @@ private static int indexOfUnescapedChar(@Nonnull final String str, final char ch
190190 return -1 ;
191191 }
192192
193+ /*
194+ This works on the principle that the copula '=' is the _governing verb_ of any key to value
195+ expression. So parsing begins based on the verb ('=') not on the assumed expression termination
196+ character (','). The Left and right values of the split based on ('=') are collected and checked for
197+ the correct statement terminator (an unescaped ','). Any value on the left of an unescaped ',' is a
198+ value. Any value on the right is a key. These are placed in their respective ordered lists and then
199+ recombined into the tags HashMap.
200+ */
193201 private static Map <String , String > parseTags (@ Nonnull final String value ) {
194202 final Map <String , String > tags = new HashMap <>();
195203 final List <String > keys = new ArrayList <>();
196204 final List <String > values = new ArrayList <>();
197205 if (!value .isEmpty ()) {
198206 String [] chunks = value .split ("=" );
199207 for (int i = 0 ; i < chunks .length ; i ++) {
200- if (i == 0 ) {
208+ if (i == 0 ) { // first element will be a key on its own.
201209 keys .add (chunks [i ]);
202- } else if (i == chunks .length - 1 ) {
210+ } else if (i == chunks .length - 1 ) { // the last element will be a value on its own.
203211 values .add (chunks [i ]);
204- } else {
212+ } else { // check for legitimate keys and values
205213 int commaIndex = indexOfUnescapedChar (chunks [i ], ',' );
206214 if (commaIndex != -1 ) {
207215 String v = chunks [i ].substring (0 , commaIndex );
@@ -212,9 +220,9 @@ private static Map<String, String> parseTags(@Nonnull final String value) {
212220 }
213221 }
214222 for (int i = 0 ; i < keys .size (); i ++) {
215- tags .put (
216- keys .get (i ).contains ("\\ , " ) ? "\" " + keys .get (i ) + "\" " : keys .get (i ),
217- values .get (i ).contains ("\\ , " ) ? "\" " + values .get (i ) + "\" " : values .get (i )
223+ tags .put ( // be sure to surround any values containing escapes with double quotes
224+ keys .get (i ).contains ("\\ " ) ? "\" " + keys .get (i ) + "\" " : keys .get (i ),
225+ values .get (i ).contains ("\\ " ) ? "\" " + values .get (i ) + "\" " : values .get (i )
218226 );
219227 }
220228 }
0 commit comments