标准C++实现字符串类CString

// String.cpp: implementation of the CString class.
 //
 //////////////////////////////////////////////////////////////////////
 
#include "String.h"
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdarg.h>
 #include <string.h>
 #include <ctype.h>
 
#if defined(WIN32)
 #include <windows.h>
 #elif defined(linux)
 #elif defined(_AIX)
  #include <extension.h>
 #else
 #endif
 #ifdef _xlC
        #include <strings.h>
 #endif
 #include "macros.h"
 #include "String.h"
 using namespace clib;
 
// constructors
 
// allocate buffers
 CString::CString(int iBufferNum)   
 {
  m_pBuffer = new char[iBufferNum * STR_PAGE_SIZE];      //动态申请内存
  m_nLength = 0;                                        //长度
  m_nSize  = iBufferNum * STR_PAGE_SIZE;                //字符串大小初始化为iBufferNum个4*1024
  m_pDataStart = m_pDataEnd = m_pBuffer;              //开始字符串指针全部指向m_pBuffer
 }
 
// new one character
 CString::CString(char c, int iBufferNum)        //参数为字符c和iBufferNum,构造函数
 {
  char data[2], len = 2; 
 
 data[0] = c;                      //将c放在字符数组首位
  data[1] = 0;                      //将字符数组data第二位置为0
  if (len > iBufferNum * STR_PAGE_SIZE)      //如果iBufferNum个4*1024小于3
  {
  m_pBuffer = new char[len + 1]; //为字符串类的主要字符串m_pBuffer动态申请内存空间
  m_nSize  = len + 1;        //字符串大小为3
  }
  else
  {
  m_pBuffer = new char[iBufferNum * STR_PAGE_SIZE];    //字符串申请内存空间 
  m_nSize  = iBufferNum * STR_PAGE_SIZE;
  }
 
 // copy data and set total length
  CreateFromData(data, len);            //根据字符数组data开始创建对象m_pDataStart和m_pDataEnd
 }
 // copy data from a null terminated string
 CString::CString(const char *data, int iBufferNum)
 {
  long len = strlen(data);
 
 if (len > iBufferNum * STR_PAGE_SIZE)    //字符串的大小大于制定数量个4*1024
  {
  m_pBuffer = new char[len + 1]; // 动态申请内存空间
  m_nSize  = len + 1;
  }
  else
  {
  m_pBuffer = new char[iBufferNum * STR_PAGE_SIZE];
  m_nSize  = iBufferNum * STR_PAGE_SIZE;
  }
 
 // copy data and set total length
  CreateFromData(data, len);        //根据字符数组data开始创建对象m_pDataStart和m_pDataEnd 
 }
 
// copy data from a binary data
 CString::CString(const char *data, long len, int iBufferNum)  //构造函数的参数为字符串
 {
  if (len > iBufferNum * STR_PAGE_SIZE)        //如果字符串的大小大于制定数量个4*1024
  {
  m_pBuffer = new char[len + 1]; // 动态申请内存空间
  m_nSize  = len + 1;
  }
  else
  {
  m_pBuffer = new char[iBufferNum * STR_PAGE_SIZE];
  m_nSize  = iBufferNum * STR_PAGE_SIZE;
  }
 
 // copy data and set total length
  CreateFromData(data, len); //根据字符数组data开始创建对象m_pDataStart和m_pDataEnd 
 }
 
// destructor
 CString::~CString(void)
 {
  SafeDestroy();
 }
 
// destroy allocated memory space and init all pointers
 void CString::SafeDestroy(void)
 {
  SAFEDELETE(m_pBuffer);
  // Important for InitPointers()
  m_nSize = 0;
  Empty();
 }
 

void CString::Empty(void)               
 {
 #ifdef WIN32
  ZeroMemory(m_pBuffer, m_nSize);
 #else // Linux, AIX
  memset(m_pBuffer, 0, m_nSize);
 #endif
  m_pDataStart = m_pDataEnd = m_pBuffer;        //将该字符串的头指针和尾指针全部指向字符串的头部
  m_nLength = 0;                              //字符串长度置为0
 }
 
char *CString::GetData(void) const          //获得指向该字符串的头部指针
 {
  return m_pDataStart;
 }
 

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/987761179eb0962ebeff51d44e8e322f.html