diff --git a/src/replaceStr/main.cpp b/src/replaceStr/main.cpp deleted file mode 100644 index 645d2be..0000000 --- a/src/replaceStr/main.cpp +++ /dev/null @@ -1,45 +0,0 @@ -#include -#include - -void print_help(){ - zt::print("if you want help please add \"-h\"\n"); -} - -int main(const int argc,const char *argv[]){ - if(argc==1){ - print_help(); - exit(-1); - } - - std::string argv1(argv[1]); - if(argc==2){ - if(argv1=="-h"){ - zt::print("help:\n","\tThis is a simple string replacer to replace characters of the same length.\n","\t \n","\t\t-By Zengtudor\n"); - }else{ - print_help(); - } - }else if(argc==4){ - std::string_view be_replaced(argv[2]),to_replace(argv[3]); - if(be_replaced.size()!=to_replace.size()){ - zt::eprint("The length of last two characters are not same!\n"); - exit(-1); - } - for(auto i: zt::Range((size_t)0,argv1.size())){ - if(argv1[i]==be_replaced[0]){ - for(auto j: zt::Range((size_t)0,be_replaced.size())){ - if(argv1[i+j]!=be_replaced[j]){ - goto out_of_match; - } - } - for(auto j:zt::Range((size_t)0,be_replaced.size())){ - argv1[i+j]=to_replace[j]; - } - } - out_of_match:; - } - }else{ - print_help(); - } - - zt::print(argv1,"\n"); -} \ No newline at end of file diff --git a/src/repstr/main.cpp b/src/repstr/main.cpp new file mode 100644 index 0000000..129ca8d --- /dev/null +++ b/src/repstr/main.cpp @@ -0,0 +1,50 @@ +#include +#include +#include + +void print_help_exit(){ + throw std::runtime_error("if you want help please add \"-h\"\n"); +} + +int main(const int argc,const char *argv[]){ + try{ + if(argc==1){ + print_help_exit(); + } + + std::string argv1(argv[1]); + if(argc==2){ + if(argv1=="-h"){ + zt::print("help:\n","\tThis is a simple string replacer to replace characters of the same length.\n","\t \n","\t\t-By Zengtudor\n"); + return 0; + }else{ + print_help_exit(); + } + }else if(argc==4){ + std::string_view be_replaced(argv[2]),to_replace(argv[3]); + if(be_replaced.size()!=to_replace.size()){ + throw std::runtime_error("The length of last two characters are not same!\n"); + } + for(auto i: zt::Range((size_t)0,argv1.size())){ + if(argv1[i]==be_replaced[0]){ + for(auto j: zt::Range((size_t)0,be_replaced.size())){ + if(argv1[i+j]!=be_replaced[j]){ + goto out_of_match; + } + } + for(auto j:zt::Range((size_t)0,be_replaced.size())){ + argv1[i+j]=to_replace[j]; + } + } + out_of_match:; + } + }else{ + print_help_exit(); + } + + zt::print(argv1,"\n"); + + }catch(std::runtime_error &e){ + zt::eprint("caught an error because:\n","\te.what(): ",e.what(),"\n"); + } +} \ No newline at end of file diff --git a/xmake.lua b/xmake.lua index d295fb9..bcc6f3e 100644 --- a/xmake.lua +++ b/xmake.lua @@ -6,6 +6,6 @@ set_warnings("all") add_includedirs("src/tools") -target("replaceStr") +target("repstr") set_kind("binary") - add_files("./src/replaceStr/*.cpp") \ No newline at end of file + add_files("./src/repstr/*.cpp") \ No newline at end of file