Skip to content

Commit 04a7c30

Browse files
committed
Replace .srcCharCode[*] with .srcCharCodeAt(*) using regex
1 parent dfab381 commit 04a7c30

23 files changed

+78
-72
lines changed

markdown_it/helpers/parse_link_label.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def parseLinkLabel(state: StateInline, start: int, disableNested: bool = False)
1818
level = 1
1919

2020
while state.pos < state.posMax:
21-
marker = state.srcCharCode[state.pos]
21+
marker = state.srcCharCodeAt(state.pos)
2222
if marker == 0x5D: # /* ] */)
2323
level -= 1
2424
if level == 0:

markdown_it/rules_block/blockquote.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def blockquote(state: StateBlock, startLine: int, endLine: int, silent: bool):
2323
return False
2424

2525
# check the block quote marker
26-
if state.srcCharCode[pos] != 0x3E: # /* > */
26+
if state.srcCharCodeAt(pos) != 0x3E: # /* > */
2727
return False
2828
pos += 1
2929

@@ -36,7 +36,7 @@ def blockquote(state: StateBlock, startLine: int, endLine: int, silent: bool):
3636
initial = offset = state.sCount[startLine] + 1
3737

3838
try:
39-
second_char_code: Optional[int] = state.srcCharCode[pos]
39+
second_char_code: Optional[int] = state.srcCharCodeAt(pos)
4040
except IndexError:
4141
second_char_code = None
4242

@@ -72,7 +72,7 @@ def blockquote(state: StateBlock, startLine: int, endLine: int, silent: bool):
7272
state.bMarks[startLine] = pos
7373

7474
while pos < max:
75-
ch = state.srcCharCode[pos]
75+
ch = state.srcCharCodeAt(pos)
7676

7777
if isSpace(ch):
7878
if ch == 0x09: # / tab /
@@ -146,7 +146,7 @@ def blockquote(state: StateBlock, startLine: int, endLine: int, silent: bool):
146146
# Case 1: line is not inside the blockquote, and this line is empty.
147147
break
148148

149-
evaluatesTrue = state.srcCharCode[pos] == 0x3E and not isOutdented # /* > */
149+
evaluatesTrue = state.srcCharCodeAt(pos) == 0x3E and not isOutdented # /* > */
150150
pos += 1
151151
if evaluatesTrue:
152152
# This line is inside the blockquote.
@@ -155,7 +155,7 @@ def blockquote(state: StateBlock, startLine: int, endLine: int, silent: bool):
155155
initial = offset = state.sCount[nextLine] + 1
156156

157157
try:
158-
next_char: Optional[int] = state.srcCharCode[pos]
158+
next_char: Optional[int] = state.srcCharCodeAt(pos)
159159
except IndexError:
160160
next_char = None
161161

@@ -191,7 +191,7 @@ def blockquote(state: StateBlock, startLine: int, endLine: int, silent: bool):
191191
state.bMarks[nextLine] = pos
192192

193193
while pos < max:
194-
ch = state.srcCharCode[pos]
194+
ch = state.srcCharCodeAt(pos)
195195

196196
if isSpace(ch):
197197
if ch == 0x09:

markdown_it/rules_block/fence.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def fence(state: StateBlock, startLine: int, endLine: int, silent: bool):
2121
if pos + 3 > maximum:
2222
return False
2323

24-
marker = state.srcCharCode[pos]
24+
marker = state.srcCharCodeAt(pos)
2525

2626
# /* ~ */ /* ` */
2727
if marker != 0x7E and marker != 0x60:
@@ -67,7 +67,7 @@ def fence(state: StateBlock, startLine: int, endLine: int, silent: bool):
6767
# test
6868
break
6969

70-
if state.srcCharCode[pos] != marker:
70+
if state.srcCharCodeAt(pos) != marker:
7171
continue
7272

7373
if state.sCount[nextLine] - state.blkIndent >= 4:

markdown_it/rules_block/heading.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def heading(state: StateBlock, startLine: int, endLine: int, silent: bool):
1919
if state.sCount[startLine] - state.blkIndent >= 4:
2020
return False
2121

22-
ch: Optional[int] = state.srcCharCode[pos]
22+
ch: Optional[int] = state.srcCharCodeAt(pos)
2323

2424
# /* # */
2525
if ch != 0x23 or pos >= maximum:
@@ -29,15 +29,15 @@ def heading(state: StateBlock, startLine: int, endLine: int, silent: bool):
2929
level = 1
3030
pos += 1
3131
try:
32-
ch = state.srcCharCode[pos]
32+
ch = state.srcCharCodeAt(pos)
3333
except IndexError:
3434
ch = None
3535
# /* # */
3636
while ch == 0x23 and pos < maximum and level <= 6:
3737
level += 1
3838
pos += 1
3939
try:
40-
ch = state.srcCharCode[pos]
40+
ch = state.srcCharCodeAt(pos)
4141
except IndexError:
4242
ch = None
4343

@@ -51,7 +51,7 @@ def heading(state: StateBlock, startLine: int, endLine: int, silent: bool):
5151

5252
maximum = state.skipSpacesBack(maximum, pos)
5353
tmp = state.skipCharsBack(maximum, 0x23, pos) # #
54-
if tmp > pos and isSpace(state.srcCharCode[tmp - 1]):
54+
if tmp > pos and isSpace(state.srcCharCodeAt(tmp - 1)):
5555
maximum = tmp
5656

5757
state.line = startLine + 1

markdown_it/rules_block/hr.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def hr(state: StateBlock, startLine: int, endLine: int, silent: bool):
2222
if state.sCount[startLine] - state.blkIndent >= 4:
2323
return False
2424

25-
marker = state.srcCharCode[pos]
25+
marker = state.srcCharCodeAt(pos)
2626
pos += 1
2727

2828
# Check hr marker: /* * */ /* - */ /* _ */
@@ -33,7 +33,7 @@ def hr(state: StateBlock, startLine: int, endLine: int, silent: bool):
3333

3434
cnt = 1
3535
while pos < maximum:
36-
ch = state.srcCharCode[pos]
36+
ch = state.srcCharCodeAt(pos)
3737
pos += 1
3838
if ch != marker and not isSpace(ch):
3939
return False

markdown_it/rules_block/html_block.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def html_block(state: StateBlock, startLine: int, endLine: int, silent: bool):
4444
if not state.md.options.get("html", None):
4545
return False
4646

47-
if state.srcCharCode[pos] != 0x3C: # /* < */
47+
if state.srcCharCodeAt(pos) != 0x3C: # /* < */
4848
return False
4949

5050
lineText = state.src[pos:maximum]

markdown_it/rules_block/lheading.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def lheading(state: StateBlock, startLine: int, endLine: int, silent: bool):
3737
maximum = state.eMarks[nextLine]
3838

3939
if pos < maximum:
40-
marker = state.srcCharCode[pos]
40+
marker = state.srcCharCodeAt(pos)
4141

4242
# /* - */ /* = */
4343
if marker == 0x2D or marker == 0x3D:
@@ -74,6 +74,7 @@ def lheading(state: StateBlock, startLine: int, endLine: int, silent: bool):
7474
state.line = nextLine + 1
7575

7676
token = state.push("heading_open", "h" + str(level), 1)
77+
assert marker is not None
7778
token.markup = chr(marker)
7879
token.map = [startLine, state.line]
7980

markdown_it/rules_block/list.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ def skipBulletListMarker(state: StateBlock, startLine: int):
1414
pos = state.bMarks[startLine] + state.tShift[startLine]
1515
maximum = state.eMarks[startLine]
1616

17-
marker = state.srcCharCode[pos]
17+
marker = state.srcCharCodeAt(pos)
1818
pos += 1
1919
# Check bullet /* * */ /* - */ /* + */
2020
if marker != 0x2A and marker != 0x2D and marker != 0x2B:
2121
return -1
2222

2323
if pos < maximum:
24-
ch = state.srcCharCode[pos]
24+
ch = state.srcCharCodeAt(pos)
2525

2626
if not isSpace(ch):
2727
# " -test " - is not a list item
@@ -42,7 +42,8 @@ def skipOrderedListMarker(state: StateBlock, startLine: int):
4242
if pos + 1 >= maximum:
4343
return -1
4444

45-
ch = state.srcCharCode[pos]
45+
ch = state.srcCharCodeAt(pos)
46+
assert ch is not None
4647
pos += 1
4748

4849
# /* 0 */ /* 9 */
@@ -54,7 +55,8 @@ def skipOrderedListMarker(state: StateBlock, startLine: int):
5455
if pos >= maximum:
5556
return -1
5657

57-
ch = state.srcCharCode[pos]
58+
ch = state.srcCharCodeAt(pos)
59+
assert ch is not None
5860
pos += 1
5961

6062
# /* 0 */ /* 9 */
@@ -74,7 +76,7 @@ def skipOrderedListMarker(state: StateBlock, startLine: int):
7476
return -1
7577

7678
if pos < maximum:
77-
ch = state.srcCharCode[pos]
79+
ch = state.srcCharCodeAt(pos)
7880

7981
if not isSpace(ch):
8082
# " 1.test " - is not a list item
@@ -156,7 +158,8 @@ def list_block(state: StateBlock, startLine: int, endLine: int, silent: bool):
156158
return False
157159

158160
# We should terminate list on style change. Remember first one to compare.
159-
markerCharCode = state.srcCharCode[posAfterMarker - 1]
161+
markerCharCode = state.srcCharCodeAt(posAfterMarker - 1)
162+
assert markerCharCode is not None
160163

161164
# For validation mode we can terminate immediately
162165
if silent:
@@ -198,7 +201,7 @@ def list_block(state: StateBlock, startLine: int, endLine: int, silent: bool):
198201
)
199202

200203
while pos < maximum:
201-
ch = state.srcCharCode[pos]
204+
ch = state.srcCharCodeAt(pos)
202205

203206
if ch == 0x09: # \t
204207
offset += 4 - (offset + state.bsCount[nextLine]) % 4
@@ -318,7 +321,7 @@ def list_block(state: StateBlock, startLine: int, endLine: int, silent: bool):
318321
if posAfterMarker < 0:
319322
break
320323

321-
if markerCharCode != state.srcCharCode[posAfterMarker - 1]:
324+
if markerCharCode != state.srcCharCodeAt(posAfterMarker - 1):
322325
break
323326

324327
# Finalize list

markdown_it/rules_block/reference.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,17 @@ def reference(state: StateBlock, startLine, _endLine, silent):
2222
if state.sCount[startLine] - state.blkIndent >= 4:
2323
return False
2424

25-
if state.srcCharCode[pos] != 0x5B: # /* [ */
25+
if state.srcCharCodeAt(pos) != 0x5B: # /* [ */
2626
return False
2727

2828
# Simple check to quickly interrupt scan on [link](url) at the start of line.
2929
# Can be useful on practice: https:#github.com/markdown-it/markdown-it/issues/54
3030
while pos < maximum:
3131
# /* ] */ /* \ */ /* : */
32-
if state.srcCharCode[pos] == 0x5D and state.srcCharCode[pos - 1] != 0x5C:
32+
if state.srcCharCodeAt(pos) == 0x5D and state.srcCharCodeAt(pos - 1) != 0x5C:
3333
if pos + 1 == maximum:
3434
return False
35-
if state.srcCharCode[pos + 1] != 0x3A:
35+
if state.srcCharCodeAt(pos + 1) != 0x3A:
3636
return False
3737
break
3838
pos += 1

markdown_it/rules_block/state_block.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ def skipEmptyLines(self, from_pos: int) -> int:
153153
def skipSpaces(self, pos: int) -> int:
154154
"""Skip spaces from given position."""
155155
while pos < len(self.src):
156-
if not isSpace(self.srcCharCode[pos]):
156+
if not isSpace(self.srcCharCodeAt(pos)):
157157
break
158158
pos += 1
159159
return pos
@@ -164,14 +164,14 @@ def skipSpacesBack(self, pos: int, minimum: int) -> int:
164164
return pos
165165
while pos > minimum:
166166
pos -= 1
167-
if not isSpace(self.srcCharCode[pos]):
167+
if not isSpace(self.srcCharCodeAt(pos)):
168168
return pos + 1
169169
return pos
170170

171171
def skipChars(self, pos: int, code: int) -> int:
172172
"""Skip char codes from given position."""
173173
while pos < len(self.src):
174-
if self.srcCharCode[pos] != code:
174+
if self.srcCharCodeAt(pos) != code:
175175
break
176176
pos += 1
177177
return pos
@@ -182,7 +182,7 @@ def skipCharsBack(self, pos: int, code: int, minimum: int) -> int:
182182
return pos
183183
while pos > minimum:
184184
pos -= 1
185-
if code != self.srcCharCode[pos]:
185+
if code != self.srcCharCodeAt(pos):
186186
return pos + 1
187187
return pos
188188

@@ -204,7 +204,7 @@ def getLines(self, begin: int, end: int, indent: int, keepLastLF: bool) -> str:
204204
last = self.eMarks[line]
205205

206206
while (first < last) and (lineIndent < indent):
207-
ch = self.srcCharCode[first]
207+
ch = self.srcCharCodeAt(first)
208208
if isSpace(ch):
209209
if ch == 0x09:
210210
lineIndent += 4 - (lineIndent + self.bsCount[line]) % 4

markdown_it/rules_block/table.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,14 @@ def table(state: StateBlock, startLine: int, endLine: int, silent: bool):
7171
pos = state.bMarks[nextLine] + state.tShift[nextLine]
7272
if pos >= state.eMarks[nextLine]:
7373
return False
74-
first_ch = state.srcCharCode[pos]
74+
first_ch = state.srcCharCodeAt(pos)
7575
pos += 1
7676
if first_ch not in {0x7C, 0x2D, 0x3A}: # not in {"|", "-", ":"}
7777
return False
7878

7979
if pos >= state.eMarks[nextLine]:
8080
return False
81-
second_ch = state.srcCharCode[pos]
81+
second_ch = state.srcCharCodeAt(pos)
8282
pos += 1
8383
# not in {"|", "-", ":"} and not space
8484
if second_ch not in {0x7C, 0x2D, 0x3A} and not isSpace(second_ch):
@@ -90,7 +90,7 @@ def table(state: StateBlock, startLine: int, endLine: int, silent: bool):
9090
return False
9191

9292
while pos < state.eMarks[nextLine]:
93-
ch = state.srcCharCode[pos]
93+
ch = state.srcCharCodeAt(pos)
9494

9595
# /* | */ /* - */ /* : */
9696
if ch not in {0x7C, 0x2D, 0x3A} and not isSpace(ch):

markdown_it/rules_inline/autolink.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def autolink(state: StateInline, silent: bool) -> bool:
1212

1313
pos = state.pos
1414

15-
if state.srcCharCode[pos] != 0x3C: # /* < */
15+
if state.srcCharCodeAt(pos) != 0x3C: # /* < */
1616
return False
1717

1818
start = state.pos
@@ -23,7 +23,7 @@ def autolink(state: StateInline, silent: bool) -> bool:
2323
if pos >= maximum:
2424
return False
2525

26-
ch = state.srcCharCode[pos]
26+
ch = state.srcCharCodeAt(pos)
2727

2828
if ch == 0x3C: # /* < */
2929
return False

markdown_it/rules_inline/backticks.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
def backtick(state: StateInline, silent: bool) -> bool:
1010

1111
pos = state.pos
12-
ch = state.srcCharCode[pos]
12+
ch = state.srcCharCodeAt(pos)
1313

1414
# /* ` */
1515
if ch != 0x60:
@@ -20,7 +20,7 @@ def backtick(state: StateInline, silent: bool) -> bool:
2020
maximum = state.posMax
2121

2222
# scan marker length
23-
while pos < maximum and (state.srcCharCode[pos] == 0x60): # /* ` */
23+
while pos < maximum and (state.srcCharCodeAt(pos) == 0x60): # /* ` */
2424
pos += 1
2525

2626
marker = state.src[start:pos]
@@ -43,7 +43,7 @@ def backtick(state: StateInline, silent: bool) -> bool:
4343
matchEnd = matchStart + 1
4444

4545
# scan marker length
46-
while matchEnd < maximum and (state.srcCharCode[matchEnd] == 0x60): # /* ` */
46+
while matchEnd < maximum and (state.srcCharCodeAt(matchEnd) == 0x60): # /* ` */
4747
matchEnd += 1
4848

4949
closerLength = matchEnd - matchStart

markdown_it/rules_inline/emphasis.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
def tokenize(state: StateInline, silent: bool):
88
"""Insert each marker as a separate text token, and add it to delimiter list"""
99
start = state.pos
10-
marker = state.srcCharCode[start]
10+
marker = state.srcCharCodeAt(start)
1111

1212
if silent:
1313
return False

markdown_it/rules_inline/entity.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ def entity(state: StateInline, silent: bool):
1414
pos = state.pos
1515
maximum = state.posMax
1616

17-
if state.srcCharCode[pos] != 0x26: # /* & */
17+
if state.srcCharCodeAt(pos) != 0x26: # /* & */
1818
return False
1919

2020
if (pos + 1) < maximum:
21-
ch = state.srcCharCode[pos + 1]
21+
ch = state.srcCharCodeAt(pos + 1)
2222

2323
if ch == 0x23: # /* # */
2424
match = DIGITAL_RE.search(state.src[pos:])

0 commit comments

Comments
 (0)