applyEdit applies a single edit to the content and returns the modified content and result.
(content []byte, edit EditSpec, index int)
| 261 | |
| 262 | // applyEdit applies a single edit to the content and returns the modified content and result. |
| 263 | func applyEdit(content []byte, edit EditSpec, index int) ([]byte, EditResult) { |
| 264 | result := EditResult{ |
| 265 | Desc: edit.Desc, |
| 266 | } |
| 267 | if result.Desc == "" { |
| 268 | result.Desc = fmt.Sprintf("Edit %d", index+1) |
| 269 | } |
| 270 | |
| 271 | if edit.OldStr == "" { |
| 272 | result.Applied = false |
| 273 | result.Error = "old_str cannot be empty" |
| 274 | return content, result |
| 275 | } |
| 276 | |
| 277 | oldBytes := []byte(edit.OldStr) |
| 278 | count := bytes.Count(content, oldBytes) |
| 279 | if count == 0 { |
| 280 | result.Applied = false |
| 281 | result.Error = "old_str not found in file" |
| 282 | return content, result |
| 283 | } |
| 284 | if count > 1 { |
| 285 | result.Applied = false |
| 286 | result.Error = fmt.Sprintf("old_str appears %d times, must appear exactly once", count) |
| 287 | return content, result |
| 288 | } |
| 289 | |
| 290 | modifiedContent := bytes.Replace(content, oldBytes, []byte(edit.NewStr), 1) |
| 291 | result.Applied = true |
| 292 | return modifiedContent, result |
| 293 | } |
| 294 | |
| 295 | // ApplyEdits applies a series of edits to the given content and returns the modified content. |
| 296 | // This is atomic - all edits succeed or all fail. |
no outgoing calls
no test coverage detected