This commit is contained in:
Zengtudor 2024-09-17 13:23:36 +08:00
parent d7ede1e333
commit 0b5338d157
3 changed files with 52 additions and 47 deletions

View File

@ -1,45 +0,0 @@
#include <tools.hpp>
#include <string_view>
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<string> <character to be replaced> <character to replace>\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");
}

50
src/repstr/main.cpp Normal file
View File

@ -0,0 +1,50 @@
#include <stdexcept>
#include <tools.hpp>
#include <string_view>
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<string> <character to be replaced> <character to replace>\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");
}
}

View File

@ -6,6 +6,6 @@ set_warnings("all")
add_includedirs("src/tools")
target("replaceStr")
target("repstr")
set_kind("binary")
add_files("./src/replaceStr/*.cpp")
add_files("./src/repstr/*.cpp")