Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/stdlib/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,12 @@ pub fn native_replace(args: Vec<Value>) -> Result<Value, RuntimeError> {
let text = expect_text(&args[0])?;
let old = expect_text(&args[1])?;
let new = expect_text(&args[2])?;

// Optimization: avoid string allocation if string doesn't contain old
if !text.contains(old.as_ref()) {
return Ok(Value::Text(Arc::clone(&text)));
}
Comment on lines +283 to +286
Comment on lines +282 to +286

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion | 🟠 Major | ⚡ Quick win

Add test coverage for the optimization path.

The optimization correctly avoids allocation when the pattern is not found, but there's no test case covering this path. Consider adding a test to verify the behavior when old is not present in text.

🧪 Suggested test case

Add this test to the tests module:

#[test]
fn test_replace_not_found() {
    let result = native_replace(vec![
        Value::Text(Arc::from("hello world")),
        Value::Text(Arc::from("xyz")),
        Value::Text(Arc::from("rust")),
    ])
    .unwrap();
    assert_eq!(result, Value::Text(Arc::from("hello world")));
}
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/stdlib/text.rs` around lines 282 - 286, Add a unit test that exercises
the optimization path in native_replace by verifying it returns the original Arc
text when the `old` pattern isn't present: create a test named
`test_replace_not_found` in the tests module that calls `native_replace` with
Value::Text(Arc::from("hello world")) as the text and a non-existent pattern
like Value::Text(Arc::from("xyz")), unwraps the result, and asserts it equals
Value::Text(Arc::from("hello world"))); this ensures the early-return branch
(the contains check in native_replace) is covered.


let result = text.replace(old.as_ref(), new.as_ref());
Ok(Value::Text(Arc::from(result)))
}
Expand Down
Loading