|
14 | 14 | # all ASCII chars that may be the first char of an identifier |
15 | 15 | _ASCII_ID_FIRST_CHARS = frozenset(string.ascii_letters + "_") |
16 | 16 |
|
17 | | -# lookup table for whether 7-bit ASCII chars are valid in a Python identifier |
18 | | -_IS_ASCII_ID_CHAR = [(chr(x) in _ASCII_ID_CHARS) for x in range(128)] |
19 | | -# lookup table for whether 7-bit ASCII chars are valid as the first |
20 | | -# char in a Python identifier |
21 | | -_IS_ASCII_ID_FIRST_CHAR = \ |
22 | | - [(chr(x) in _ASCII_ID_FIRST_CHARS) for x in range(128)] |
23 | | - |
24 | 17 |
|
25 | 18 | class HyperParser: |
26 | 19 | def __init__(self, editwin, index): |
@@ -166,53 +159,47 @@ def _eat_identifier(cls, str, limit, pos): |
166 | 159 |
|
167 | 160 | This ignores non-identifier eywords are not identifiers. |
168 | 161 | """ |
169 | | - is_ascii_id_char = _IS_ASCII_ID_CHAR |
170 | | - |
171 | 162 | # Start at the end (pos) and work backwards. |
172 | 163 | i = pos |
173 | 164 |
|
174 | 165 | # Go backwards as long as the characters are valid ASCII |
175 | 166 | # identifier characters. This is an optimization, since it |
176 | 167 | # is faster in the common case where most of the characters |
177 | 168 | # are ASCII. |
178 | | - while i > limit and ( |
179 | | - ord(str[i - 1]) < 128 and |
180 | | - is_ascii_id_char[ord(str[i - 1])] |
181 | | - ): |
| 169 | + while i > limit and str[i - 1] in _ASCII_ID_CHARS: |
182 | 170 | i -= 1 |
183 | 171 |
|
184 | 172 | # If the above loop ended due to reaching a non-ASCII |
185 | 173 | # character, continue going backwards using the most generic |
186 | 174 | # test for whether a string contains only valid identifier |
187 | 175 | # characters. |
188 | | - if i > limit and ord(str[i - 1]) >= 128: |
189 | | - while i - 4 >= limit and ('a' + str[i - 4:pos]).isidentifier(): |
| 176 | + if i > limit and str[i - 1] > '\x7f': |
| 177 | + while i - 4 >= limit and ('a' + str[i - 4:i]).isidentifier(): |
190 | 178 | i -= 4 |
191 | | - if i - 2 >= limit and ('a' + str[i - 2:pos]).isidentifier(): |
| 179 | + if i - 2 >= limit and ('a' + str[i - 2:i]).isidentifier(): |
192 | 180 | i -= 2 |
193 | | - if i - 1 >= limit and ('a' + str[i - 1:pos]).isidentifier(): |
| 181 | + if i - 1 >= limit and ('a' + str[i - 1]).isidentifier(): |
194 | 182 | i -= 1 |
195 | 183 |
|
196 | 184 | # The identifier candidate starts here. If it isn't a valid |
197 | 185 | # identifier, don't eat anything. At this point that is only |
198 | 186 | # possible if the first character isn't a valid first |
199 | 187 | # character for an identifier. |
200 | | - if not str[i:pos].isidentifier(): |
| 188 | + if i < pos and not str[i].isidentifier(): |
201 | 189 | return 0 |
202 | 190 | elif i < pos: |
203 | 191 | # All characters in str[i:pos] are valid ASCII identifier |
204 | 192 | # characters, so it is enough to check that the first is |
205 | 193 | # valid as the first character of an identifier. |
206 | | - if not _IS_ASCII_ID_FIRST_CHAR[ord(str[i])]: |
| 194 | + if str[i] not in _ASCII_ID_FIRST_CHARS: |
207 | 195 | return 0 |
208 | 196 |
|
209 | 197 | # All keywords are valid identifiers, but should not be |
210 | 198 | # considered identifiers here, except for True, False and None. |
211 | | - if i < pos and ( |
212 | | - iskeyword(str[i:pos]) and |
213 | | - str[i:pos] not in cls._ID_KEYWORDS |
214 | | - ): |
215 | | - return 0 |
| 199 | + if i < pos: |
| 200 | + word = str[i:pos] |
| 201 | + if iskeyword(word) and word not in cls._ID_KEYWORDS: |
| 202 | + return 0 |
216 | 203 |
|
217 | 204 | return pos - i |
218 | 205 |
|
|
0 commit comments