From 5232b8fb3b7e08136c38c8fb2b1c4970403d7e58 Mon Sep 17 00:00:00 2001 From: Thomas Lindop Date: Wed, 28 May 2025 00:32:08 +0100 Subject: [PATCH] >> add basic search and replace --- main.go | 39 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index 32ce07c..5a021d9 100644 --- a/main.go +++ b/main.go @@ -40,6 +40,16 @@ import ( */ type runeset [][2]rune +/** + * A basic search-and-replace operation. Replaces each instance of "Target" + * with each member of "Subs" in order, with all instances beyond + * length(Subs) being replaced by the last member of Subs. + */ +type replacement struct { + Target string + Subs []string +} + // Unrelated to the standard library's "context". type context struct { FileList []string @@ -50,6 +60,7 @@ type context struct { DoVersion bool Rset runeset TruncLen int + Replacements []replacement } @@ -207,6 +218,16 @@ func (ctx *context) truncate(name string) string { return name } +func (ctx *context) searchAndReplace(name string) string { + for _, rep := range ctx.Replacements { + for i:=0; i>) for replacement: please use '{TARGET}:{REPLACEMNT1},{REPLACEMNT2},...'", + args[index], + )) + } + result.Replacements = append( + result.Replacements, + replacement{ target, strings.Split(subs, ",") }, + ) case "-t", "--truncate": index++ var err error @@ -321,9 +356,9 @@ func main() { // Calculate the new name oldName := fpath.Base(file) dirName := fpath.Dir(file) - newName := ctx.truncate(ctx.restrictRuneset( + newName := ctx.truncate(ctx.restrictRuneset(ctx.searchAndReplace( strings.ToValidUTF8(oldName, "_INVALID_"), - )) + ))) newPath := fpath.Join(dirName, newName) // Check that we want to rename this file if newPath == file {