Skip to content

Commit ca61761

Browse files
authored
Fix IndexError in isUri (#287)
1 parent a435038 commit ca61761

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

protovalidate/internal/extra_func.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -931,7 +931,7 @@ def __scheme(self) -> bool:
931931
while self.__alpha() or self.__digit() or self.__take("+") or self.__take("-") or self.__take("."):
932932
pass
933933

934-
if self._string[self._index] == ":":
934+
if self.__peek(":"):
935935
return True
936936

937937
self._index = start
@@ -997,9 +997,8 @@ def __userinfo(self) -> bool:
997997
while self.__unreserved() or self.__pct_encoded() or self.__sub_delims() or self.__take(":"):
998998
pass
999999

1000-
if self._index < len(self._string):
1001-
if self._string[self._index] == "@":
1002-
return True
1000+
if self.__peek("@"):
1001+
return True
10031002

10041003
self._index = start
10051004
return False
@@ -1023,14 +1022,11 @@ def __host(self) -> bool:
10231022
host = IP-literal / IPv4address / reg-name.
10241023
10251024
"""
1026-
if self._index >= len(self._string):
1027-
return False
1028-
10291025
start = self._index
10301026
self._pct_encoded_found = False
10311027

10321028
# Note: IPv4address is a subset of reg-name
1033-
if (self._string[self._index] == "[" and self.__ip_literal()) or self.__reg_name():
1029+
if (self.__peek("[") and self.__ip_literal()) or self.__reg_name():
10341030
if self._pct_encoded_found:
10351031
raw_host = self._string[start : self._index]
10361032
# RFC 3986:
@@ -1188,7 +1184,7 @@ def __reg_name(self) -> bool:
11881184
# End of authority
11891185
return True
11901186

1191-
if self._string[self._index] == ":":
1187+
if self.__peek(":"):
11921188
return True
11931189

11941190
self._index = start
@@ -1380,7 +1376,7 @@ def __query(self) -> bool:
13801376
while self.__pchar() or self.__take("/") or self.__take("?"):
13811377
pass
13821378

1383-
if self._index == len(self._string) or self._string[self._index] == "#":
1379+
if self._index == len(self._string) or self.__peek("#"):
13841380
return True
13851381

13861382
self._index = start
@@ -1537,6 +1533,9 @@ def __take(self, char: str) -> bool:
15371533

15381534
return False
15391535

1536+
def __peek(self, char: str) -> bool:
1537+
return self._index < len(self._string) and self._string[self._index] == char
1538+
15401539

15411540
def make_extra_funcs(locale: str) -> dict[str, celpy.CELFunction]:
15421541
# TODO(#257): Fix types and add tests for StringFormat.

0 commit comments

Comments
 (0)