📄 nstring.h
字号:
#ifndef N_STRING_H
#define N_STRING_H
//------------------------------------------------------------------------------
/**
@胶飘傅 努贰胶
@author
- RadonLabs GmbH
@since
- 2005.6.13
@remarks
- 瘤肯 眠啊
*/
//------------------------------------------------------------------------------
#pragma once
#include "../ProgramCommon/Define.h"
#include <ctype.h>
#include <string.h>
//#using <mscorlib.dll>
//using namespace System;
#include "Narray.h"
class nString
{
public:
/// constructor
nString();
/// constructor 1
nString(const char* str);
/// copy constructor
nString(const nString& rhs);
/// set to int val
nString(int intVal);
/// set to float val
nString(float floatVal);
/// destructor
~nString();
/// = operator
nString& operator=(const nString& rhs);
/// = operator with string
nString& operator=(const char* rhs);
/// += operator with char*
nString& operator+=(const char* rhs);
/// += operator with string
nString& operator+=(const nString& rhs);
/// Is `a' equal to `b'?
friend bool operator == (const nString& a, const nString& b);
/// Is `a' inequal to `b'?
friend bool operator != (const nString& a, const nString& b);
/// Subscript operator (read only).
const char operator[](int i) const;
/// Subscript operator (writeable).
char& operator[](int i);
/// set as char ptr, with explicit length
void Set(const char* ptr, int length);
/// set as char ptr
void Set(const char* str);
/// set as int value
void SetInt(int val);
/// set as float value
void SetFloat(float val);
/// get string as char ptr
const char* Get() const;
/// return content as integer
int AsInt() const;
/// return content as float
float AsFloat() const;
/// return length of string
int Length() const;
/// clear the string
void Clear();
/// return true if string object is empty
bool IsEmpty() const;
/// append character pointer
void Append(const char* str);
/// append string
void Append(const nString& str);
/// append a range of characters
void AppendRange(const char* str, uint numChars);
/// append int value
void AppendInt(int val);
/// append float value
void AppendFloat(float val);
/// convert string to lower case
void ToLower();
/// convert string to upper case
void ToUpper();
/// get first token (this will destroy the string)
const char* GetFirstToken(const char* whiteSpace);
/// get next token (this will destroy the string)
const char* GetNextToken(const char* whiteSpace);
/// tokenize string into a provided nString array
int Tokenize(const char* whiteSpace, nArray<nString>& tokens) const;
/// extract substring
nString ExtractRange(int from, int to) const;
/// terminate string at first occurence of character in set
void Strip(const char* charSet);
/// Index of first appearance of `v' starting from index `startIndex'.
int IndexOf(const nString& v, int startIndex) const;
/// return index of character in string
int FindChar(unsigned char c, int startIndex) const;
/// terminate string at given index
void TerminateAtIndex(int index);
/// strip slash at end of path, if exists
void StripTrailingSlash();
/// delete characters from charset at left side of string
nString TrimLeft(const char* charSet) const;
/// delete characters from charset at right side of string
nString TrimRight(const char* charSet) const;
/// trim characters from charset at both sides of string
nString Trim(const char* charSet) const;
/// substitute every occurance of a string with another string
nString Substitute(const char* str, const char* substStr) const;
/// convert string inplace from UTF-8 to 8-bit ANSI
void UTF8toANSI();
/// convert ANSI to UTF-8 in place
void ANSItoUTF8();
/// get pointer to extension (without the dot)
const char* GetExtension() const;
/// check if extension matches (no dot in the extension!)
bool CheckExtension(const char* ext) const;
/// convert backslashes to slashes
void ConvertBackslashes();
/// remove extension
void StripExtension();
/// extract the part after the last directory separator
nString ExtractFileName() const;
/// extract the last directory of the path
nString ExtractLastDirName() const;
/// extract the part before the last directory separator
nString ExtractDirName() const;
/// extract path until last slash
nString ExtractToLastSlash() const;
/// check if this string matches the given pattern
// bool MatchPattern(const nString& pattern) const;
/// 箭磊 牢瘤 八荤
bool IsNumberic();
protected:
/// copy contents
void Copy(const nString& src);
/// delete contents
void Delete();
/// get pointer to last directory separator
char* GetLastSlash() const;
enum
{
LOCALSTRINGSIZE = 14,
};
char* string;
char localString[LOCALSTRINGSIZE];
ushort strLen;
};
//------------------------------------------------------------------------------
/** global
*/
/*
bool
n_strmatch(const char* str, const char* pat)
{
char c2;
while (true)
{
if (*pat == 0)
{
if (*str == 0) return true;
else return false;
}
if ((*str == 0) && (*pat != '*')) return false;
if (*pat=='*')
{
pat++;
if (*pat==0) return true;
while (true)
{
if (n_strmatch(str, pat)) return true;
if (*str==0) return false;
str++;
}
}
if (*pat=='?') goto match;
if (*pat=='[')
{
pat++;
while (true)
{
if ((*pat==']') || (*pat==0)) return false;
if (*pat==*str) break;
if (pat[1] == '-')
{
c2 = pat[2];
if (c2==0) return false;
if ((*pat<=*str) && (c2>=*str)) break;
if ((*pat>=*str) && (c2<=*str)) break;
pat+=2;
}
pat++;
}
while (*pat!=']')
{
if (*pat==0)
{
pat--;
break;
}
pat++;
}
goto match;
}
if (*pat=='\\')
{
pat++;
if (*pat==0) return false;
}
if (*pat!=*str) return false;
match:
pat++;
str++;
}
}
*/
//------------------------------------------------------------------------------
/**
*/
inline
nString::nString() :
string(0),
strLen(0)
{
this->localString[0] = 0;
}
//------------------------------------------------------------------------------
/**
*/
inline
void
nString::Delete()
{
if (this->string)
{
free((void*) this->string);
this->string = 0;
}
this->localString[0] = 0;
}
//------------------------------------------------------------------------------
/**
*/
inline
nString::~nString()
{
this->Delete();
}
//------------------------------------------------------------------------------
/**
*/
inline
void
nString::Set(const char* str, int length)
{
this->Delete();
if (str)
{
this->strLen = length;
char* ptr = this->localString;
if (strLen >= LOCALSTRINGSIZE)
{
ptr = (char*) malloc(strLen + 1);
this->string = ptr;
}
else
{
ptr = this->localString;
}
int i;
for (i = 0; i < strLen; i++)
{
*ptr++ = *str++;
}
*ptr = 0;
}
}
//------------------------------------------------------------------------------
/**
*/
inline
void
nString::Set(const char* str)
{
int len = 0;
if (str)
{
len = strlen(str);
}
this->Set(str, len);
}
//------------------------------------------------------------------------------
/**
*/
inline
void
nString::SetInt(int val)
{
char buf[128];
_snprintf(buf, sizeof(buf), "%d", val);
this->Set(buf);
}
//------------------------------------------------------------------------------
/**
*/
inline
void
nString::SetFloat(float val)
{
char buf[128];
_snprintf(buf, sizeof(buf), "%.6f", val);
this->Set(buf);
}
//------------------------------------------------------------------------------
/**
*/
inline
void
nString::Copy(const nString& src)
{
ASSERT(0 == this->string);
const char* str = src.Get();
if (str)
{
this->Set(str);
}
}
//------------------------------------------------------------------------------
/**
*/
inline
nString::nString(const char* str) :
string(0),
strLen(0)
{
this->localString[0] = 0;
this->Set(str);
}
//------------------------------------------------------------------------------
/**
*/
inline
nString::nString(const nString& rhs) :
string(0),
strLen(0)
{
this->localString[0] = 0;
this->Copy(rhs);
}
//------------------------------------------------------------------------------
/**
*/
inline
nString::nString(int intVal) :
string(0),
strLen(0)
{
this->SetInt(intVal);
}
//------------------------------------------------------------------------------
/**
*/
inline
nString::nString(float floatVal) :
string(0),
strLen(0)
{
this->SetFloat(floatVal);
}
//------------------------------------------------------------------------------
/**
*/
inline
const char*
nString::Get() const
{
if (this->string)
{
return this->string;
}
else if (this->localString[0])
{
return this->localString;
}
else
{
return "";
}
}
//------------------------------------------------------------------------------
/**
*/
inline
nString&
nString::operator=(const nString& rhs)
{
if (&rhs != this)
{
this->Delete();
this->Copy(rhs);
}
return *this;
}
//------------------------------------------------------------------------------
/**
*/
inline
nString&
nString::operator=(const char* rhs)
{
this->Set(rhs);
return *this;
}
//------------------------------------------------------------------------------
/**
*/
inline
void
nString::AppendRange(const char* str, uint numChars)
{
ASSERT(str);
if (numChars > 0)
{
ushort rlen = numChars;
ushort tlen = this->strLen + rlen;
if (this->string)
{
char* ptr = (char*) malloc(tlen + 1);
strcpy(ptr, this->string);
strncat(ptr, str, numChars);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -