update
This commit is contained in:
parent
ae4f499db1
commit
ce51439acb
@ -1,47 +1,47 @@
|
||||
#include <iostream>
|
||||
#include<sstream>
|
||||
#include <cstddef>
|
||||
#include <cstdlib>
|
||||
#include<tools.hpp>
|
||||
#include <string_view>
|
||||
|
||||
template<typename ...Args>
|
||||
void print(const Args&...args){
|
||||
std::stringstream ss;
|
||||
((ss<<args),...);
|
||||
std::cout<<ss.str();
|
||||
return;
|
||||
void print_help(){
|
||||
zt::print("if you want help please add \"-h\"\n");
|
||||
}
|
||||
|
||||
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[]){
|
||||
for(auto i:Range(1,11)){
|
||||
print(i," ");
|
||||
if(argc==1){
|
||||
print_help();
|
||||
exit(-1);
|
||||
}
|
||||
print("\n");
|
||||
|
||||
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");
|
||||
}
|
63
src/tools/tools.hpp
Normal file
63
src/tools/tools.hpp
Normal file
@ -0,0 +1,63 @@
|
||||
#pragma once
|
||||
|
||||
#include <iostream>
|
||||
#include <ostream>
|
||||
#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>
|
||||
inline void print(std::ostream &os,const Args&...args){
|
||||
std::stringstream ss;
|
||||
((ss<<args),...);
|
||||
os<<ss.str();
|
||||
return;
|
||||
}
|
||||
|
||||
template<typename ...Args>
|
||||
inline void print(const Args&...args){
|
||||
print(std::cin,args...);
|
||||
return;
|
||||
}
|
||||
|
||||
template<typename ...Args>
|
||||
inline void eprint(const Args&...args){
|
||||
print(std::cerr,args...);
|
||||
return;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user