Fix crash by searching for non-contained string

This commit is contained in:
Matúš Púll 2025-04-17 20:59:57 +02:00
parent 0497e5993d
commit 8e29aad9e0

View file

@ -253,7 +253,7 @@ void jump(position p) {
}
// Find next string appearance
position find(string text) {
std::pair<bool, position> find(string text) {
count_type len = text.size();
count_type file_size = file.size();
@ -263,9 +263,9 @@ position find(string text) {
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 {true,{r, pos}};
}
return cur;
return {false, position()};
}
// Replace string appearance
void replace(position p, count_type length, string text) {
@ -333,15 +333,29 @@ int main(int argc, char* argv[]) {
case 'f':
last_find = get_string("to find");
case 'n':
jump(find(last_find));
{
auto result = find(last_find);
if(!result.first) {
print_line(file_offset + cur.r);
break;
}
jump(result.second);
}
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);
{
auto result = find(last_find);
if(!result.first) {
print_line(file_offset + cur.r);
break;
}
jump(result.second);
replace(cur + file_offset, last_find.size(), last_replace);
}
print_file(file_offset + cur.r);
break;
case 'p':