Skip to content

Commit

Permalink
fix #37
Browse files Browse the repository at this point in the history
  • Loading branch information
svenkreiss committed Jan 9, 2021
1 parent 32831cb commit 5134502
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
17 changes: 17 additions & 0 deletions tests/test_subsuper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import unicodeit


def test_superscript_12():
assert unicodeit.replace('a^{12}') == 'a¹²'


def test_superscript_minus1():
assert unicodeit.replace('cm^{-1}') == 'cm⁻¹'


def test_subscript_12():
assert unicodeit.replace('a_{12}') == 'a₁₂'


def test_subscript_minus1():
assert unicodeit.replace('cm_{-1}') == 'cm₋₁'
8 changes: 4 additions & 4 deletions unicodeit/replace.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ def replace(f: str):
offset = 0
for s in re.finditer(
r"_\{[0-9\+-=\(\)<>\-aeoxjhklmnpstiruv"
r"\u03B2\u03B3\u03C1\u03C6\u03C7]+\}", f):
r"\u03B2\u03B3\u03C1\u03C6\u03C7\u2212]+\}", f):
newstring, n = re.subn(
r"([0-9\+-=\(\)<>\-aeoxjhklmnpstiruv"
r"\u03B2\u03B3\u03C1\u03C6\u03C7])",
r"\u03B2\u03B3\u03C1\u03C6\u03C7\u2212])",
r"_\1", s.group(0)[2:-1])
f = f[:s.start() + offset] + newstring + f[s.end() + offset:]
offset += n * 2 - (n + 3)
Expand All @@ -34,11 +34,11 @@ def replace(f: str):
for s in re.finditer(
r"\^\{[0-9\+-=\(\)<>ABDEGHIJKLMNOPRTUW"
r"abcdefghijklmnoprstuvwxyz"
r"\u03B2\u03B3\u03B4\u03C6\u03C7\u222B]+\}", f):
r"\u03B2\u03B3\u03B4\u03C6\u03C7\u222B\u2212]+\}", f):
newstring, n = re.subn(
r"([0-9\+-=\(\)<>ABDEGHIJKLMNOPRTUW"
r"abcdefghijklmnoprstuvwxyz"
r"\u03B2\u03B3\u03B4\u03C6\u03C7\u222B])",
r"\u03B2\u03B3\u03B4\u03C6\u03C7\u222B\u2212])",
r"^\1", s.group(0)[2:-1])
f = f[:s.start() + offset] + newstring + f[s.end() + offset:]
offset += n * 2 - (n + 3)
Expand Down

0 comments on commit 5134502

Please sign in to comment.