Replace quoted substrings of input string. Return a new string and a mapping of replacements.
(s)
| 1170 | |
| 1171 | |
| 1172 | def eliminate_quotes(s): |
| 1173 | """Replace quoted substrings of input string. |
| 1174 | |
| 1175 | Return a new string and a mapping of replacements. |
| 1176 | """ |
| 1177 | d = {} |
| 1178 | |
| 1179 | def repl(m): |
| 1180 | kind, value = m.groups()[:2] |
| 1181 | if kind: |
| 1182 | # remove trailing underscore |
| 1183 | kind = kind[:-1] |
| 1184 | p = {"'": "SINGLE", '"': "DOUBLE"}[value[0]] |
| 1185 | k = f'{kind}@__f2py_QUOTES_{p}_{COUNTER.__next__()}@' |
| 1186 | d[k] = value |
| 1187 | return k |
| 1188 | |
| 1189 | new_s = re.sub(r'({kind}_|)({single_quoted}|{double_quoted})'.format( |
| 1190 | kind=r'\w[\w\d_]*', |
| 1191 | single_quoted=r"('([^'\\]|(\\.))*')", |
| 1192 | double_quoted=r'("([^"\\]|(\\.))*")'), |
| 1193 | repl, s) |
| 1194 | |
| 1195 | assert '"' not in new_s |
| 1196 | assert "'" not in new_s |
| 1197 | |
| 1198 | return new_s, d |
| 1199 | |
| 1200 | |
| 1201 | def insert_quotes(s, d): |