Skip to content

Commit d026370

Browse files
Fix a few regex inefficiencies (#1326)
Explanations: * `.*?$` is the same as `.*`: Being lazy at the end is the same as being greedy. * `[^\-]` is the same as `[^-]`: The `-` here cannot be considered a part of a range. * The dot in `user.(name|email)` acts as a metacharacter. * `(?:(via)|(?:via (.*?))|(?: (.*?)))$` is equivalent to `(?:via$|via (.*)| (.*))`. Consequently, a line below was changed from `.get(2).or_else(|| m.get(3))` to `.get(1).or_else(|| m.get(2))`.
1 parent 6cd803e commit d026370

File tree

6 files changed

+13
-9
lines changed

6 files changed

+13
-9
lines changed

rye-devtools/src/rye_devtools/find_uv_downloads.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class UvDownloads:
4848
"pc-windows-msvc": ("windows", None),
4949
}
5050

51-
RE = re.compile(r"uv-(?P<arch>[^\-]+)-(?P<plat_env>.+)(\.tar\.gz|\.zip)$")
51+
RE = re.compile(r"uv-(?P<arch>[^-]+)-(?P<plat_env>.+)(\.tar\.gz|\.zip)$")
5252

5353
def __init__(self, client: httpx.Client) -> None:
5454
self.client = client

rye/src/installer.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ while to_resolve:
6969
print(json.dumps(result))
7070
"#;
7171
static SUCCESSFULLY_DOWNLOADED_RE: Lazy<Regex> =
72-
Lazy::new(|| Regex::new("(?m)^Successfully downloaded (.*?)$").unwrap());
72+
Lazy::new(|| Regex::new("(?m)^Successfully downloaded (.*)").unwrap());
7373

7474
#[derive(Eq, PartialEq)]
7575
pub struct ToolInfo {

rye/src/lock.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use crate::uv::{UvBuilder, UvPackageUpgrade};
2525

2626
static FILE_EDITABLE_RE: Lazy<Regex> = Lazy::new(|| Regex::new(r"^-e (file://.*?)\s*$").unwrap());
2727
static DEP_COMMENT_RE: Lazy<Regex> =
28-
Lazy::new(|| Regex::new(r"^ # (?:(via)|(?:via (.*?))|(?: (.*?)))$").unwrap());
28+
Lazy::new(|| Regex::new(r"^ # (?:via$|via (.*)| (.*))").unwrap());
2929
static REQUIREMENTS_HEADER: &str = r#"# generated by rye
3030
# use `rye lock` or `rye sync` to update this lockfile
3131
#
@@ -39,7 +39,7 @@ static REQUIREMENTS_HEADER: &str = r#"# generated by rye
3939
4040
"#;
4141
static PARAM_RE: Lazy<Regex> = Lazy::new(|| {
42-
Regex::new(r"^# (pre|features|all-features|with-sources|universal):\s*(.*?)$").unwrap()
42+
Regex::new(r"^# (pre|features|all-features|with-sources|universal):\s*(.*)").unwrap()
4343
});
4444

4545
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
@@ -510,7 +510,7 @@ fn finalize_lockfile(
510510
continue;
511511
}
512512
} else if let Some(m) = DEP_COMMENT_RE.captures(line) {
513-
if let Some(dep) = m.get(2).or_else(|| m.get(3)).map(|x| x.as_str()) {
513+
if let Some(dep) = m.get(1).or_else(|| m.get(2)).map(|x| x.as_str()) {
514514
if !dep.starts_with("-r ") {
515515
// we cannot tell today based on the output where this comes from. This
516516
// can show up because it's a root dependency, because it's a dev dependency

rye/src/platform.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ pub fn get_default_author_with_fallback(dir: &PathBuf) -> Option<(String, String
187187
.arg("config")
188188
.arg("--get-regexp")
189189
.current_dir(dir)
190-
.arg("^user.(name|email)$")
190+
.arg(r"^user\.(name|email)$")
191191
.stdout(Stdio::piped())
192192
.output()
193193
{

rye/tests/.test-list.rs.pending-snap

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{"run_id":"1725330156-141805000","line":18,"new":{"module_name":"test_list","snapshot_name":"basic_list","metadata":{"source":"rye/tests/test-list.rs","assertion_line":18,"info":{"program":"rye","args":["list"],"env":{"__RYE_UV_EXCLUDE_NEWER":"2023-11-18T12:00:00Z","RYE_HOME":"/Users/crmarsh/workspace/rye/target/debug/rye-test-dir/home","UV_CACHE_DIR":"/var/folders/nt/6gf2v7_s3k13zq_t3944rwz40000gn/T/.rye-tests---5aZXcS/uv-cache"}}},"snapshot":"success: true\nexit_code: 0\n----- stdout -----\n\n----- stderr -----\n"},"old":{"module_name":"test_list","metadata":{},"snapshot":"success: true\nexit_code: 0\n----- stdout -----\njinja2==3.1.2\nmarkupsafe==2.1.3\n-e file:[TEMP_PATH]/project\n\n----- stderr -----"}}
2+
{"run_id":"1725330156-141805000","line":47,"new":null,"old":null}
3+
{"run_id":"1725330197-789961000","line":47,"new":null,"old":null}
4+
{"run_id":"1725330197-789961000","line":18,"new":null,"old":null}

rye/tests/test_self.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ fn test_version() {
5151
let _guard = space.lock_rye_home();
5252

5353
let mut settings = Settings::clone_current();
54-
settings.add_filter(r"(?m)^(rye )\d+\.\d+\.\d+?$", "$1[VERSION]");
55-
settings.add_filter(r"(?m)^(commit: ).*?$", "$1[COMMIT]");
56-
settings.add_filter(r"(?m)^(platform: ).*?$", "$1[PLATFORM]");
54+
settings.add_filter(r"(?m)^(rye )\d+\.\d+\.\d+$", "$1[VERSION]");
55+
settings.add_filter(r"(?m)^(commit: ).*", "$1[COMMIT]");
56+
settings.add_filter(r"(?m)^(platform: ).*", "$1[PLATFORM]");
5757
let _guard = settings.bind_to_scope();
5858

5959
rye_cmd_snapshot!(space.rye_cmd().arg("--version"), @r###"

0 commit comments

Comments
 (0)