write tool fixes
This commit is contained in:
@@ -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));
|
||||
|
||||
Reference in New Issue
Block a user