write tool fixes

This commit is contained in:
Luxferre
2026-10-14 12:19:11 +03:00
parent 59c85e9217
commit 05bce7ca9c
4 changed files with 31 additions and 6 deletions
+7
View File
@@ -291,6 +291,13 @@ std::string write_file(std::string_view path, long line_offset, long byte_offset
nd.append(data, 0, prefix_len);
nd.append(content);
if (suffix_len) nd.append(data, end_del, suffix_len);
/* Keep a single trailing newline for line-based / whole-file text writes.
* Byte-based edits are exact byte operations and must not be mutated with an
* injected newline, so the guard is skipped whenever a byte offset or byte
* deletion is actually in effect. */
bool byte_based = (line_offset < 0 && byte_offset >= 0) ||
(del_lines < 0 && del_bytes >= 0);
if (!byte_based && !nd.empty() && nd.back() != '\n') nd.push_back('\n');
if (!write_file_str(p, nd)) {
*err = 1;
return sfmt("[tool error: write %s: %s]", p.c_str(), strerror(errno));