From ce51439acbb2f64ccc9830032e31fe28e08e85ee Mon Sep 17 00:00:00 2001 From: Zengtudor Date: Tue, 17 Sep 2024 00:35:18 +0800 Subject: [PATCH] update --- src/replaceStr/main.cpp | 82 ++++++++++++++++++++--------------------- src/tools/tools.hpp | 63 +++++++++++++++++++++++++++++++ xmake.lua | 2 + 3 files changed, 106 insertions(+), 41 deletions(-) create mode 100644 src/tools/tools.hpp diff --git a/src/replaceStr/main.cpp b/src/replaceStr/main.cpp index cd001da..774319e 100644 --- a/src/replaceStr/main.cpp +++ b/src/replaceStr/main.cpp @@ -1,47 +1,47 @@ -#include -#include +#include +#include +#include +#include -template -void print(const Args&...args){ - std::stringstream ss; - ((ss< -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 \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/tools/tools.hpp b/src/tools/tools.hpp new file mode 100644 index 0000000..2d0cf46 --- /dev/null +++ b/src/tools/tools.hpp @@ -0,0 +1,63 @@ +#pragma once + +#include +#include +#include + +#define NAME_VALUE(v)#v," : ",(v) + +namespace zt { + template + 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 + inline void print(std::ostream &os,const Args&...args){ + std::stringstream ss; + ((ss< + inline void print(const Args&...args){ + print(std::cin,args...); + return; + } + + template + inline void eprint(const Args&...args){ + print(std::cerr,args...); + return; + } +} \ No newline at end of file diff --git a/xmake.lua b/xmake.lua index 10a7398..d295fb9 100644 --- a/xmake.lua +++ b/xmake.lua @@ -4,6 +4,8 @@ set_languages("c++23") set_warnings("all") +add_includedirs("src/tools") + target("replaceStr") set_kind("binary") add_files("./src/replaceStr/*.cpp") \ No newline at end of file