beetlebug▾
/

ztcl::code replace

Changes the one place of a text in a file.

ztcl::code replace FILE OLD NEW ?-all?

Description

OLD must be at exactly one place of FILE, unless -all, which changes each place. A text at more than one place gives the error AMBIGUOUS with the lines of the places. A text that is not in the file gives the error NOMATCH with the lines that have the text with other white space, or with the first line of OLD that differs from the file.

The file is read and written as UTF-8 with no change of its line ends. A binary file gives the error BINARY.

Returns

A line FILE:A-B +ADDED -REMOVED for each change.

Examples

Each example of this page runs after package require ztcl::code # A folder with three files, outside a git tree. set dir /tmp/ztcl-ref-code-[pid] file delete -force $dir file mkdir $dir cd $dir proc put {path text} { set ch [open $path w] puts -nonewline $ch $text close $ch } put a.zig "const std = @import(\"std\");\n\npub const Point = struct {\n x: i32,\n\n pub fn len(p: Point) i32 {\n return p.x;\n }\n};\n" put b.txt "one\ntwo\ntwo\n" put c.md "# Title\n\n## Part\n\ntext\n" # The tests use no model. set ::ztcl::code::backend {http ""} set ::ztcl::code::llm_url http://127.0.0.1:9/v1/chat/completions .

% ztcl::code replace a.zig "x: i32," "x: i64,"
a.zig:4 +1 -1

% ztcl::code replace b.txt two 2 -all
b.txt:2 +1 -1
b.txt:3 +1 -1

Errors

% put d.txt "a\na\n"
> ztcl::code replace d.txt a b
the text is at 2 places of d.txt: lines 1, 2. Give more text, or use -all.
-errorcode ZTCL CODE AMBIGUOUS

% ztcl::code replace a.zig "y: i32," x
the text is not in a.zig, and no line of it is in a.zig
-errorcode ZTCL CODE NOMATCH

Facts

Capability
file. A new interpreter does not have it until the host turns it on.
Tcl release
Tcl 9.1 and 8.6.
Since
ztcl 1.0.0-beta.2