自制的C++简单的StringBuffer类!

来源:互联网 发布:2017中国网民数据统计 编辑:程序博客网 时间:2024/04/29 17:30

 

头文件:
#ifndef STRINGBUFFER_H
#define STRINGBUFFER_H

class StringBuffer
{
private:
    char* *buf,*newBuf_1,*newBuf_2;
    long initSize,bufLen,remLen,lastIndex;
    void init();//初始化
    void addBUF();//扩展StringBuffer的容量
    int isb,isf;
    void strcat(char *);//复制char到StringBuffer
public:
    StringBuffer();
    StringBuffer(char *str);
    void append(char* str);
    char* toString();//以char*形式返回字符串
    void deleteAllStr();//删除所有字符
    long length();//返回字符串的长度
    long capacity();//返回StringBuffer的当前容量
    long indexOf(char *,int);//从int位置开始查找char*
    long lastIndexOf(char *);//返最后一个char*的位置
    int numOf(char *);//返回含有char*的个数
    bool deleteStr(int,int);//删除指定位置的字符
    bool insert(int ,char*);//插入字符到指定的位置
    bool replace(int,int,char*);//替换指定位置的字符为char*
    bool replaceAll(char*,char*);//替换所有指定char*为char*
    bool replaceFirst(char*,char*);//替换第一个char*为char*
    char *substring(int,int);//返回截取指定位置的字符串
};
#endif

实现体:

#include "StringBuffer.h"
#include <string.h>

//构造函数
StringBuffer::StringBuffer()
{
    init();
};
//构造函数
StringBuffer::StringBuffer(char *str)
{
    init();
    strcat(str);
    remLen=bufLen-strlen(str);
   
};
//初始化
void StringBuffer::init()
{
    initSize=51200;
    newBuf_1=new char[initSize];
    memset(newBuf_1,0,initSize);
    buf=&newBuf_1;
    bufLen=initSize;
    remLen=initSize-1;
    isb=0;
    isf=1;
    lastIndex=0;
};
//增加StringBuffer容量
void StringBuffer::addBUF()
{
   
    if(bufLen<524288)
    {
        remLen+=bufLen*2;
        bufLen=bufLen+bufLen*2;
    }
    else
    {
        bufLen=bufLen+524288;
        remLen+=524288;
    }
    if(isb==0)
    {
        newBuf_2=new char[bufLen];
        strcpy(newBuf_2,*buf);
        delete newBuf_1;
        buf=&newBuf_2;
        isb=1;
        return;
    }
    else
    {
        newBuf_1=new char[bufLen];
        strcpy(newBuf_1,*buf);
        delete newBuf_2;
        buf=&newBuf_1;
        isb=0;
    }
   
};
//添加字符串
void StringBuffer::append(char *str)
{
    long m_len=strlen(str);
   
    if(m_len>remLen)
    {
        addBUF();
        append(str);
        return;
    }
    else
    {
        strcat(str);
        remLen-=m_len;
    }
};
//实现添加字符串(私有)
void StringBuffer::strcat(char *str)
{
    while((*(*buf+lastIndex)=*str++)!='/0')
        lastIndex++;
};
//返回char*
char* StringBuffer::toString()
{
    return *buf;
};
//删除所有字符串
void StringBuffer::deleteAllStr()
{
    memset(*buf,0,bufLen);
    lastIndex=0;
    remLen=bufLen-1;
};
//返回字符串长度
long StringBuffer::length()
{
    return lastIndex;
};
//返回StringBuffer容量
long StringBuffer::capacity()
{
    return bufLen;
};
//查找字符串
long StringBuffer::indexOf(char *str,int start)
{
    int strLen=strlen(str);
    long position=-1;
    bool isFind=false;
    if(strLen==1)
    {
        for(long i=start;i<lastIndex;i++)
        {
            if(*(*buf+i)==str[0])
            {
                position=i;
                break;
            }
        }
    }
    else
    {
        for(long i=start;i<lastIndex;i++)
        {
            if(*(*buf+i)==str[0])
            {
                for(int a=1;a<strLen;a++)
                {
                    if(*(*buf+(i+a))!=str[a])
                        break;
                    if(a==strLen-1)
                    {
                        isFind=true;
                        position=i;
                    }
                }
            }
            if(isFind==true)
                break;
        }
    }
    return position;
};
//返回最后str的位置
long StringBuffer::lastIndexOf(char *str)
{
    int strLen=strlen(str);
    long position=-1;
    bool isFind=false;
    for(long i=lastIndex;i>=0;i--)
    {
        if(*(*buf+i)==str[0])
        {
            for(int a=1;a<strLen;a++)
            {
               
                if(*(*buf+(i+a))!=str[a])
                    break;
                if(a==strLen-1)
                {
                    isFind=true;
                    position=i;
                }
            }
        }
        if(isFind==true)
            break;
    }
    return position;
};
//查找str的个数
int StringBuffer::numOf(char *str)
{
    int strLen=strlen(str);
    int num=0;
    for(long i=0;i<lastIndex;i++)
    {
        if(*(*buf+i)==str[0])
        {
            for(int a=1;a<strLen;a++)
            {
               
                if(*(*buf+(i+a))!=str[a])
                    break;
                if(a==strLen-1)
                {
                    num++;
                    i=i+strLen-1;
                }
            }
        }
       
    }
    return num;
};
//删除从start到end的字符串
bool StringBuffer::deleteStr(int start,int end)
{
    int s=start,e=end;
    if(s<0||s>lastIndex-1||start>end)
        return false;
    if(e>lastIndex-1)
        e=end=lastIndex-1;
    while((*(*buf+s)=*(*buf+(e+1)))!='/0')
    {
        s++;
        e++;
    }
    remLen=remLen+(end-start)+1;
    lastIndex=lastIndex-(end-start+1);
    return true;
};
//插入字符串到指定位置
bool StringBuffer::insert(int start,char* str)
{
    if(start<0||start>lastIndex-1)
        return false;
    int strLen=strlen(str);
    long backLen=lastIndex-start+1;//需要往后退的字符个数
    long lI=lastIndex;
    if(strLen>remLen)
        addBUF();
    for(int i=0;i<backLen;i++)
    {
        *(*buf+lI+strLen)=*(*buf+lI);
        lI--;
    }
    for(i=0;i<strLen;i++)
    {
        *(*buf+start+i)=str[i];
    }
    lastIndex+=strLen;
    remLen-=strLen;
    return true;
};
//替代字符串
bool StringBuffer::replace(int start,int end,char*str)
{
    if(start<0||start>lastIndex-1||start>end)
        return false;
    if(end>lastIndex-1)
        end=lastIndex-1;
    int strLen=strlen(str),s=start,e=end;
    if(strLen>remLen)
        addBUF();
    int reLen=end-start+1;//替代长度
    if(reLen>strLen)
    {
        while((*(*buf+(s+strLen))=*(*buf+(e+1)))!='/0')
        {
            s++;
            e++;
        }
        for(int i=0;i<strLen;i++)
        {
            *(*buf+start+i)=str[i];
        }
    }
    else
    {

        long backLen=lastIndex+1-end;//需要往后退的字符个数
        int bL= strLen-reLen;//后退多少个空间
        long lI=lastIndex;
        if(bL>0)
        for(int i=0;i<backLen;i++)
        {
            *(*buf+lI+bL)=*(*buf+lI);
            lI--;
        }
        for(int i=0;i<strLen;i++)
        {
            *(*buf+start+i)=str[i];
        }
    }
    remLen=remLen-(reLen-strLen);
    lastIndex=lastIndex-(reLen-strLen);
    return true;
};
//把所有str1替换成str2
bool StringBuffer::replaceAll(char* str1,char*str2)
{    int strLen1=strlen(str1),strLen2=strlen(str2);
    int i=indexOf(str1,0);
    int e=i+strLen1-1;
    while(i>=0)
    {
        replace(i,e,str2);
        i=indexOf(str1,(i+strLen2));
        e=i+strLen1-1;
    }
    return true;
};
//替代第一个str1为str2;
bool StringBuffer::replaceFirst(char*str1,char*str2)
{
    int strLen1=strlen(str1),strLen2=strlen(str2);
    int i=indexOf(str1,0);
    int e=i+strLen1-1;
    replace(i,e,str2);
    return true;
};
//截取字符字串
char *StringBuffer::substring(int start,int end)
{
    if(start<0||start>lastIndex-1||start>end)
        return NULL;
    if(end>lastIndex-1)
        end=lastIndex-1;
    int strLen=end-start+1;
    char *str=new char[strLen+1];
    str[strLen]=0;
    for(int i=0;i<strLen;i++)
    {
        str[i]=*(*buf+start+i);
    }
    return str;
};
http://bosslife.blog.163.com/blog/static/114917017200931341617713/