Implement find and replace
This commit is contained in:
parent
3b872f4ad7
commit
0497e5993d
1 changed files with 36 additions and 0 deletions
36
main.cpp
36
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);
|
||||
|
|
Loading…
Reference in a new issue