| 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 properly, preserving 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("'foo, bar', baz") | |
| 226 | ['foo, bar', 'baz'] | |
| 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 char in "\"'" and not in_quotes: | |
| 250 | # Start of quoted section | |
| 251 | in_quotes = True | |
| 252 | quote_char = char | |
| 253 | elif char == quote_char and in_quotes: | |
| 254 | # End of quoted section | |
| 255 | in_quotes = False | |
| 256 | quote_char = None | |
| 257 | elif char == sep and not in_quotes: | |
| 258 | # Field separator outside quotes | |
| 259 | field = ''.join(current).strip() | |
| 260 | if field: | |
| 261 | result.append(_unquote(field)) | |
| 262 | current = [] | |
| 263 | i += 1 | |
| 264 | continue | |
| 265 | ||
| 266 | current.append(char) | |
| 267 | i += 1 | |
| 268 | ||
| 269 | # Add the last field | |
| 270 | field = ''.join(current).strip() | |
| 271 | if field: | |
| 272 | result.append(_unquote(field)) | |
| 273 | ||
| 274 | return result | |
| 275 | while i < len(string): | |
| 276 | char = string[i] | |
| 277 | ||
| 278 | if char in "\"'" and not in_quotes: | |
| 279 | # Start of quoted section | |
| 280 | in_quotes = True | |
| 281 | quote_char = char | |
| 282 | elif char == quote_char and in_quotes: | |
| 283 | # End of quoted section | |
| 284 | in_quotes = False | |
| 285 | quote_char = None | |
| 286 | elif char == sep and not in_quotes: | |
| 287 | # Field separator outside quotes | |
| 288 | field = ''.join(current).strip() | |
| 289 | if field: | |
| 290 | result.append(_unquote(field)) | |
| 291 | current = [] | |
| 292 | i += 1 | |
| 293 | continue | |
| 294 | ||
| 295 | current.append(char) | |
| 296 | i += 1 | |
| 297 | ||
| 298 | # Add the last field | |
| 299 | field = ''.join(current).strip() | |
| 300 | if field: | |
| 301 | result.append(_unquote(field)) | |
| 302 | ||
| 303 | return result | |
| 233 | 304 | |
| 234 | 305 | |
| 235 | 306 | 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.