⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mystr.cpp

📁 内存管理程序
💻 CPP
📖 第 1 页 / 共 3 页
字号:
* Purpose:	Prepend a string before us
*********************************************************************/

void CStr::AddStringAtLeft(const char* s)
{
	UINT slen = strlen(s);
	if (slen == 0)
		return;
	Buffer (int(GetLength() + slen));
	// Move existing data -- do NOT use memcpy!!
	memmove (data.m_Text+slen, data.m_Text, GetLength()+1);
	// And copy bytes to be prepended
	memcpy (data.m_Text, s, slen);
	data.m_Length = int(data.m_Length + slen);
}


/*********************************************************************
* Proc:		operator +LPCSTR for CStr
*********************************************************************/

CStr operator+(const char* lpsz, const CStr& s)
{
	CStr temp (int(s.GetLength() + strlen(lpsz)));
	temp  = lpsz;
	temp += s;
	return temp;
}


/*********************************************************************
* Proc:		operator +char for CStr
*********************************************************************/

CStr operator+(const char ch, const CStr& s)
{
	CStr temp (int(s.GetLength() + 1));
	temp = ch;
	temp += s;
	return temp;
}


/*********************************************************************
* Proc:		CStr::GetLastChar
*********************************************************************/

char CStr::GetLastChar() const
{
	int l = data.m_Length;
	if (( l < 1) || ( data.m_Text == NULL ) )
		ThrowBadIndex();
	return data.m_Text[l-1];
}

/*********************************************************************
* Proc:		CStr::GetWord 解析出单词 -1终止
//解析出第一个单词 -1终止
//解析出最后一个单词 -1终止
*********************************************************************/

int  CStr::GetFirstWord(CStr& result)
{
	result.Empty();
	int start,end;
	end = start= 0;
	char *p = data.m_Text;
	while (p[start] == ' ')
			start++;
	end    =  start;
	while (p[end] !=' '&&p[end] !='\0')
		    end++;
	if(end>start)
	{
	   GetMiddle(start,end-start,result);
	   return 1;
	}
	return -1;
}

int  CStr::GetLastWord(CStr& result)
{
	result.Empty();

	if(IsEmpty())
		return -1;

	int start,end;
	end = GetLength()-1;
	start= 0;

	char *p = data.m_Text;
	while (end &&p[end] == ' ')
			end--;

	if((end==0)&&p[end] == ' ')
		return -1;

	start    =  end;
	while (start&&p[start] !=' ')
		    start--;

	if((start==0)&&p[start] !=' ')
	{
		GetMiddle(start,end-start+1,result);
		return 1;
	}

	if(end>start)
	{
		GetMiddle(start+1,end-start,result);
		return 1;
	}
	
	return -1;

}

int  CStr::GetWord(CStr& result)
{
	result.Empty();
	int start,end;
	end = 0;
       start = m_wordfindpos;
	char *p = data.m_Text;
	while (p[start] == ' ')
			start++;
	end    =  start;
	while (p[end] !=' '&&p[end] !='\0')
		    end++;
	if(end>start)
	{
          m_wordfindpos = end;
	   GetMiddle(start,end-start,result);
	   return 1;
	}
	return -1;
}


/*********************************************************************
* Proc:		CStr::GetLeft
*********************************************************************/

void CStr::GetMiddle (int start, int chars, CStr& result)
{
	result.Empty();
	// Nothing to return?
	int l = GetLength();
	if (l == 0  ||  (UINT(start)+chars) == 0)
		return;
	// Do not return data beyond the end of the string
	if (start >= l)
		return;
	if ((start+chars) >= l)
		chars = int(l - start);
	// Copy bytes
	result.CoreAddChars(GetString()+start, chars);
}

void CStr::GetLeft (int chars, CStr& result)
{
	GetMiddle(0, chars, result);
}

void CStr::GetRight (int chars, CStr& result)
{
	if (chars >= GetLength()) {
		result = *this;
		return;
	}
	GetMiddle(int(GetLength()-chars), chars, result);
}

CStr CStr::Left( int pos )
{
	CStr  result;
	GetMiddle( 0, pos, result );
	return result;
}

CStr CStr::Right( int chars )
{
	CStr  result;

	if (chars >= GetLength()) {
		result = *this;
	}
	else
	{
		GetMiddle(int(GetLength()-chars), chars, result);
	}
	return result;
}

CStr CStr::Mid( int nFirst )
{
	CStr result;
	if (nFirst >= GetLength()) {
		result.Empty();		
	}
	else
	{
		GetMiddle(nFirst, int(GetLength()-nFirst), result);
	}
	return result;
}

CStr CStr::Mid( int nFirst, int nchars )
{
	CStr result;
	if (nFirst >= GetLength()) {
		result.Empty();
		return result;
	}

	if( nchars > ( GetLength() - nFirst ) )
	{
		nchars = GetLength() - nFirst;
	}

	GetMiddle(nFirst, nchars, result);

	return result;
}


/*********************************************************************
* Proc:		CStr::IsAlpha CStr::IsDigit
*********************************************************************/

int CStr::IsAlpha()
{
	char *p = data.m_Text;
	int len = GetLength();
	int i;
	for(i=0;i<len;i++)
	{
		if(isalpha(p[i])==0)
			return 0;
	}
	return i;
}

int CStr::IsDigit()
{
	char *p = data.m_Text;
	int len = GetLength();
	int i;
	for(i=0;i<len;i++)
	{
		if(isdigit(p[i])==0)
			return 0;
	}
	return i;
}

/*********************************************************************
* Proc:		CStr::ConvertInt 
*********************************************************************/

int  CStr::ConvertInt()
{
	return atoi (data.m_Text);
}

/*********************************************************************
* Proc:		CStr::TruncateAt
* Purpose:	Cuts off the string at the character with the specified
*			index.  The allocated buffer remains the same.
*********************************************************************/

void CStr::TruncateAt (int idx)
{
	if (idx >= GetLength())
		return;
	// Spawn a copy if necessary
	Buffer (data.m_Alloc);		// Preserve buffer size
	// And do the truncation
	data.m_Text[idx] = 0;
	data.m_Length = idx;
}


/*********************************************************************
* Proc:		CStr::Find and ReverseFind
* Purpose:	Scan the string for a particular character (must not
*			be 0); return the index where the character is found
*			first, or -1 if cannot be met
*********************************************************************/

int CStr::Find (char ch, int startat /*= 0*/) const
{
	// Start from middle of string?
	if (startat > 0) {
		if (startat >= GetLength())
			ThrowBadIndex();
	}
	char* scan = strchr (data.m_Text+startat, ch);
	if (scan == NULL)
		return -1;
	else
		return scan - data.m_Text;
}

int CStr::ReverseFind (char ch, int startat /*= -1*/) const
{
	if (startat == (int) -1) {
		// Scan entire string
		char* scan = strrchr (data.m_Text, ch);
		if (scan)
			return scan - data.m_Text;
	}
	else {
		// Make sure the index is OK
		if (startat >= GetLength())
			ThrowBadIndex();
		for (int findex = (int) startat-1; findex >= 0; findex--) {
			if (data.m_Text[findex] == ch)
				return findex;
		}
	}
	return -1;
}
int  CStr::ReverseFind (const char *match,int len, int startat ) const
{
	int datalen;
	if (-1==startat)
		datalen = data.m_Length;
	else
	{
		if (startat>=0&&startat<data.m_Length)
			datalen = startat;
		else
			return -1;
	}

	for (int i=datalen - len-1; i>=0 ; i--)
	{
		if ( data.m_Text[i]!=*match )
			continue;
		if ( 0!=memcmp(match,data.m_Text+i,len ) )
			continue;
		return i;
	}
	return -1;
}

/*********************************************************************
 * NAME:    Find()
 * PURPOSE: Find a string in text
 *
 * PARAM:   match   = the string we are looking for
 *          startat = where to start search  string 
*********************************************************************/

int CStr::Find( const char *match,int startat /*= 0*/) const
{

	// Start from middle of string?
	if (startat > 0) {
		if (startat >= GetLength())
			ThrowBadIndex();
	}

	char* scan = strstr (data.m_Text+startat, match);
	if (scan == NULL)
		return -1;
	else
		return scan - data.m_Text;
}

/*********************************************************************
* Proc:		CStr::Compare and CompareNoCase
*********************************************************************/


int CStr::Compare (const char* match,int chars) const
{
	int i;
	if(chars)
		i = strncmp (data.m_Text, match,chars);
	else
		i = strcmp (data.m_Text, match);

	if (i == 0)
		return 0;
	else if (i < 0)
		return -1;
	else
		return 1;
}

bool CStr::Equal(const char* match,int charnum)
{
	char *p = data.m_Text;
	int len = data.m_Length;
	if(len>=charnum&&memcmp(p,match,charnum)==0)
		return true;
	return false;
}

int CStr::CompareNoCase (const char* match,int chars) const
{
	int i ;
	if(chars)
		i= strncasecmp (data.m_Text, match,chars);
	else
		i= strcasecmp (data.m_Text, match);

	if (i == 0)
		return 0;
	else if (i < 0)
		return -1;
	else
		return 1;
}


/*********************************************************************
* Proc:		CStr::GrowTo
* Purpose:	If the buffer is smaller than the amount of characters
*			specified, reallocates the buffer.  This function cannot
*			reallocate to a buffer smaller than the existing one.
*********************************************************************/

void CStr::GrowTo(int size)
{
	Buffer (size);
}


/*********************************************************************
* Proc:		CStr::operator == (basic forms, the rest are inline)
*********************************************************************/

bool operator ==(const CStr& s1, const CStr& s2)
{
	int slen = s2.GetLength();
	if (s1.GetLength() != slen)
		return false;
	return memcmp(s1.GetString(), s2.GetString(), slen) == 0;
}

bool operator ==(const CStr& s1, LPCTSTR s2)
{

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -