📄 string.cpp
字号:
/******************************************************************************** string.cpp: Strings manipulation*-------------------------------------------------------------------------------* (c)1999-2001 VideoLAN* $Id: string.cpp,v 1.3 2002/09/09 14:02:34 jpsaman Exp $** Authors: Benoit Steiner <benny@via.ecp.fr>** This program is free software; you can redistribute it and/or* modify it under the terms of the GNU General Public License* as published by the Free Software Foundation; either version 2* of the License, or (at your option) any later version.** This program is distributed in the hope that it will be useful,* but WITHOUT ANY WARRANTY; without even the implied warranty of* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the* GNU General Public License for more details.** You should have received a copy of the GNU General Public License* along with this program; if not, write to the Free Software* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.**-------------------------------------------------------------------------------* TO DO: a reference counter to implement the copy on write scheme********************************************************************************///------------------------------------------------------------------------------// Preamble//------------------------------------------------------------------------------#include "defs.h"#include <ctype.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include "common.h"#include "string.h"#include "debug.h"//******************************************************************************// class C_String//******************************************************************************////******************************************************************************//------------------------------------------------------------------------------// Default constructor//------------------------------------------------------------------------------// The string is initialised to '\0' in order to have a homogeneous behaviour // even if the string is empty, so that all if(!m_pszBuff) can be avoided//------------------------------------------------------------------------------C_String::C_String(){ m_iLength = 0; m_pszBuff = new char[1]; m_pszBuff[0] = '\0';}//------------------------------------------------------------------------------// Simple constructor//------------------------------------------------------------------------------// Memory is dynamicly allocated but no test is made to detect memory leaks, so// if the system run short of memory, a segfault will probably happen//------------------------------------------------------------------------------C_String::C_String(const char* pszSrc){ ASSERT(pszSrc); m_iLength = strlen(pszSrc); m_pszBuff = new char[m_iLength + 1]; memcpy(m_pszBuff, pszSrc, m_iLength + 1);}//------------------------------------------------------------------------------// Simple constructor//------------------------------------------------------------------------------// Memory is dynamicly allocated but no test is made to detect memory leaks, so// if the system run short of memory, a segfault will probably happen//------------------------------------------------------------------------------C_String::C_String(char cSrc){ m_iLength = 1; m_pszBuff = new char[2]; m_pszBuff[0] = cSrc; m_pszBuff[1] = '\0';}//------------------------------------------------------------------------------// Simple constructor//------------------------------------------------------------------------------// Memory is dynamicly allocated but no test is made to detect memory leaks,// so if the system run short of memory, a segfault will probably happen//------------------------------------------------------------------------------C_String::C_String(s32 iSrc){ // int is at most 4 giga on a 32 bits computers, so 12 digits are enough ASSERT(sizeof(int) <= 4); char pszNumber[12]; ASSERT(sprintf(pszNumber, "%d", iSrc)); sprintf(pszNumber, "%d", iSrc); // Stores the value of iSrc in the buffer of the string m_iLength = strlen(pszNumber); m_pszBuff = new char[m_iLength + 1]; memcpy(m_pszBuff, pszNumber, m_iLength+1);}//------------------------------------------------------------------------------// Simple constructor//------------------------------------------------------------------------------// Memory is dynamicly allocated but no test is made to detect memory leaks,// so if the system run short of memory, a segfault will probably happen//------------------------------------------------------------------------------C_String::C_String(u32 iSrc){ // int is at most 4 giga on a 32 bits computers, so 12 digits are enough ASSERT(sizeof(int) <= 4); char pszNumber[12]; ASSERT(sprintf(pszNumber, "%d", iSrc)); sprintf(pszNumber, "%d", iSrc); // Stores the value of iSrc in the buffer of the string m_iLength = strlen(pszNumber); m_pszBuff = new char[m_iLength + 1]; memcpy(m_pszBuff, pszNumber, m_iLength+1);}//------------------------------------------------------------------------------// Copy constructor//------------------------------------------------------------------------------C_String::C_String(const C_String& strSrc){ m_iLength = strSrc.m_iLength; m_pszBuff = new char[m_iLength+1]; memcpy(m_pszBuff, strSrc.m_pszBuff, m_iLength+1);}//------------------------------------------------------------------------------// Internal constructor//------------------------------------------------------------------------------// Memory is allocated but not initialised: this is why this constructor is// protected and reserved to internal use//------------------------------------------------------------------------------C_String::C_String(unsigned int iSize, bool bDummy){ m_iLength = iSize; m_pszBuff = new char[iSize+1];}//------------------------------------------------------------------------------// Destructor//------------------------------------------------------------------------------// Strings are never empty in our implementation: the buffer contains always at// least a NULL char//------------------------------------------------------------------------------C_String::~C_String(){ ASSERT(m_pszBuff); // Prevent internal bug delete[] m_pszBuff;}//------------------------------------------------------------------------------// //------------------------------------------------------------------------------// Old buffer is freed, and a new one is allocated to store the string pzsSrc//------------------------------------------------------------------------------C_String& C_String::operator = (const char *pszSrc){ ASSERT(pszSrc); // Delete current instance ASSERT(m_pszBuff); delete[] m_pszBuff; // Store the new string m_iLength = strlen(pszSrc); m_pszBuff = new char[m_iLength + 1]; memcpy(m_pszBuff, pszSrc, m_iLength+1); return *this;}//------------------------------------------------------------------------------// //------------------------------------------------------------------------------// Old buffer is freed, and a new one is allocated to store the string strSrc//------------------------------------------------------------------------------C_String& C_String::operator = (const C_String& strSrc){ // To avoid pbs if the str = str operation is tried if(*this != strSrc) { // Delete old buffer ASSERT(m_pszBuff); delete[] m_pszBuff; // Store the new string m_iLength = strSrc.m_iLength; m_pszBuff = new char[m_iLength + 1]; memcpy(m_pszBuff, strSrc.m_pszBuff, m_iLength+1); } return *this;}//------------------------------------------------------------------------------// //------------------------------------------------------------------------------// Old buffer is freed, and a new one is allocated to store the integer iSrc// which is converted to its textual representation//------------------------------------------------------------------------------C_String& C_String::operator = (const int iSrc){ // This is not the most efficient way to do this, but this allow us to deal // only once with the int to string conversion *this = C_String(iSrc); return *this;}//------------------------------------------------------------------------------// //------------------------------------------------------------------------------////------------------------------------------------------------------------------C_String* C_String::Clone(){ return new C_String(*this);}//------------------------------------------------------------------------------// //------------------------------------------------------------------------------// Final NULL char is not counted//------------------------------------------------------------------------------unsigned int C_String::Length() const{ return m_iLength;};//------------------------------------------------------------------------------// //------------------------------------------------------------------------------// This operator is read only, it cannot be used to modify the string//------------------------------------------------------------------------------const char& C_String::operator [] (unsigned int iIndex) const{ // Check that index range is correct ASSERT(iIndex < m_iLength); return m_pszBuff[iIndex];}//------------------------------------------------------------------------------// //------------------------------------------------------------------------------// Give read only access to the raw string//------------------------------------------------------------------------------const char* C_String::GetString() const{ return m_pszBuff;}//------------------------------------------------------------------------------// //------------------------------------------------------------------------------bool C_String::operator == (const char* pszArg) const{ ASSERT(pszArg); return !strcmp(m_pszBuff, pszArg);}//------------------------------------------------------------------------------// //------------------------------------------------------------------------------bool C_String::operator == (const C_String& strArg) const{ return !strcmp(m_pszBuff, strArg.m_pszBuff);}//------------------------------------------------------------------------------// //------------------------------------------------------------------------------bool C_String::operator != (const char* pszArg) const{ ASSERT(pszArg); return strcmp(m_pszBuff, pszArg);}//------------------------------------------------------------------------------// //------------------------------------------------------------------------------bool C_String::operator != (const C_String& strArg) const{ return strcmp(m_pszBuff, strArg.m_pszBuff);}//------------------------------------------------------------------------------// //------------------------------------------------------------------------------////------------------------------------------------------------------------------C_String C_String::operator + (const char* pszToken) const{ ASSERT(pszToken); C_String strResult(m_pszBuff); strResult += pszToken; return strResult;}//------------------------------------------------------------------------------// //------------------------------------------------------------------------------////------------------------------------------------------------------------------C_String C_String::operator + (char cChar) const{ C_String strResult(m_iLength+1, true); memcpy(strResult.m_pszBuff, m_pszBuff, m_iLength); strResult.m_pszBuff[m_iLength] = cChar; strResult.m_pszBuff[m_iLength+1] = '\0'; return strResult;}//------------------------------------------------------------------------------// //------------------------------------------------------------------------------////------------------------------------------------------------------------------C_String C_String::operator + (s32 iNumber) const{ // This is not the most efficient way to do this, but this allow us to deal // only once with the int to string conversion C_String strResult(m_pszBuff); strResult += iNumber; return strResult;}//------------------------------------------------------------------------------// //------------------------------------------------------------------------------////------------------------------------------------------------------------------C_String C_String::operator + (u32 iNumber) const{ // This is not the most efficient way to do this, but this allow us to deal // only once with the int to string conversion C_String strResult(m_pszBuff); strResult += iNumber; return strResult;}//------------------------------------------------------------------------------// //------------------------------------------------------------------------------// //------------------------------------------------------------------------------C_String C_String::operator + (const C_String& strToken) const{ C_String strResult(m_pszBuff); strResult += strToken; return strResult;}//------------------------------------------------------------------------------// //------------------------------------------------------------------------------// //------------------------------------------------------------------------------C_String& C_String::operator += (const char *pszToken){ ASSERT(pszToken); int iTokLen = strlen(pszToken); char* pszTmp = m_pszBuff; m_pszBuff = new char[m_iLength + iTokLen + 1]; memcpy(m_pszBuff, pszTmp, m_iLength); memcpy(m_pszBuff+m_iLength, pszToken, iTokLen+1); m_iLength += iTokLen; delete[] pszTmp; return *this;}//------------------------------------------------------------------------------// //------------------------------------------------------------------------------////------------------------------------------------------------------------------C_String& C_String::operator += (char cChar){ *this += C_String(cChar); return *this;}//------------------------------------------------------------------------------//
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -