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

📄 mystr.cpp

📁 内存管理程序
💻 CPP
📖 第 1 页 / 共 3 页
字号:
	int slen = strlen(s2);
	if (s1.GetLength() != slen)
		return false;
	return memcmp(s1.GetString(), s2, slen) == 0;
}

/*********************************************************************
* Proc:		CStr::RemoveLeft
*********************************************************************/

void CStr::RemoveLeft(int count)
{
	if (GetLength() <= count) {
		Empty();
		return;
	}
	if (count == 0)
		return;
	Buffer (data.m_Alloc);		// Preserve buffer size
	memmove (data.m_Text, data.m_Text+count, GetLength()-count+1);
	data.m_Length = int(data.m_Length - count);
}

void CStr::RemoveMiddle(int start, int count)
{
	if (GetLength() <= start) {
		Empty();
		return;
	}
	Buffer (data.m_Alloc);		// Preserve buffer size
	char* pstart = data.m_Text + start;
	if (GetLength() <= (start+count)) {
		pstart[0] = 0;
		data.m_Length = start;
		return;
	}
	memmove (pstart, pstart+count, GetLength()-(start+count)+1);
	data.m_Length = int(data.m_Length - count);
}

void CStr::RemoveRight(int count)
{
	if (GetLength() <= count)
		Empty();
	else
		TruncateAt (int(GetLength() - count));
}


/*********************************************************************
* Proc:		CStr::FmtOneValue
* Purpose:	Helper for CStr::Format, formats one %??? item
* In:		x - ptr to the '%' sign in the specification; on exit,
*				will point to the first char after the spec.
* Out:		True if OK, False if should end formatting (but also copy
*			the remaining characters at x)
*********************************************************************/

bool CStr::FmtOneValue (const char*& x, va_list& marker)
{
	// Start copying format specifier to a local buffer
	char fsbuf[64];
	fsbuf[0] = '%';
	int fsp = 1;
GetMoreSpecifiers:
	// Get one character
#ifdef _DEBUG
	if (fsp >= sizeof(fsbuf)) {
		CStr::ThrowPgmError();
		return false;
	}
#endif
	char ch = x[0];
	if (ch == 0)
		return false;		// unexpected end of format string
	x++;
	// Chars that may exist in the format prefix
	const char fprefix[] = "-+0 #*.123456789hlL";
	if (strchr (fprefix, ch) != NULL) {
		fsbuf[fsp] = ch;
		fsp++;
		goto GetMoreSpecifiers;
	}
	// 's' is the most important parameter specifier type
	if (ch == 's') {
		// Find out how many characters should we actually print.
		//   To do this, get the string length, but also try to
		//   detect a .precision field in the format specifier prefix.
		const char* value = va_arg (marker, const char*);
		UINT slen = strlen(value);
		fsbuf[fsp] = 0;
		char* precis = strchr (fsbuf, '.');
		if (precis != NULL  &&  precis[1] != 0) {
			// Convert value after dot, put within 0 and slen
			char* endptr;
			int result = (int) strtol (precis+1, &endptr, 10);
			if (result >= 0  &&  result < int(slen))
				slen = (UINT) result;
		}
		// Copy the appropriate number of characters
		if (slen > 0)
			CoreAddChars (value, (int) slen);
		return true;
	}
	// '!' is our private extension, allows direct passing of CStr*
	if (ch == '!') {
		// No precision characters taken into account here.
		const CStr* value = va_arg (marker, const CStr*);
		*this += *value;
		return true;
	}
	// Chars that format an integer value
	const char intletters[] = "cCdiouxX";
	if (strchr (intletters, ch) != NULL) {
		fsbuf[fsp] = ch;
		fsbuf[fsp+1] = 0;
		char valbuf[64];
		int value = va_arg (marker, int);
		sprintf (valbuf, fsbuf, value);
		*this += valbuf;
		return true;
	};
	// Chars that format a double value
	const char dblletters[] = "eEfgG";
	if (strchr (dblletters, ch) != NULL) {
		fsbuf[fsp] = ch;
		fsbuf[fsp+1] = 0;
		char valbuf[256];
		double value = va_arg (marker, double);
		sprintf (valbuf, fsbuf, value);
		*this += valbuf;
		return true;
	}
	// 'Print pointer' is supported
	if (ch == 'p') {
		fsbuf[fsp] = ch;
		fsbuf[fsp+1] = 0;
		char valbuf[64];
		void* value = va_arg (marker, void*);
		sprintf (valbuf, fsbuf, value);
		*this += valbuf;
		return true;
	};
	// 'store # written so far' is obscure and unsupported
	if (ch == 'n') {
		ThrowPgmError();
		return false;
	}
	// 'Print unicode string' is not supported also
	if (ch == 'S') {
		ThrowNoUnicode();
		return false;
	}
	// If we fall here, the character does not represent an item
	AddChar (ch);
	return true;
}


/*********************************************************************
* Proc:		CStr::Format
* Purpose:	sprintf-like method
*********************************************************************/

void CStr::FormatCore (const char* x, va_list& marker)
{
	for (;;) {
		// Locate next % sign, copy chunk, and exit if no more
		LPCSTR next_p = strchr (x, '%');
		if (next_p == NULL)
			break;
		if (next_p > x)
			CoreAddChars (x, int(next_p-x));
		x = next_p+1;
		// We're at a parameter
		if (!FmtOneValue (x, marker))
			break;		// Copy rest of string and return
	}
	if (x[0] != 0)
		*this += x;
}

void CStr::Format(const char* fmt, ...)
{
	Empty();
	// Walk the string
	va_list marker;
	va_start(marker, fmt);
	FormatCore (fmt, marker);
	va_end(marker);
}

/*********************************************************************
* Proc:		operator + (CStr and CStr, CStr and LPCSTR)
*********************************************************************/

CStr operator+(const CStr& s1, const CStr& s2)
{
	CStr out (int(s1.GetLength() + s2.GetLength()));
	out  = s1;
	out += s2;
	return out;
}

CStr operator+(const CStr& s, const char* lpsz)
{
	UINT slen = strlen(lpsz);
	CStr out (int(s.GetLength() + slen));
	out.CoreAddChars(s.data.m_Text, s.GetLength());
	out += lpsz;
	return out;
}

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

/*==========================================================
* 函数名      : CStr::LTrim
* 描述        : Remove leading characters from a string.  All characters
*             : to be excluded are passed as a parameter; NULL means
*             : 'truncate spaces'
* 返回        : void 
* 参数        : const char* charset 
* 注释        : 
*=========================================================*/
void CStr::LTrim(const char* charset /*= NULL*/)
{
	int good = 0;
	char *defset = "\r\n \t";
	
	if (charset) {
		while ( (strchr (charset, data.m_Text[good]) != NULL) && ( good < data.m_Length ) )
			good++;
	}
	else {
		while ( (strchr (defset, data.m_Text[good]) != NULL) && ( good < data.m_Length ) )
			good++;
	}
	if (good > 0)
		RemoveLeft (good);
}

/*==========================================================
* 函数名      : CStr::RTrim
* 描述        : Remove trailing characters; see LTrim
* 返回        : void 
* 参数        : const char* charset 
* 注释        : 
*=========================================================*/
void CStr::RTrim(const char* charset /*= NULL*/)
{
	int good = data.m_Length;
	char *defset = "\r\n \t";

	if (good == 0)
		return;
	if (charset) {
		while (good > 0  &&  strchr (charset, data.m_Text[good-1]) != NULL)
			--good;
	}
	else {
		while (good > 0  &&  strchr (defset, data.m_Text[good-1]) != NULL)
			--good;
	}
	TruncateAt (good);		// Also works well with good == 0
}

/*==========================================================
* 函数名      : CStr::MakeUpper
* 描述        : 
* 返回        : void 
* 注释        : 
*=========================================================*/
void CStr::MakeUpper()
{
//	strcpy( data->m_Text , strupr(data->m_Text) );

	char * p = data.m_Text;

	while( *p != 0 )
		{
			if( ( *p >= 'a' ) && ( *p <= 'z' ) ) *p = *p - 'a' + 'A';
			p++;
		}

	return;
}

/*==========================================================
* 函数名      : CStr::MakeLower
* 描述        : 
* 返回        : void 
* 注释        : 
*=========================================================*/
void CStr::MakeLower()
{
//	strcpy( data->m_Text , strlwr(data->m_Text) );

	char * p = data.m_Text;

	while( *p != 0 )
		{
			if( ( *p >= 'A' ) && ( *p <= 'Z' ) ) *p = *p - 'A' + 'a';
			p++;
		}

	return;
}

/*==========================================================
* 函数名      : CStr::MakeReverse
* 描述        : 
* 返回        : void 
* 注释        : 
*=========================================================*/
void CStr::MakeReverse()
{
//	strcpy( data->m_Text , strrev(data->m_Text) );

char * start, * end;

char ch;

start = end = data.m_Text;

while( *end++ != 0 );

while( end - start > 1 )
{
	ch = *start;
	*start = *end;
	*end = ch;
	start++;
	end--;
}

	return;
}

/*==========================================================
* 函数名      : CStr::Replace
* 描述        : 
* 返回        : int  
* 参数        :  const char chOld
* 参数        : const char chNew
* 注释        : 
*=========================================================*/
int  CStr::Replace( const char chOld, const char chNew )
{
	//如是修改前和修改的字符相同,则什么也不发生.
	if( chOld == chNew ) return 0;
	
	int iCount = 0;//用来进行计数的
	for( int pos = 0 ; pos < data.m_Length ; pos++ )
	{
		if( data.m_Text[pos] == chOld )
		{
			data.m_Text[pos] = chNew;
			iCount++;
		}
	}

	return iCount;//note , msdn
}

void CStr::SetEndChar(const char ch)
{
	char *p1 = data.m_Text;
	int len =data.m_Length;
	char *p2 = (char*)memchr(p1,ch,len);
	if(p2)
	{
		*p2 = '\0';
		data.m_Length = p2 -p1;
	}
}


/*==========================================================
* 函数名      : CStr::Replace
* 描述        : 
* 返回        : int  
* 参数        :  const char* lpszOld
* 参数        : const char* lpszNew
* 注释        : 
*=========================================================*/
int  CStr::Replace( const char* lpszOld, const char* lpszNew )
{
	//如果两个字符相同或OLD是NEW的子串,则什么也不做
//	if( strcmp( lpszOld , lpszNew ) == 0 )
//		return 0;

	if( strlen(lpszOld) == 0 )
		return 0;

	if( strstr( lpszNew , lpszOld ) != NULL )
		return 0;

	CStr strLeft,strRight;

	int iCount = 0;
	int	iPlace;
	while(true)
	{
		if( (iPlace = Find(lpszOld)) == -1 )
		{
			return iCount;
		}
		else//发现了子串
		{
			GetLeft(iPlace , strLeft);
			GetRight( GetLength()-iPlace-strlen(lpszOld) , strRight);
			*this = strLeft + lpszNew + strRight;
		}
	}

//	return iCount;//note , msdn
}

/*==========================================================
* 函数名      : CStr::FindOneOf
* 描述        : 
* 返回        : int  
* 参数        : const char* lpszCharSet
* 注释        : 
*=========================================================*/
int  CStr::FindOneOf( const char* lpszCharSet ) const
{
	//取出m_Text中的每个字符,判断是否在lpszCharSet中,一旦出现立即返回
	for( int pos = 0 ; pos < data.m_Length ; pos++ )
	{
		if( strchr(lpszCharSet , data.m_Text[pos]) != NULL )
			return pos;
	}

	return -1;//没有找到对应的项
}

⌨️ 快捷键说明

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