Notate consecutive characters as sequence (1-4 instead of 1234)
(string)
| 135 | result = r'%s%s-' % (result[:pos], result[pos + 1:]) |
| 136 | |
| 137 | def sequentize(string): |
| 138 | """ |
| 139 | Notate consecutive characters as sequence |
| 140 | |
| 141 | (1-4 instead of 1234) |
| 142 | """ |
| 143 | first, last, result = None, None, [] |
| 144 | for char in map(ord, string): |
| 145 | if last is None: |
| 146 | first = last = char |
| 147 | elif last + 1 == char: |
| 148 | last = char |
| 149 | else: |
| 150 | result.append((first, last)) |
| 151 | first = last = char |
| 152 | if last is not None: |
| 153 | result.append((first, last)) |
| 154 | return ''.join(['%s%s%s' % ( |
| 155 | chr(first), |
| 156 | last > first + 1 and '-' or '', |
| 157 | last != first and chr(last) or '' |
| 158 | ) for first, last in result]) # noqa |
| 159 | |
| 160 | return _re.sub( |
| 161 | r"([\000-\040'`])", # ' and ` for better portability |
no test coverage detected