From b77cc350f0548b64edca45a6b97bae84f5bd0fb6 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 9 Sep 2024 21:58:02 -0500 Subject: [PATCH] Small performance improvements to checking for chars in paths (#1143) --- CHANGES/1143.misc.rst | 1 + yarl/_url.py | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) create mode 100644 CHANGES/1143.misc.rst diff --git a/CHANGES/1143.misc.rst b/CHANGES/1143.misc.rst new file mode 100644 index 000000000..a3ac5b659 --- /dev/null +++ b/CHANGES/1143.misc.rst @@ -0,0 +1 @@ +Improved performance of processing paths -- by :user:`bdraco`. diff --git a/yarl/_url.py b/yarl/_url.py index ae9fc17cb..01f9d8fab 100644 --- a/yarl/_url.py +++ b/yarl/_url.py @@ -789,7 +789,7 @@ def raw_parts(self) -> Tuple[str, ...]: else: parts = ["/"] + path[1:].split("/") else: - if path.startswith("/"): + if path and path[0] == "/": parts = ["/"] + path[1:].split("/") else: parts = path.split("/") @@ -868,7 +868,7 @@ def _validate_authority_uri_abs_path(host: str, path: str) -> None: Raise ValueError if not. """ - if host and path and not path.startswith("/"): + if host and path and not path[0] == "/": raise ValueError( "Path in a URL with authority should start with a slash ('/') if set" ) @@ -1395,7 +1395,7 @@ def with_suffix(self, suffix: str) -> "URL": """ if not isinstance(suffix, str): raise TypeError("Invalid suffix type") - if suffix and not suffix.startswith(".") or suffix == ".": + if suffix and not suffix[0] == "." or suffix == ".": raise ValueError(f"Invalid suffix {suffix!r}") name = self.raw_name if not name: