def iternext(self):
"""
Iterate through characters of the string.
Count escaped l, L, c, C, E and backslash as a single char.
"""
if self.index > self.max_index:
raise StopIteration
char = self.string[self.index:self.index + 1]
if char == self._b_slash:
m = self._replace_ref.match(self.string[self.index + 1:])
if m:
ref = m.group(0)
if len(ref) == 1 and ref in self._long_replace_refs:
if ref == self._hex:
raise SyntaxError('Format for byte is \\xXX!')
elif ref == self._group:
raise SyntaxError('Format for group is \\g<group_name_or_index>!')
elif ref == self._unicode_name:
raise SyntaxError('Format for Unicode name is \\N{name}!')
elif ref == self._unicode_narrow: # pragma: no cover
raise SyntaxError('Format for Unicode is \\uXXXX!')
elif ref == self._unicode_wide: # pragma: no cover
raise SyntaxError('Format for wide Unicode is \\UXXXXXXXX!')
if self.use_format and (m.group(3) or m.group(4)):
char += self._b_slash
self.index -= 1
if not self.use_format or not m.group(4):
char += m.group(1) if m.group(1) else m.group(2)
elif self.use_format and char in (self._lc_bracket, self._rc_bracket):
m = self._format_replace_group.match(self.string[self.index:])
if m:
if m.group(2):
char = m.group(2)
else:
self.index += 1
else:
raise ValueError("Single unmatched curly bracket!")
self.index += len(char)
self.current = char
return self.current