This commit is contained in:
Zengtudor 2024-09-17 00:35:18 +08:00
parent ae4f499db1
commit eb85086657
3 changed files with 88 additions and 41 deletions

View File

@ -1,47 +1,42 @@
#include <iostream> #include <cstddef>
#include<sstream> #include<tools.hpp>
#include <string_view>
template<typename ...Args> void print_help(){
void print(const Args&...args){ zt::print("if you want help please add \"-h\"\n");
std::stringstream ss;
((ss<<args),...);
std::cout<<ss.str();
return;
} }
template<typename T>
class Range{
private:
T _start,_end,_step;
public:
Range(const T start,const T end){Range(start,end,1);}
Range(const T start,const T end,const T step):_start(start),_end(end),_step(step){}
struct Iterator{
T current;
T step;
Iterator()=delete;
Iterator(const T start)noexcept{Iterator(start,1);}
Iterator(const T start,const T step)noexcept:current(start),step(step){}
bool operator!=(const Iterator &other)const noexcept{
return current<other.current;
}
Iterator& operator++()noexcept{
current+=step;
return *this;
}
T operator*(){
return current;
}
};
Iterator begin()const noexcept{return Iterator(_start,_step);}
Iterator end()const noexcept{return Iterator(_end,_step);}
};
int main(const int argc,const char *argv[]){ int main(const int argc,const char *argv[]){
for(auto i:Range(1,11)){ std::string argv1(argv[1]);
print(i," "); 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()){
goto end;
}
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();
} }
print("\n");
zt::print(argv1,"\n");
end:;
} }

50
src/tools/tools.hpp Normal file
View File

@ -0,0 +1,50 @@
#pragma once
#include <iostream>
#include <sstream>
#define NAME_VALUE(v)#v," : ",(v)
namespace zt {
template<typename T>
class Range{
private:
T _start,_end,_step;
public:
// constexpr Range()=delete;
constexpr Range(const T start,const T end,const T step)noexcept:_start(start),_end(end),_step(step){}
constexpr Range(const T start,const T end)noexcept:Range(start,end,1){}
constexpr Range(const T end)noexcept:Range(1,end,1){}
struct Iterator{
T current,step;
constexpr Iterator()=delete;
constexpr Iterator(const T start,const T step)noexcept:current(start),step(step){}
constexpr bool operator!=(const Iterator &other)const noexcept{
return current<other.current;
}
constexpr Iterator& operator++()noexcept{
current+=step;
return *this;
}
constexpr T operator*()const noexcept{
return current;
}
};
constexpr Iterator begin(){
return Iterator(_start,_step);
}
constexpr Iterator end(){
return Iterator(_end,_step);
}
};
template<typename ...Args>
void print(const Args&...args){
std::stringstream ss;
((ss<<args),...);
std::cout<<ss.str();
return;
}
}

View File

@ -4,6 +4,8 @@ set_languages("c++23")
set_warnings("all") set_warnings("all")
add_includedirs("src/tools")
target("replaceStr") target("replaceStr")
set_kind("binary") set_kind("binary")
add_files("./src/replaceStr/*.cpp") add_files("./src/replaceStr/*.cpp")