diff --git a/main.cpp b/main.cpp index 24c8d87..c231b29 100644 --- a/main.cpp +++ b/main.cpp @@ -253,7 +253,7 @@ void jump(position p) { } // Find next string appearance -position find(string text) { +std::pair 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':