跳到正文

个人记录

用 C++ 实现 Python 中的 str

Famishin约 22 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 22 分钟

版本

仅有当前版本

请用 C++ 11 及以上版本,否则不能通过编译

欢迎在评论区提出错误

之前的 list

2023/1/19:实现了 Python 3 中的大部分 str 的常用功能,暂不支持反向切片,splitness,split,format,decode,encode 函数未实现,isnumeric,isdigit,isdecimal 三个函数是一样的,replace 不支持第三个参数(是我懒得写),部分函数只不支持中文字符,查找函数用的是 KMP 算法。

C++
#include<cctype>
#include<stdio.h>
#include<string.h>
#include<malloc.h>
#define For(var,Range) for(int var=Range.st;var<Range.en;var+=Range.dx)
#define inf 2147483647
class range{
	public:
		int st,en,dx;
		range(const int a,const int b,const int c=1){st=a;en=b;dx=c;}
		range(const int b){st=0;en=b;dx=1;}
};
struct mt{
	char trans[128]{0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127};
	mt(const char* a,const char* b,int _sz){
		for(int i=0;i<_sz;++i)
			trans[a[i]]=b[i];
	}
};
class str;
bool operator==(str,str);
class mt;
void del(str&lst,const int pos);
class str{
	char* data=new char[0];
	int _sz=0;
	public:
		char* begin(){return data;}
		char* end(){return data+_sz;}
		friend char min(str);
		friend char max(str);
		friend int len(str);
		friend int _strcmp(str a,str b);
		str(){}
		str(const char c){data=new char[_sz=1];*data=c;}
		str(const char* str){
			data=new char[_sz=strlen(str)];
			memcpy(data,str,_sz);
			data[_sz]=0;
		}
		str(const char* str,const int sz){
			data=new char[_sz=sz];
			memcpy(data,str,_sz);
			data[_sz]=0;
		}
		str(const str&init_val){data=new char[_sz=init_val._sz];memcpy(data,init_val.data,_sz);}
		char& operator[](const int pos){return pos<0?data[_sz+pos]:data[pos];}
		str capitalize(){
			str ret=*this;
			ret[0]=toupper(ret[0]);
			for(int i=1;i<ret._sz;++i)
				ret[i]=tolower(ret[i]);
			return ret;
		}
		str center(const int width,const char x=' '){
			if(width<=_sz)return *this;
			const int _back=(width-_sz)/2;
			const int _front=_back+(width-_sz)%2;
			str s;
			s.data=new char[s._sz=_front];
			for(int i=0;i<s._sz;++i)s[i]=x;
			s+=*this;
			s.data=(char*)realloc(s.data,s._sz=width);
			for(int i=0;i<_back;++i)s[_front+_sz+i]=x;
			return s;
		}
		int count(str s){
			const int lent=_sz,lens=s._sz;
			static int* nex=(int*)calloc(lens,4);
			int i=0,j=-1,cnt=0;
			nex[0]=-1;
			while(i<lens)
				if(j==-1||s[i]==s[j])nex[++i]=++j;
				else j=nex[j];
			i=j=0;
			while(i<lent){
				if(j==-1||(*this)[i]==s[j])++i,++j;
				else j=nex[j];
				if(j==lens)j=0,++cnt;
			}
			return cnt;
		}
		int count(str s,int st,int en=inf){
			if(en>_sz)en=_sz;
			const int lent=en,lens=s._sz;
			static int* nex=(int*)calloc(lens,4);
			int i=st,j=-1,cnt=0;
			nex[0]=-1;
			while(i<lens)
				if(j==-1||s[i]==s[j])nex[++i]=++j;
				else j=nex[j];
			i=st;j=0;
			while(i<lent){
				if(j==-1||(*this)[i]==s[j])++i,++j;
				else j=nex[j];
				if(j==lens)j=0,++cnt;
			}
			return cnt;
		}
		bool endswith(str suf){return!memcmp(data+_sz-suf._sz,suf.data,suf._sz);}
		bool endswith(str suf,const int st,int en=inf){
			if(en>_sz)en=_sz;
			if(en-st<suf._sz)return 0;
			return!memcmp(data+en-suf._sz,suf.data,suf._sz);
		}
		str expandtabs(const int tabsize=8,const char ch=' '){
			str result;
			int pos=0;
			for(char c:*this){
				if(c=='\t'){
					result+=str(ch)*(tabsize-pos%tabsize);
					pos=0;
				}else{
					result+=c;
					pos=(c=='\n')?0:pos+1;
				}
			}
			return result;
		}
		int find(str s){
			const int lent=_sz,lens=s._sz;
			static int* nex=(int*)calloc(lens,4);
			int i=0,j=-1;
			nex[0]=-1;
			while(i<lens)
				if(j==-1||s[i]==s[j])nex[++i]=++j;
				else j=nex[j];
			i=j=0;
			while(i<lent){
				if(j==-1||(*this)[i]==s[j])++i,++j;
				else j=nex[j];
				if(j==lens)return i-j;
			}
			return -1;
		}
		int find(str s,int st,int en=inf){
			if(en>_sz)en=_sz;
			const int lent=en,lens=s._sz;
			static int* nex=(int*)calloc(lens,4);
			int i=st,j=-1;
			nex[0]=-1;
			while(i<lens)
				if(j==-1||s[i]==s[j])nex[++i]=++j;
				else j=nex[j];
			i=st;j=0;
			while(i<lent){
				if(j==-1||(*this)[i]==s[j])++i,++j;
				else j=nex[j];
				if(j==lens)return i-j;
			}
			return -1;
		}
		int index(str x,const int st=0,int en=inf){return find(x,st,en);}
		bool isalnum(){for(char c:*this)if(!std::isalnum(c))return 0;return 1;}
		bool isalpha(){for(char c:*this)if(!std::isalpha(c))return 0;return 1;}
		bool isdecimal(){for(char c:*this)if(!std::isdigit(c))return 0;return 1;}
		bool isdigit(){for(char c:*this)if(!std::isdigit(c))return 0;return 1;}
		bool islower(){for(char c:*this)if(std::isalpha(c))return std::islower(c);return 0;}
		bool isspace(){for(char c:*this)if(!std::isspace(c))return 0;return 1;}
		bool isupper(){for(char c:*this)if(std::isalpha(c))return std::isupper(c);return 0;}
		str lower(){str s=*this;for(char& x:s)x=tolower(x);return s;}
		str ljust(const int width,const int fillchar){return *this+str(fillchar)*(width-_sz);}
		str lstrip(str chars=" "){
			static bool f[128]={0};
			for(int i:chars)f[i]=1;
			for(int pos=0;pos<_sz;++pos)
				if(!f[data[pos]])return data+pos;
			return"";
		}
		mt maketrans(str a,str b){
			if(a._sz!=b._sz)return mt("","",0);
			return mt(a.data,b.data,a._sz);
		}
		str* partition(str part){
			static str ret[3];
			const int pos=find(part);
			if(pos==-1){*ret=*this;ret[1]=ret[2]="";return ret;}
			ret[0].data=new char[ret[0]._sz=pos];
			memcpy(ret[0].data,data,pos);
			ret[1]=part;
			ret[2].data=new char[ret[2]._sz=_sz-pos-part._sz];
			memcpy(ret[2].data,data+pos+part._sz,ret[2]._sz);
			return ret;
		}
		str replace(str s,str t){
			const int lent=_sz,lens=s._sz;
			static int* nex=(int*)calloc(lens,4);
			int i=0,j=-1,cnt=0;
			nex[0]=-1;
			while(i<lens)
				if(j==-1||s[i]==s[j])nex[++i]=++j;
				else j=nex[j];
			i=j=0;
			while(i<lent){
				if(j==-1||(*this)[i]==s[j])++i,++j;
				else j=nex[j];
				if(j==lens)j=0,++cnt;
			}
			static int* pos=new int[cnt];
			int tot=0;
			i=j=0;
			while(i<lent){
				if(j==-1||(*this)[i]==s[j])++i,++j;
				else j=nex[j];
				if(j==lens)pos[tot++]=i-s._sz;
			}
			static char* ret=new char[_sz+cnt*(t._sz-s._sz)];
			int tot3=0,tot2=0;
			for(int i=0;i<_sz;++i){
				if(i==pos[tot3]){
					++tot3;
					tot2+=sprintf(ret+tot2,"%s",t.data);
				}else ret[tot2++]=data[i];
			}
			ret[tot2]=0;
			return ret;
		}
		int rfind(str s){
			const int lent=_sz,lens=s._sz;
			static int* nex=(int*)calloc(lens,4);
			int i=0,j=-1,ret=-1;
			nex[0]=-1;
			while(i<lens)
				if(j==-1||s[i]==s[j])nex[++i]=++j;
				else j=nex[j];
			i=j=0;
			while(i<lent){
				if(j==-1||(*this)[i]==s[j])++i,++j;
				else j=nex[j];
				if(j==lens)ret=i-j;
			}
			return ret;
		}
		int rfind(str s,int st,int en=inf){
			if(en>_sz)en=_sz;
			const int lent=en,lens=s._sz;
			static int* nex=(int*)calloc(lens,4);
			int i=st,j=-1,ret=-1;
			nex[0]=-1;
			while(i<lens)
				if(j==-1||s[i]==s[j])nex[++i]=++j;
				else j=nex[j];
			i=st;j=0;
			while(i<lent){
				if(j==-1||(*this)[i]==s[j])++i,++j;
				else j=nex[j];
				if(j==lens)ret=i-j;
			}
			return ret;
		}
		int rindex(const char x,const int st=0,int en=inf){return rfind(x,st,en);}
		str rjust(const int width,const int fillchar=32){return str(fillchar)*(width-_sz)+*this;}
		str* rpartition(str part){
			static str ret[3];
			const int pos=rfind(part);
			if(pos==-1){*ret=*this;ret[1]=ret[2]="";return ret;}
			ret[0].data=new char[ret[0]._sz=pos];
			memcpy(ret[0].data,data,pos);
			ret[1]=part;
			ret[2].data=new char[ret[2]._sz=_sz-pos-part._sz];
			memcpy(ret[2].data,data+pos+part._sz,ret[2]._sz);
			return ret;
		}
		str rstrip(str chars=" "){
			static bool f[128]={0};
			for(int i:chars)f[i]=1;
			for(int pos=_sz-1;pos>=0;--pos)
				if(!f[data[pos]])return str(data,pos+1);
			return"";
		}
		bool startswith(str suf){return!memcmp(data,suf.data,suf._sz);}
		bool startswith(str suf,const int st,int en=inf){
			if(en>_sz)en=_sz;
			if(en-st<suf._sz)return 0;
			return!memcmp(data+st,suf.data,suf._sz);
		}
		str strip(str chars=" "){return(*this).lstrip(chars).rstrip(chars);}
		str swapcase(){
			str ret=*this;
			for(char& s:ret)if(std::isalpha(s))s^=32;
			return ret;
		}
		str title(){
			str ret=*this;
			ret[0]=toupper(ret[0]);
			for(int i=1;i<_sz;++i)
				ret[i]=std::isalpha(ret[i-1])?tolower(ret[i]):toupper(ret[i]);
			return ret;
		}
		bool istitle(){return title()==*this;}
		str translate(const mt mp){
			str ret=*this;
			for(char& c:ret)c=mp.trans[c];
			return ret;
		}
		str upper(){str s=*this;for(char& x:s)x=toupper(x);return s;}
		str zfill(const int width){return str("0")*(width-_sz)+*this;}
		str operator+(str other){
			str ret=*this;
			ret.data=(char*)realloc(ret.data,(ret._sz+other._sz)*sizeof(char));
			memcpy(ret.data+ret._sz,other.data,other._sz*sizeof(char));
			ret._sz+=other._sz;
			return ret;
		}
		str operator+=(str other){
			data=(char*)realloc(data,(_sz+other._sz)*sizeof(char));
			memcpy(data+_sz,other.data,other._sz*sizeof(char));
			_sz+=other._sz;
			return *this;
		}
		str operator*(const int x){
			if(x<1)return"";
			str ret;
			ret.data=new char[ret._sz=_sz*x];
			for(int i=0;i<ret._sz;++i)ret.data[i]=(*this)[i%_sz];
			return ret;
		}
		str operator*=(const int x){
			const int sz=_sz;
			data=(char*)realloc(data,(_sz*=x)*sizeof(char));
			for(int i=0;i<_sz;++i)data[i]=(*this)[i%sz];
			return *this;
		}
		str split(int st=inf,int en=inf,int dx=inf){
			if(st==inf)st=0;
			if(st<0)st+=_sz;
			if(en==inf)en=_sz;
			if(en<0)en+=_sz;
			if(dx==inf)dx=1;
			str ret;
			ret.data=new char[ret._sz=(en-st)/dx+2];
			int i=0;
			for(int x=st;x<en;x+=dx)ret.data[i++]=(*this)[x];
			ret.data=(char*)realloc(ret.data,(ret._sz=i)*sizeof(char));
			return ret;
		}
};
char min(str lst){char val=lst[0];for(int m=1;m<lst._sz;++m)if(lst[m]<val)val=lst[m];return val;}
char max(str lst){char val=lst[0];for(int m=1;m<lst._sz;++m)if(lst[m]>val)val=lst[m];return val;}
int len(str lst){return lst._sz;}
bool in(const char val,str l){
	for(char x:l)if(x==val)return 1;
	return 0;
}
int _strcmp(str a,str b){
	const int sz=b._sz<a._sz?b._sz:a._sz;
	for(int i=0;i<sz;++i){
		if(a[i]<b[i])return-1;
		if(a[i]>b[i])return 1;
	}
	return b._sz==a._sz?0:a._sz>b._sz?1:-1;
}
bool operator<(str a,str b){return!~_strcmp(a,b);}
bool operator<=(str a,str b){return _strcmp(a,b)<1;}
bool operator>=(str a,str b){return _strcmp(a,b)>-1;}
bool operator>(str a,str b){return _strcmp(a,b)==1;}
bool operator==(str a,str b){return!_strcmp(a,b);}
bool operator!=(str a,str b){return _strcmp(a,b);}
str operator*(int x,str a){return a*x;}
void print(str s){
	for(char c:s)putchar(c);
	printf(",size=%d\n",len(s));
}
#undef T
int main(){
	//函数的功能请在 https://www.runoob.com/python/python-strings.html 查看
}
原文