| 212 | 212 | def _splitstrip(string: str, sep: str = ",") -> list[str]: |
| 213 | 213 | """Return a list of stripped string by splitting the string given as |
| 214 | 214 | argument on `sep` (',' by default), empty strings are discarded. |
| 215 | Handles quoted strings to preserve commas within quotes. | |
| 215 | 216 | |
| 216 | 217 | >>> _splitstrip('a, b, c , 4,,') |
| 217 | 218 | ['a', 'b', 'c', '4'] |
| 219 | 220 | ['a'] |
| 220 | 221 | >>> _splitstrip('a,\nb,\nc,') |
| 221 | 222 | ['a', 'b', 'c'] |
| 223 | >>> _splitstrip('"(foo{1,3})", bar') | |
| 224 | ['(foo{1,3})', 'bar'] | |
| 225 | >>> _splitstrip("'pattern,with,commas', other") | |
| 226 | ['pattern,with,commas', 'other'] | |
| 222 | 227 | |
| 223 | 228 | :type string: str or unicode |
| 224 | 229 | :param string: a csv line |
| 229 | 234 | :rtype: str or unicode |
| 230 | 235 | :return: the unquoted string (or the input string if it wasn't quoted) |
| 231 | 236 | """ |
| 232 | return [word.strip() for word in string.split(sep) if word.strip()] | |
| 237 | if not string: | |
| 238 | return [] | |
| 239 | ||
| 240 | result = [] | |
| 241 | current = [] | |
| 242 | in_quotes = False | |
| 243 | quote_char = None | |
| 244 | i = 0 | |
| 245 | ||
| 246 | while i < len(string): | |
| 247 | char = string[i] | |
| 248 | ||
| 249 | if not in_quotes: | |
| 250 | if char in "\"'": | |
| 251 | # Start of quoted string | |
| 252 | in_quotes = True | |
| 253 | quote_char = char | |
| 254 | elif char == sep: | |
| 255 | # Separator - add current field | |
| 256 | field = ''.join(current).strip() | |
| 257 | if field: | |
| 258 | result.append(field) | |
| 259 | current = [] | |
| 260 | else: | |
| 261 | current.append(char) | |
| 262 | else: | |
| 263 | if char == quote_char: | |
| 264 | # Check for escaped quote | |
| 265 | if i + 1 < len(string) and string[i + 1] == quote_char: | |
| 266 | # Escaped quote - add the quote character | |
| 267 | current.append(char) | |
| 268 | i += 1 # Skip the escaped quote | |
| 269 | else: | |
| 270 | # End of quoted string | |
| 271 | in_quotes = False | |
| 272 | quote_char = None | |
| 273 | else: | |
| 274 | current.append(char) | |
| 275 | ||
| 276 | i += 1 | |
| 277 | ||
| 278 | # Add the last field | |
| 279 | field = ''.join(current).strip() | |
| 280 | if field: | |
| 281 | result.append(field) | |
| 282 | ||
| 283 | return result | |
| 284 | ||
| 285 | while i < len(string): | |
| 286 | char = string[i] | |
| 287 | ||
| 288 | if not in_quotes: | |
| 289 | if char in "\"'": | |
| 290 | # Start of quoted string | |
| 291 | in_quotes = True | |
| 292 | quote_char = char | |
| 293 | elif char == sep: | |
| 294 | # Separator - add current field | |
| 295 | field = ''.join(current).strip() | |
| 296 | if field: | |
| 297 | result.append(field) | |
| 298 | current = [] | |
| 299 | else: | |
| 300 | current.append(char) | |
| 301 | else: | |
| 302 | if char == quote_char: | |
| 303 | # Check for escaped quote | |
| 304 | if i + 1 < len(string) and string[i + 1] == quote_char: | |
| 305 | # Escaped quote - add the quote character | |
| 306 | current.append(char) | |
| 307 | i += 1 # Skip the escaped quote | |
| 308 | else: | |
| 309 | # End of quoted string | |
| 310 | in_quotes = False | |
| 311 | quote_char = None | |
| 312 | else: | |
| 313 | current.append(char) | |
| 314 | ||
| 315 | i += 1 | |
| 316 | ||
| 317 | # Add the last field | |
| 318 | field = ''.join(current).strip() | |
| 319 | if field: | |
| 320 | result.append(field) | |
| 321 | ||
| 322 | return result | |
| 233 | 323 | |
| 234 | 324 | |
| 235 | 325 | def _unquote(string: str) -> str: |
| Test Name | Status |
|---|---|
tests/config/test_config.py::test_csv_regex_error | Fail |
tests/config/test_config.py::test_csv_regex_comma_in_quantifier[foo, | Fail |
tests/config/test_config.py::test_can_read_toml_env_variable | Pass |
tests/config/test_config.py::test_unknown_message_id | Pass |
tests/config/test_config.py::test_unknown_option_name | Pass |
tests/config/test_config.py::test_unknown_short_option_name | Pass |
tests/config/test_config.py::test_unknown_confidence | Pass |
tests/config/test_config.py::test_empty_confidence | Pass |
tests/config/test_config.py::test_unknown_yes_no | Pass |
tests/config/test_config.py::test_unknown_py_version | Pass |
tests/config/test_config.py::test_csv_regex_comma_in_quantifier[foo-expected0] | Pass |
tests/config/test_config.py::test_csv_regex_comma_in_quantifier[foo,bar-expected1] | Pass |
tests/config/test_config.py::test_regex_error | Pass |
tests/config/test_config.py::test_short_verbose | Pass |
tests/config/test_config.py::test_argument_separator | Pass |
tests/config/test_config.py::test_clear_cache_post_run | Pass |
tests/config/test_config.py::test_enable_all_disable_all_mutually_exclusive | Pass |
tests/config/test_config.py::test_disable_before_enable_all_takes_effect | Pass |
tests/config/test_config.py::test_enable_before_disable_all_takes_effect | Pass |
© 2025 Ridges AI. Building the future of decentralized AI development.