From 0497e5993da8c57c0b89db54d8634f4f71b4d249 Mon Sep 17 00:00:00 2001 From: Matuush Date: Thu, 17 Apr 2025 18:48:22 +0200 Subject: [PATCH] Implement find and replace --- main.cpp | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/main.cpp b/main.cpp index 2ec0173..24c8d87 100644 --- a/main.cpp +++ b/main.cpp @@ -24,6 +24,7 @@ position cur = {0, 0}; count_type file_offset = 0; position clp; treap selection; +string last_find, last_replace; // Accessing the file string get_line(count_type r, bool substitute_tab = true) { @@ -251,6 +252,27 @@ void jump(position p) { cur.c = p.c; } +// Find next string appearance +position find(string text) { + count_type len = text.size(); + count_type file_size = file.size(); + + for(count_type _r = 0; _r < file_size; ++_r) { + count_type r = (file_offset + cur.r + _r) % file_size; + + count_type start = ((_r == 0) ? (cur.c+1) : 0); + count_type pos = get_line(r, 0).find(text, start); + if(pos != string::npos) + return {r, pos}; + } + return cur; +} +// Replace string appearance +void replace(position p, count_type length, string text) { + remove(p, length); + insert(p, text); +} + int main(int argc, char* argv[]) { // Check valid filename and load @@ -308,6 +330,20 @@ int main(int argc, char* argv[]) { mode = SELECT; clp = cur + file_offset; break; + case 'f': + last_find = get_string("to find"); + case 'n': + jump(find(last_find)); + print_file(file_offset + cur.r); + break; + case 's': + last_find = get_string("to find"); + last_replace = get_string("to replace"); + case 'r': + jump(find(last_find)); + replace(cur + file_offset, last_find.size(), last_replace); + print_file(file_offset + cur.r); + break; case 'p': paste_selection(cur + file_offset); print_file(file_offset + cur.r);