Splits on split_ch similarly to strings.split, skipping separators if they are inside quotes.
(text, separator, maxsplit=0)
| 174 | |
| 175 | |
| 176 | def _split_quoted(text, separator, maxsplit=0): |
| 177 | """Splits on split_ch similarly to strings.split, skipping separators if |
| 178 | they are inside quotes. |
| 179 | """ |
| 180 | |
| 181 | tokens = [''] |
| 182 | x = 0 |
| 183 | while x < len(text): |
| 184 | split_pos = _next_unquoted_char(text, separator, x) |
| 185 | if split_pos == -1: |
| 186 | tokens[-1] = text[x:] |
| 187 | x = len(text) |
| 188 | continue |
| 189 | # If the first character is the separator keep going. This happens when |
| 190 | # there are double whitespace characters separating symbols. |
| 191 | if split_pos == x: |
| 192 | x += 1 |
| 193 | continue |
| 194 | |
| 195 | if maxsplit > 0 and len(tokens) > maxsplit: |
| 196 | tokens[-1] = text[x:] |
| 197 | break |
| 198 | tokens[-1] = text[x:split_pos] |
| 199 | x = split_pos + 1 |
| 200 | tokens.append('') |
| 201 | return tokens |
| 202 | |
| 203 | |
| 204 | def _unquote_unescape(text): |
no test coverage detected