📄 nstring.h
字号:
free((void*) this->string);
this->string = ptr;
this->strLen = tlen;
}
else if (this->localString[0])
{
if (tlen >= LOCALSTRINGSIZE)
{
char* ptr = (char*) malloc(tlen + 1);
strcpy(ptr, this->localString);
strncat(ptr, str, numChars);
this->localString[0] = 0;
this->string = ptr;
}
else
{
strncat(this->localString, str, numChars);
}
this->strLen = tlen;
}
else
{
this->Set(str, numChars);
}
}
}
//------------------------------------------------------------------------------
/**
*/
inline
void
nString::Append(const char* str)
{
ASSERT(str);
ushort rlen = strlen(str);
this->AppendRange(str, rlen);
}
//------------------------------------------------------------------------------
/**
*/
inline
void
nString::Append(const nString& str)
{
this->Append(str.Get());
}
//------------------------------------------------------------------------------
/**
*/
inline
void
nString::AppendInt(int val)
{
nString str;
str.SetInt(val);
this->Append(str);
}
//------------------------------------------------------------------------------
/**
*/
inline
void
nString::AppendFloat(float val)
{
nString str;
str.SetFloat(val);
this->Append(str);
}
//------------------------------------------------------------------------------
/**
*/
inline
nString&
nString::operator+=(const char* rhs)
{
this->Append(rhs);
return *this;
}
//------------------------------------------------------------------------------
/**
*/
inline
nString&
nString::operator+=(const nString& rhs)
{
this->Append(rhs.Get());
return *this;
}
//------------------------------------------------------------------------------
/**
*/
inline
bool
operator == (const nString& a, const nString& b)
{
return strcmp(a.Get(), b.Get()) == 0;
}
//------------------------------------------------------------------------------
/**
*/
inline
bool
operator != (const nString& a, const nString& b)
{
return strcmp(a.Get(), b.Get()) != 0;
}
//------------------------------------------------------------------------------
/**
*/
inline
const char
nString::operator[](int i) const
{
ASSERT((0 <= i) && (i <= (strLen - 1)));
if (this->string != 0)
{
return this->string[i];
}
else
{
return this->localString[i];
}
}
//------------------------------------------------------------------------------
/**
*/
inline
char&
nString::operator[](int i)
{
ASSERT((0 <= i) && (i <= (strLen - 1)));
if (this->string != 0)
{
return this->string[i];
}
else
{
return this->localString[i];
}
}
//------------------------------------------------------------------------------
/**
*/
inline
int
nString::Length() const
{
return this->strLen;
}
//------------------------------------------------------------------------------
/**
*/
inline
void
nString::Clear()
{
this->Delete();
}
//------------------------------------------------------------------------------
/**
*/
inline
bool
nString::IsEmpty() const
{
if (this->string && (this->string[0] != 0))
{
return false;
}
else if (this->localString[0] != 0)
{
return false;
}
return true;
}
//------------------------------------------------------------------------------
/**
*/
inline
void
nString::ToLower()
{
char* str = (char*) (this->string ? this->string : this->localString);
if (str)
{
char c;
char* ptr = (char*) str;
while ((c = *ptr))
{
*ptr++ = tolower(c);
}
}
}
//------------------------------------------------------------------------------
/**
*/
inline
void
nString::ToUpper()
{
char* str = (char*) (this->string ? this->string : this->localString);
if (str)
{
char c;
char* ptr = (char*) str;
while ((c = *ptr))
{
*ptr++ = toupper(c);
}
}
}
//------------------------------------------------------------------------------
/**
*/
static
inline
nString
operator+(const nString& s0, const nString& s1)
{
nString newString = s0;
newString.Append(s1.Get());
return newString;
}
//------------------------------------------------------------------------------
/**
Get the first token in the string, delimited by one or more of the
character in 'whiteSpace'. This simply invokes the strtok() function
internally, and will destroy the contained string. After calling
GetFirstToken(), invoke GetNextToken() until 0 returns.
ATTENTION: if somebody else calls strtok() while GetFirstToken()/
GetNextToken() is underway, everything will break apart!
Check out the Tokenize() method for a better alternative.
@param whiteSpace string containing white space characters
*/
inline
const char*
nString::GetFirstToken(const char* whiteSpace)
{
ASSERT(whiteSpace);
return strtok((char*) this->Get(), whiteSpace);
}
//------------------------------------------------------------------------------
/**
Get the next token in the string. Call this after a GetFirstToken()
or GetNextToken(). Returns 0 if no more tokens in string. This method
will destroy the original string.
ATTENTION: if somebody else calls strtok() while GetFirstToken()/
GetNextToken() is underway, everything will break apart!
Check out the Tokenize() method for a better alternative.
@param whiteSpace string containing whitespace characters
*/
inline
const char*
nString::GetNextToken(const char* whiteSpace)
{
ASSERT(whiteSpace);
return strtok(0, whiteSpace);
}
//------------------------------------------------------------------------------
/**
Tokenize the string into a provided nString array. Returns the number
of tokens. This method is recommended over GetFirstToken()/GetNextToken(),
since it is atomic. This nString object will not be destroyed
(as is the case with GetFirstToken()/GetNextToken().
@param whiteSpace [in] a string containing the whitespace characters
@param tokens [out] nArray<nString> where tokens will be appended
@return number of tokens found
*/
inline
int
nString::Tokenize(const char* whiteSpace, nArray<nString>& tokens) const
{
int numTokens = 0;
// create a temporary string, which will be destroyed during the operation
nString str(*this);
char* ptr = (char*) str.Get();
const char* token;
while (0 != (token = strtok(ptr, whiteSpace)))
{
tokens.Append(nString(token));
ptr = 0;
numTokens++;
}
return numTokens;
}
//------------------------------------------------------------------------------
/**
Extract sub string.
*/
inline
nString
nString::ExtractRange(int from, int numChars) const
{
ASSERT(from <= this->strLen);
ASSERT((from + numChars) <= this->strLen);
const char* str = this->Get();
nString newString;
newString.Set(&(str[from]), numChars);
return newString;
}
//------------------------------------------------------------------------------
/**
Terminates the string at the first occurance of one of the characters
in charSet.
*/
inline
void
nString::Strip(const char* charSet)
{
ASSERT(charSet);
char* str = (char*) this->Get();
char* ptr = strpbrk(str, charSet);
if (ptr)
{
*ptr = 0;
}
}
//------------------------------------------------------------------------------
/**
@result Index or -1 if not found.
*/
inline
int
nString::IndexOf(const nString& v, int startIndex) const
{
ASSERT(0 <= startIndex && startIndex <= Length() - 1);
ASSERT(!v.IsEmpty());
for (int i = startIndex; i < Length(); i++)
{
if (Length() - i < v.Length())
{
break;
}
if (strncmp(&(Get()[i]), v.Get(), v.Length()) == 0)
{
return i;
}
}
return -1;
}
//------------------------------------------------------------------------------
/**
Return index of character in string, or -1 if not found.
*/
inline
int
nString::FindChar(unsigned char c, int startIndex) const
{
if (this->Length() > 0)
{
ASSERT(startIndex < this->Length());
const char* ptr = strchr(this->Get() + startIndex, c);
if (ptr)
{
return ptr - this->Get();
}
}
return -1;
}
//------------------------------------------------------------------------------
/**
Terminates the string at the given index.
*/
inline
void
nString::TerminateAtIndex(int index)
{
ASSERT(index < this->Length());
char* ptr = (char*) this->Get();
ptr[index] = 0;
}
//------------------------------------------------------------------------------
/**
Strips last slash, if the path name ends on a slash.
*/
inline
void
nString::StripTrailingSlash()
{
if (this->strLen > 0)
{
int pos = strLen - 1;
char* str = (char*) this->Get();
if ((str[pos] == '/') || (str[pos] == '\\'))
{
str[pos] = 0;
}
}
}
//------------------------------------------------------------------------------
/**
Returns a new string which is this string, stripped on the left side
by all characters in the char set.
*/
inline
nString
nString::TrimLeft(const char* charSet) const
{
ASSERT(charSet);
if (this->IsEmpty())
{
return *this;
}
int charSetLen = strlen(charSet);
int thisIndex = 0;
bool stopped = false;
while (!stopped && (thisIndex < this->strLen))
{
int charSetIndex;
bool match = false;
for (charSetIndex = 0; charSetIndex < charSetLen; charSetIndex++)
{
if ((*this)[thisIndex] == charSet[charSetIndex])
{
// a match
match = true;
break;
}
}
if (!match)
{
// stop if no match
stopped = true;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -