differsOnlyInWhitespace reports whether oldString matches content at exactly one spot once every whitespace run is collapsed: i.e. the sole mismatch is indentation/tabs/newlines. Bounded with spaces so a match can't straddle a token boundary and mislabel an unrelated near-miss.
(content, oldString string)
| 66 | // so the exactly-once guarantee holds. |
| 67 | if idx := strings.Index(content, oldString); strings.Contains(content[idx+1:], oldString) { |
| 68 | return "(ambiguous: old_string overlaps itself - provide more context to make it unique)" |
| 69 | } |
| 70 | updated := strings.Replace(content, oldString, newString, 1) |
| 71 | if err := os.WriteFile(path, []byte(updated), 0o644); err != nil { |
| 72 | return fmt.Sprintf("(write error: %v)", err) |
| 73 | } |
| 74 | return fmt.Sprintf("edited %s: -%d +%d bytes", path, len(oldString), len(newString)) |