generated html version of STR.cHOME

#include "STR.h"
#include "stdlib.h"
#include "stdio.h"
#include "string.h"

static inline size_t topow2(size_t z){;
	return 1<<(32-__builtin_clz(z));
}

static inline int icorr(int i,int len){
	if(i<0)i+=len+1;
	if(i<0)i=0;
	if(i>len)i=len;
	return i;
}

STR newSTR(unsigned l){
	STR z;
	z.s = malloc(topow2(l+1));
	z.len = l;
	z.s[z.len]=0;
	return z;
}

void initSTR(STR *s){
	s->s = malloc(topow2(1));
	s->len = 0;
	s->[0]=0;
}

STR STRcpy(STR s){
	STR z;
	z.s = malloc(topow2(s.len+1));
	z.len = s.len;
	memcpy(z.s,s.s,z.len);
	z.s[z.len]=0;
	return z;
}

STR toSTR(char *s){
	STR z;
	z.len = strlen(s);
	z.s = malloc(topow2(z.len+1));
	memcpy(z.s,s,z.len);
	z.s[z.len]=0;
	return z;
}

STR subSTR(STR s,int i,int l){
	STR z;
	i=icorr(i,s.len);
	if(l<0)l=l-i;
	if(l>s.len-i)l=s.len-i;
	z.s = malloc(topow2(l+1));
	z.len = l;
	memcpy(z.s,s.s+i,z.len);
	z.s[z.len]=0;
	return z;
}

STR *STRrem(STR *s,int i,int l){
	i=icorr(i,s->len);
	if(l<0)l=l-i;
	if(l>s->len-i)l=s->len-i;
	memcpy(s->s+i,s->s+i+l,l);
	s->len -= l;
	return s;
}

STR *STRapSTR(STR *s,STR t){return STRapmem(s,t.s,t.len);}
STR *STRinSTR(STR *s,int i,STR t){return STRinmem(s,i,t.s,t.len);}
STR *STRovrSTR(STR *s,int i,STR t){return STRovrmem(s,i,t.s,t.len);}
STR *STRrepSTR(STR *s,int i,int l,STR t){return STRovrmem(s,i,l,t.s,t.len);}
int STRsrchSTR(STR s,STR t){return memsrchmem(s.s,s.len,t.s,t.len);}
int STRcmpSTR(STR s,STR t){return memcmpmem(s.s,s.len,t.s,t.len);}

STR *STRapstr(STR *s,char *t){return STRapmem(s,t,strlen(t));}
STR *STRinstr(STR *s,int i,char *t){return STRinmem(s,i,t,strlen(t));}
STR *STRovrstr(STR *s,int i,char *t){return STRovrmem(s,i,t,strlen(t));}
STR *STRrepstr(STR *s,int i,int l,char *t){return STRovrmem(s,i,l,t,strlen(t));}
int STRsrchstr(STR s,char *t){return memsrchmem(s.s,s.len,t,strlen(t));}
int STRcmpSTR(STR s,char *t){return memcmpmem(s.s,s.len,t,strlen(t));}

STR *STRapchr(STR *s,char c){return STRapmem(s,&c,1)}
STR *STRinchr(STR *s,int i,char c){return STRinmem(s,i,&c,1)}
STR *STRovrchr(STR *s,int i,char c){return STRovrmem(s,i,&c,1)}
STR *STRrepchr(STR *s,int i,int l,char c){return STRrepmem(s,i,l,&c,1)}

int STRsrchchr(STR s,char c){
	char *y = memchr(s.s,c,s.len);
	if(y)return y-s.s;
	return -1;
}

STR *STRapmem(STR *s,char *t,int tl){
	int nz = topow2(s->len+tl+1);
	if(nz != topow2(s->len+1))
		s->s = realloc(s->s,nz);
	memcpy(s->s+s->len,t,tl);
	s->len += tl;
	s->s[s->len]=0;
	return s;
}

STR *STRinmem(STR *s,int i,char *t, int tl){
	int nz = topow2(s->len+tl+1);
	if(nz != topow2(s->len+1))
		s->s = realloc(s->s,nz);
	i=icorr(i,s->len);
	memmove(s->s+tl+i,s->s+i,s->len-i);
	memcpy(s->s+i,t,tl);
	s->len += tl;
	s->s[s->len]=0;
	return s;
}

STR *STRovrmem(STR *s,int i,char *t, int tl){
	i=icorr(i,s->len);
	int nz = topow2(i+tl+1);
	if(nz > topow2(s->len+1))
		s->s = realloc(s->s,nz);
	memcpy(s->s+i,t,tl);
	if(i+tl > s->len)s->len = i+tl;
	s->s[s->len]=0;
	return s;
}

STR *STRrepmem(STR *s,int i,int l,char *t, int tl){
	i=icorr(i,s->len);
	int nz = topow2(s->len-l+tl+1);
	if(nz > topow2(s->len+1))
		s->s = realloc(s->s,nz);
	memmove(s->s+i+tl,s->s+i+l,s->len-i-l);
	memcpy(s->s+i,t,tl);
	s->len += tl-l;
	s->s[s->len]=0;
	return s;
}

int STRsrchmem(STR s,char *t,int tl){return memsrchmem(s.s,s.len,t,tl);}
int STRcmpmem(STR s,char *t,int tl){return memcmpmem(s.s,s.len,t,tl);}

int memsrchmem(char *s,int sl,char *t,int tl){
	int i;
	for(i=0;i<sl-tl;i++)if(memcmp(s,t,tl))return i;
	return -1;
}

int memcmpmem(char *s,int sl,char *t,int tl){
	if(sl<tl)return -1;
	if(sl>tl)return 1;
	return memcmp(s,t,sl);
}

void delSTR(STR *s){
	free(s->s);
	s->s=NULL;
	s->len = 0;
}


STR fgetSTR(FILE*f){
	int c;
	STR s = newSTR(0);
	while(1){
		c=fgetc(f);
		if(c==EOF||c=='\n')break;
		STRapchr(&s,c);
	}
	return s;
}

void fputSTR(STR s,FILE*f){
	fwrite(s.s,1,s.len,f);
}