@@ -37,57 +37,29 @@ def _replace_escaping(s: str) -> str:
3737 return ESCAPING_RE .sub (replace_escape_sequence , s )
3838
3939
40- def _is_character_escaped (s : str , charpos : int ) -> bool :
41- num_bslashes = 0
42- while (charpos > num_bslashes
43- and s [charpos - 1 - num_bslashes ] == '\\ ' ):
44- num_bslashes += 1
45- return num_bslashes % 2 == 1
46-
47-
4840def _parse_labels (labels_string : str ) -> Dict [str , str ]:
4941 labels : Dict [str , str ] = {}
5042 # Return if we don't have valid labels
5143 if "=" not in labels_string :
5244 return labels
5345
54- escaping = False
55- if "\\ " in labels_string :
56- escaping = True
46+ # remove SINGLE leading and trailing commas
47+ labels_string = labels_string .strip ().removeprefix (',' ).removesuffix (',' )
5748
58- # Copy original labels
59- sub_labels = labels_string
49+ sub_labels = labels_string .split ("," )
6050 try :
6151 # Process one label at a time
62- while sub_labels :
63- # The label name is before the equal
64- value_start = sub_labels .index ("=" )
65- label_name = sub_labels [:value_start ]
66- sub_labels = sub_labels [value_start + 1 :].lstrip ()
67- # Find the first quote after the equal
68- quote_start = sub_labels .index ('"' ) + 1
69- value_substr = sub_labels [quote_start :]
70-
71- # Find the last unescaped quote
72- i = 0
73- while i < len (value_substr ):
74- i = value_substr .index ('"' , i )
75- if not _is_character_escaped (value_substr , i ):
76- break
77- i += 1
78-
79- # The label value is between the first and last quote
80- quote_end = i + 1
81- label_value = sub_labels [quote_start :quote_end ]
82- # Replace escaping if needed
83- if escaping :
84- label_value = _replace_escaping (label_value )
85- labels [label_name .strip ()] = label_value
86-
87- # Remove the processed label from the sub-slice for next iteration
88- sub_labels = sub_labels [quote_end + 1 :]
89- next_comma = sub_labels .find ("," ) + 1
90- sub_labels = sub_labels [next_comma :].lstrip ()
52+ for label in sub_labels :
53+ label_name , label_value = label .split ("=" )
54+
55+ normalized_value = label_value .strip ()
56+ # remove SINGLE leading and trailing double quotes
57+ normalized_value = normalized_value .removeprefix ('"' ).removesuffix ('"' )
58+
59+ if "\\ " in normalized_value :
60+ normalized_value = _replace_escaping (normalized_value )
61+
62+ labels [label_name .strip ()] = normalized_value
9163
9264 return labels
9365
0 commit comments