日记:
今天我做了一个类。类的名字叫jstring。是用来对字符串进行常规的操作的。
这个类的特点是,几乎所有的jstring都用jstring&的形式引用。
来源于托管内存管理式的思想,做到变量能自由创建和释放。
创建出来的所有对象会自动释放。所以一行内要多长就可以写多长。~~
以后不需要手工分配buffer了。。~~~~
我的水平不高。希望高手指正。
下载的地方是:
http://www.lostinet.com/public/cplusplus/jstring/
http://www.lostinet.com/public/cplusplus/jstring/jstring.cpp.txt
http://www.lostinet.com/public/cplusplus/jstring/jstring.zip
相关csdn论坛文章:
http://www.csdn.net/expert/topic/984/984638.xml?temp=.7542993
// testlib.cpp : defines the entry point for the console application.
//
#include "stdafx.h"
/****************************************************************\
链式委托内存管理的字符串类jstring
栈头堆尾?
by- http://www.lostinet.com
作者:lostinet[迷失网络]。。(记得记住我啊。。)
\****************************************************************/
#ifndef _lostinet_class_jstring_
#define _lostinet_class_jstring_
/*
因为我是jscript的爱好者,并且很依赖jscript中的string类型
所以这个命名为jstring了
工作原理:
在函数内定义一个由stack分配的jstring对象。
那么在函数返回的时候,jstring对象就会释放。
jstring对象对其所有操作所产生的对象,用一条链连起来。
那么当stack生成的的jstring对象释放时,
也自动释放其相关的所有jstring了。
所以在类外部使用jstring*指针是错误的用法
而且一条链中的对象最好不要传递到其他函数中。
通常的方法是在一个函数里定义第一个
jstring js;
然后其它变量使用jstring&类型
不建议在调用函数时传递jstring*,和jstring&
忠告:
在外面使用jstring* 类型是不合法的。
千万要注意链的生存期,
特别是不同一条链的jstring&+jstring&时,返回的jstring&属于第一条链
*/
#include <stdlib.h>
#include <string.h>
class jstring
{
protected:
jstring* pallocator;
jstring* pnext;
//只有链头才用plast
jstring* plast;
char* pstring;
int length;
jstring(jstring& pa,unsigned int initlen=0,const char* initstr="")
{
pallocator=&pa;
if(pa.plast)pa.plast->pnext=this;
pa.plast=this;
pnext=plast=0;
length=initlen;
pstring=new char[initlen+1];
strcpy(pstring,initstr);
}
public:
jstring(const char* str)
{
pallocator=this;
pnext=plast=0;
length=strlen(str);
pstring=new char[length+1];
strcpy(pstring,str);
}
jstring()
{
pallocator=this;
pnext=plast=0;
pstring=new char[1];
pstring[0]=0;
length=0;
}
virtual ~jstring()
{
delete pstring;
if(pnext)delete pnext;
}
char* detach()
{
char* str=pstring;
pstring=new char[1];
pstring[0]=0;
length=0;
return str;
}
char* getbuffer()
{
return pstring;
}
char* getbuffersetlength(int len)
{
//assert(len>=0);
delete pstring;
length=len;
pstring=new char[len+1];
return pstring;
}
/****************************************************************/
int getlength()
{
return length;
}
int indexof(const char * str,int start=0)
{
if(start+1>length)return -1;
char* pstart=pstring+start;
char* pos=strstr(pstart,str);
if(pos==0)return -1;
return pos-pstring;
}
int lastindexof(const char * str)
{
int len=strlen(str);
char *buf1=new char[length+1];
char *buf2=new char[len+1];
strcpy(buf1,pstring);
strcpy(buf2,str);
strrev(buf1);
strrev(buf2);
char* pos=strstr(buf1,buf2);
delete buf1;
delete buf2;
if(pos==0)return -1;
return length+buf1-pos-len;
}
jstring& left(int len)
{
return substr(0,len);
}
jstring& right(int len)
{
return substr(length-len,len);
}
jstring& substr(int start,int len)
{
if(start<0)start=0;
if(start+1>length)
return * new jstring(*pallocator);
if(len<=0)
return * new jstring(*pallocator);
if(start+len>length)