📄 mstring.cpp
字号:
//===========================================================================//
// File: string.cpp //
// Contents: // //
//---------------------------------------------------------------------------//
// Copyright (C) Microsoft Corporation. All rights reserved. //
//===========================================================================//
#include "StuffHeaders.hpp"
#include <windows.h>
//#############################################################################
//####################### MStringRepresentation #########################
//#############################################################################
#define DELETE_FILL_CHAR ('0')
size_t
MStringRepresentation::allocationIncrement = 8;
//
//#############################################################################
//#############################################################################
//
inline size_t
MStringRepresentation::CalculateSize(size_t needed)
{
//
// calculate the allocation size for a string
//
Verify(allocationIncrement);
size_t x =
((needed + allocationIncrement) / allocationIncrement) *
allocationIncrement;
return x;
}
//
//#############################################################################
//#############################################################################
//
MStringRepresentation::MStringRepresentation()
{
stringLength = 0;
stringSize = 0;
stringText = NULL;
referenceCount = 0;
}
//
//#############################################################################
//#############################################################################
//
MStringRepresentation::MStringRepresentation(const MStringRepresentation &str)
{
Check_Object(&str);
stringLength = str.stringLength;
stringSize = str.stringSize;
if (str.stringText == NULL)
{
stringText = NULL;
}
else
{
stringText = new char[stringSize];
Register_Pointer(stringText);
Mem_Copy(stringText, str.stringText, stringLength + 1, stringSize);
}
referenceCount = 0;
}
//
//#############################################################################
//#############################################################################
//
MStringRepresentation::MStringRepresentation(const char *cstr)
{
if ((cstr == NULL) || (cstr[0] == '\x00'))
{
stringLength = 0;
stringSize = 0;
stringText = NULL;
}
else
{
stringLength = strlen(cstr);
stringSize = CalculateSize(stringLength);
stringText = new char [stringSize];
Register_Pointer(stringText);
Mem_Copy(stringText, cstr, stringLength + 1, stringSize);
}
referenceCount = 0;
}
//
//#############################################################################
//#############################################################################
//
MStringRepresentation::~MStringRepresentation()
{
if (stringText != NULL)
{
#if defined(_ARMOR)
memset(stringText, DELETE_FILL_CHAR, stringSize);
#endif
Unregister_Pointer(stringText);
delete[] stringText;
}
}
//
//#############################################################################
//#############################################################################
//
void
MStringRepresentation::TestInstance() const
{
#if 0
if (stringLength > 0 || stringText != NULL)
{
Check_Pointer(stringText);
size_t str_len = strlen(stringText);
Verify(stringLength == str_len);
}
#endif
}
//
//#############################################################################
//#############################################################################
//
void
MStringRepresentation::AllocateLength(size_t length)
{
Check_Object(this);
if (stringText != NULL)
{
#if defined(_ARMOR)
memset(stringText, DELETE_FILL_CHAR, stringSize);
#endif
Unregister_Pointer(stringText);
delete[] stringText;
}
if (!length)
{
stringLength = 0;
stringSize = 0;
stringText = NULL;
}
else
{
stringLength = length;
stringSize = CalculateSize(stringLength);
stringText = new char [stringSize];
Register_Pointer(stringText);
}
}
//
//#############################################################################
//#############################################################################
//
MStringRepresentation
MStringRepresentation::operator = (const MStringRepresentation &str)
{
Check_Object(this);
if (this == &str)
return *this;
if (stringText != NULL)
{
#if defined(_ARMOR)
memset(stringText, DELETE_FILL_CHAR, stringSize);
#endif
Unregister_Pointer(stringText);
delete[] stringText;
}
Check_Object(&str);
stringLength = str.stringLength;
stringSize = str.stringSize;
if (stringSize == 0)
{
stringText = NULL;
}
else
{
stringText = new char[stringSize];
Register_Pointer(stringText);
Mem_Copy(stringText, str.stringText, stringLength + 1, stringSize);
}
return *this;
}
//
//#############################################################################
//#############################################################################
//
MStringRepresentation
MStringRepresentation::operator = (const char *cstr)
{
Check_Object(this);
if (stringText != NULL)
{
#if defined(_ARMOR)
memset(stringText, DELETE_FILL_CHAR, stringSize);
#endif
Unregister_Pointer(stringText);
delete[] stringText;
}
if ((cstr == NULL) || (cstr[0] == '\x00'))
{
stringLength = 0;
stringSize = 0;
stringText = NULL;
}
else
{
stringLength = strlen(cstr);
stringSize = CalculateSize(stringLength);
stringText = new char [stringSize];
Register_Pointer(stringText);
Mem_Copy(stringText, cstr, stringLength + 1, stringSize);
}
return *this;
}
//
//#############################################################################
//#############################################################################
//
MStringRepresentation
Stuff::operator + (
const MStringRepresentation &str1,
const MStringRepresentation &str2
)
{
Check_Object(&str1);
Check_Object(&str2);
MStringRepresentation temp;
unsigned long totalLen =
str1.stringLength + str2.stringLength;
if (totalLen == 0)
{
return temp;
}
temp.stringLength = 0;
temp.stringSize = MStringRepresentation::CalculateSize((size_t)totalLen);
temp.stringText = new char[temp.stringSize];
Register_Pointer(temp.stringText);
Verify(temp.stringSize >= 1);
temp.stringText[0] = '\000';
if (str1.stringText != NULL)
{
Mem_Copy(temp.stringText, str1.stringText, str1.stringLength + 1, temp.stringSize);
temp.stringLength = str1.stringLength;
}
if (str2.stringText != NULL)
{
Verify(temp.stringLength < temp.stringSize);
Mem_Copy(
&temp.stringText[temp.stringLength],
str2.stringText,
str2.stringLength + 1,
temp.stringSize - temp.stringLength
);
temp.stringLength += str2.stringLength;
}
Check_Object(&temp);
return temp;
}
//
//#############################################################################
//#############################################################################
//
MStringRepresentation
Stuff::operator + (const MStringRepresentation &str, char ch)
{
Check_Object(&str);
MStringRepresentation temp;
if (str.stringText == NULL)
{
temp.stringLength = 1;
temp.stringSize = MStringRepresentation::allocationIncrement;
temp.stringText = new char [temp.stringSize];
Register_Pointer(temp.stringText);
Verify(temp.stringSize >= 2);
temp.stringText[0] = ch;
temp.stringText[1] = '\000';
}
else
{
Verify(str.stringLength != UINT_MAX);
temp.stringLength = str.stringLength + 1;
if (temp.stringLength == str.stringSize)
temp.stringSize = str.stringSize + MStringRepresentation::allocationIncrement;
else
temp.stringSize = str.stringSize;
temp.stringText = new char[temp.stringSize];
Register_Pointer(temp.stringText);
Mem_Copy(temp.stringText, str.stringText, str.stringLength, temp.stringSize);
Verify(str.stringLength < temp.stringSize);
Verify(temp.stringLength < temp.stringSize);
temp.stringText[str.stringLength] = ch;
temp.stringText[temp.stringLength] = '\000';
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -