Skip to content

Commit cb8bd65

Browse files
committed
Parse each go.sum line once in zip_versions
Lines that pass the module prefilter are now parsed directly instead of going back through the multi-line reader. The equivalence test now also checks the editor after an edit, when it holds split lines. Refs #1424 Assisted-by: Claude Code:claude-opus-5-5
1 parent 1994b4d commit cb8bd65

1 file changed

Lines changed: 29 additions & 17 deletions

File tree

‎crates/socket-patch-core/src/vendor/go_sum_edit.rs‎

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ impl GoSumEditor {
304304
.strip_prefix(module)
305305
.is_some_and(|rest| rest.starts_with(char::is_whitespace))
306306
})
307-
.flat_map(go_sum_lines)
307+
.filter_map(parse_go_sum_line)
308308
.filter_map(|l| l.zip_version_of(module).map(str::to_string))
309309
.collect()
310310
}
@@ -397,20 +397,24 @@ impl<'a> GoSumLine<'a> {
397397
/// lines are skipped. Each caller keeps its own token rule
398398
/// ([`GoSumLine::extra_tokens`]) and hash filter.
399399
pub(crate) fn go_sum_lines(text: &str) -> impl Iterator<Item = GoSumLine<'_>> {
400-
text.lines().filter_map(|line| {
401-
let mut tokens = line.split_whitespace();
402-
let (module, version, hash) = (tokens.next()?, tokens.next()?, tokens.next()?);
403-
let (version, go_mod) = match version.strip_suffix("/go.mod") {
404-
Some(version) => (version, true),
405-
None => (version, false),
406-
};
407-
Some(GoSumLine {
408-
module,
409-
version,
410-
go_mod,
411-
hash,
412-
extra_tokens: tokens.next().is_some(),
413-
})
400+
text.lines().filter_map(parse_go_sum_line)
401+
}
402+
403+
/// One line of [`go_sum_lines`], or `None` when it has fewer than three
404+
/// tokens.
405+
fn parse_go_sum_line(line: &str) -> Option<GoSumLine<'_>> {
406+
let mut tokens = line.split_whitespace();
407+
let (module, version, hash) = (tokens.next()?, tokens.next()?, tokens.next()?);
408+
let (version, go_mod) = match version.strip_suffix("/go.mod") {
409+
Some(version) => (version, true),
410+
None => (version, false),
411+
};
412+
Some(GoSumLine {
413+
module,
414+
version,
415+
go_mod,
416+
hash,
417+
extra_tokens: tokens.next().is_some(),
414418
})
415419
}
416420

@@ -859,12 +863,20 @@ mod tests {
859863
// line (#1424): compare against `go_sum_lines` over odd spacing.
860864
let content =
861865
"m v1 h1:a\n m v2 h\n\u{a0}m v3 h\nm\u{a0}v4 h\nmm v5 h\nm v6/go.mod h\nm\tv7\th\n";
862-
let editor = GoSumEditor::new(content.to_string());
866+
let text = GoSumEditor::new(content.to_string());
867+
// An edit leaves the editor holding split lines; ask both forms.
868+
let mut lines = GoSumEditor::new(content.to_string());
869+
assert!(lines.upsert_module_lines(MOD, VER, ZIP_H1, GOMOD_H1));
863870
for module in ["m", "mm", "m\u{a0}v4", ""] {
864871
let reference: Vec<String> = go_sum_lines(content)
865872
.filter_map(|l| l.zip_version_of(module).map(str::to_string))
866873
.collect();
867-
assert_eq!(editor.zip_versions(module), reference, "module {module:?}");
874+
assert_eq!(text.zip_versions(module), reference, "module {module:?}");
875+
assert_eq!(
876+
lines.zip_versions(module),
877+
reference,
878+
"lines, module {module:?}"
879+
);
868880
}
869881
}
870882
}

0 commit comments

Comments
 (0)