Skip to content

Commit 2d7cee3

Browse files
author
dalbee
committed
Preserve double-digit command placeholders
Command templates replace numbered placeholders with literal string replacement. Replacing in ascending order lets short placeholders such as $1 consume the prefix of $10 before the intended replacement runs, so templates with double-digit placeholders produce corrupted output. Constraint: Keep existing last-placeholder-captures-rest behavior unchanged Rejected: Regex replacement rewrite | larger change than needed for the prefix-collision bug Confidence: high Scope-risk: narrow Directive: Preserve descending replacement unless the placeholder parser is replaced with token-aware substitution Tested: Docker rust:latest cargo test -p cortex-commands test_substitute_two_digit_placeholders_before_prefixes -- --nocapture Tested: Docker rust:latest cargo test -p cortex-commands substitute -- --nocapture Tested: Docker rust:latest cargo check -p cortex-commands Tested: Docker rust:latest cargo fmt --check --package cortex-commands Tested: git diff --check
1 parent 7954d02 commit 2d7cee3

1 file changed

Lines changed: 13 additions & 2 deletions

File tree

src/cortex-commands/src/command.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,8 @@ pub fn substitute_placeholders(template: &str, arguments: &str) -> String {
209209
// Find the highest numbered placeholder
210210
let max_placeholder = find_max_placeholder(&result);
211211

212-
// Replace numbered placeholders
213-
for i in 1..=max_placeholder {
212+
// Replace longer placeholders first so `$1` does not corrupt `$10`.
213+
for i in (1..=max_placeholder).rev() {
214214
let placeholder = format!("${i}");
215215
let replacement = if i == max_placeholder {
216216
// Last placeholder captures all remaining arguments
@@ -374,6 +374,17 @@ No closing delimiter"#;
374374
assert_eq!(result, "A: only_one, B: , C: ");
375375
}
376376

377+
#[test]
378+
fn test_substitute_two_digit_placeholders_before_prefixes() {
379+
let template = "Run $1 and item $10";
380+
let result = substitute_placeholders(
381+
template,
382+
"alpha beta gamma delta epsilon zeta eta theta iota kappa",
383+
);
384+
385+
assert_eq!(result, "Run alpha and item kappa");
386+
}
387+
377388
#[test]
378389
fn test_command_parse() {
379390
let content = r#"---

0 commit comments

Comments
 (0)