[Projct64] Clean up std string.cpp
This commit is contained in:
parent
832248d818
commit
bc8d8b8fa1
|
@ -6,49 +6,49 @@ stdstr::stdstr()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
stdstr::stdstr( const std::string & str ) :
|
stdstr::stdstr(const std::string & str) :
|
||||||
std::string(str)
|
std::string(str)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
stdstr::stdstr( const stdstr & str ) :
|
stdstr::stdstr(const stdstr & str) :
|
||||||
std::string((const std::string &)str)
|
std::string((const std::string &)str)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
stdstr::stdstr( const char * str ) :
|
stdstr::stdstr(const char * str) :
|
||||||
std::string(str ? str : "")
|
std::string(str ? str : "")
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
strvector stdstr::Tokenize(const char * delimiter) const
|
strvector stdstr::Tokenize(const char * delimiter) const
|
||||||
{
|
{
|
||||||
strvector tokens;
|
strvector tokens;
|
||||||
|
|
||||||
stdstr::size_type lastPos = find_first_not_of(delimiter, 0);
|
stdstr::size_type lastPos = find_first_not_of(delimiter, 0);
|
||||||
stdstr::size_type pos = find_first_of(delimiter, lastPos);
|
stdstr::size_type pos = find_first_of(delimiter, lastPos);
|
||||||
while (stdstr::npos != pos || stdstr::npos != lastPos)
|
while (stdstr::npos != pos || stdstr::npos != lastPos)
|
||||||
{
|
{
|
||||||
tokens.push_back(substr(lastPos, pos - lastPos));
|
tokens.push_back(substr(lastPos, pos - lastPos));
|
||||||
lastPos = find_first_not_of(delimiter, pos);
|
lastPos = find_first_not_of(delimiter, pos);
|
||||||
pos = find_first_of(delimiter, lastPos);
|
pos = find_first_of(delimiter, lastPos);
|
||||||
}
|
}
|
||||||
return tokens;
|
return tokens;
|
||||||
}
|
}
|
||||||
|
|
||||||
strvector stdstr::Tokenize(char delimiter) const
|
strvector stdstr::Tokenize(char delimiter) const
|
||||||
{
|
{
|
||||||
strvector tokens;
|
strvector tokens;
|
||||||
|
|
||||||
stdstr::size_type lastPos = find_first_not_of(delimiter, 0);
|
stdstr::size_type lastPos = find_first_not_of(delimiter, 0);
|
||||||
stdstr::size_type pos = find_first_of(delimiter, lastPos);
|
stdstr::size_type pos = find_first_of(delimiter, lastPos);
|
||||||
while (stdstr::npos != pos || stdstr::npos != lastPos)
|
while (stdstr::npos != pos || stdstr::npos != lastPos)
|
||||||
{
|
{
|
||||||
tokens.push_back(substr(lastPos, pos - lastPos));
|
tokens.push_back(substr(lastPos, pos - lastPos));
|
||||||
lastPos = find_first_not_of(delimiter, pos);
|
lastPos = find_first_not_of(delimiter, pos);
|
||||||
pos = find_first_of(delimiter, lastPos);
|
pos = find_first_of(delimiter, lastPos);
|
||||||
}
|
}
|
||||||
return tokens;
|
return tokens;
|
||||||
}
|
}
|
||||||
|
|
||||||
void stdstr::ArgFormat(const char * strFormat, va_list & args)
|
void stdstr::ArgFormat(const char * strFormat, va_list & args)
|
||||||
|
@ -56,188 +56,190 @@ void stdstr::ArgFormat(const char * strFormat, va_list & args)
|
||||||
#pragma warning(push)
|
#pragma warning(push)
|
||||||
#pragma warning(disable:4996)
|
#pragma warning(disable:4996)
|
||||||
|
|
||||||
size_t nlen = _vscprintf( strFormat, args ) + 1;
|
size_t nlen = _vscprintf(strFormat, args) + 1;
|
||||||
char * buffer = (char *)alloca(nlen * sizeof(char));
|
char * buffer = (char *)alloca(nlen * sizeof(char));
|
||||||
buffer[nlen - 1] = 0;
|
buffer[nlen - 1] = 0;
|
||||||
if(buffer != NULL)
|
if (buffer != NULL)
|
||||||
{
|
{
|
||||||
vsprintf( buffer, strFormat , args );
|
vsprintf(buffer, strFormat, args);
|
||||||
*this = buffer;
|
*this = buffer;
|
||||||
}
|
}
|
||||||
#pragma warning(pop)
|
#pragma warning(pop)
|
||||||
}
|
}
|
||||||
|
|
||||||
void stdstr::Format(const char * strFormat, ...)
|
void stdstr::Format(const char * strFormat, ...)
|
||||||
{
|
{
|
||||||
va_list args;
|
va_list args;
|
||||||
va_start(args, strFormat);
|
va_start(args, strFormat);
|
||||||
ArgFormat(strFormat,args);
|
ArgFormat(strFormat, args);
|
||||||
va_end(args);
|
va_end(args);
|
||||||
}
|
}
|
||||||
|
|
||||||
stdstr& stdstr::ToLower(void)
|
stdstr& stdstr::ToLower(void)
|
||||||
{
|
{
|
||||||
std::transform(begin(), end(), begin(), (int(*)(int)) tolower);
|
std::transform(begin(), end(), begin(), (int(*)(int)) tolower);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
stdstr& stdstr::ToUpper(void)
|
stdstr& stdstr::ToUpper(void)
|
||||||
{
|
{
|
||||||
std::transform(begin(), end(), begin(), (int(*)(int)) toupper);
|
std::transform(begin(), end(), begin(), (int(*)(int)) toupper);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
void stdstr::Replace(const char search, const char replace )
|
void stdstr::Replace(const char search, const char replace)
|
||||||
{
|
{
|
||||||
std::string& str = *this;
|
std::string& str = *this;
|
||||||
std::string::size_type pos = str.find( search );
|
std::string::size_type pos = str.find(search);
|
||||||
while ( pos != std::string::npos )
|
while (pos != std::string::npos)
|
||||||
{
|
{
|
||||||
str.replace( pos, 1, &replace );
|
str.replace(pos, 1, &replace);
|
||||||
pos = str.find( search, pos + 1 );
|
pos = str.find(search, pos + 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void stdstr::Replace(const char * search, const char replace )
|
void stdstr::Replace(const char * search, const char replace)
|
||||||
{
|
{
|
||||||
std::string& str = *this;
|
std::string& str = *this;
|
||||||
std::string::size_type pos = str.find( search );
|
std::string::size_type pos = str.find(search);
|
||||||
size_t SearchSize = strlen(search);
|
size_t SearchSize = strlen(search);
|
||||||
while ( pos != std::string::npos )
|
while (pos != std::string::npos)
|
||||||
{
|
{
|
||||||
str.replace( pos, SearchSize, &replace );
|
str.replace(pos, SearchSize, &replace);
|
||||||
pos = str.find( search, pos + 1 );
|
pos = str.find(search, pos + 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void stdstr::Replace(const std::string& search, const std::string& replace )
|
void stdstr::Replace(const std::string& search, const std::string& replace)
|
||||||
{
|
{
|
||||||
std::string& str = *this;
|
std::string& str = *this;
|
||||||
std::string::size_type pos = str.find( search );
|
std::string::size_type pos = str.find(search);
|
||||||
size_t SearchSize = search.size();
|
size_t SearchSize = search.size();
|
||||||
while ( pos != std::string::npos )
|
while (pos != std::string::npos)
|
||||||
{
|
{
|
||||||
str.replace( pos, SearchSize, replace );
|
str.replace(pos, SearchSize, replace);
|
||||||
pos = str.find( search, pos + replace.length() );
|
pos = str.find(search, pos + replace.length());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
stdstr & stdstr::TrimLeft (const char * chars2remove)
|
stdstr & stdstr::TrimLeft(const char * chars2remove)
|
||||||
{
|
{
|
||||||
if (!empty())
|
if (!empty())
|
||||||
{
|
{
|
||||||
std::string::size_type pos = find_first_not_of(chars2remove);
|
std::string::size_type pos = find_first_not_of(chars2remove);
|
||||||
if (pos != std::string::npos)
|
if (pos != std::string::npos)
|
||||||
{
|
{
|
||||||
erase(0,pos);
|
erase(0, pos);
|
||||||
} else {
|
}
|
||||||
erase(begin(), end()); // make empty
|
else {
|
||||||
}
|
erase(begin(), end()); // make empty
|
||||||
}
|
}
|
||||||
return *this;
|
}
|
||||||
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
stdstr & stdstr::TrimRight (const char * chars2remove)
|
stdstr & stdstr::TrimRight(const char * chars2remove)
|
||||||
{
|
{
|
||||||
if (!empty())
|
if (!empty())
|
||||||
{
|
{
|
||||||
std::string::size_type pos = find_last_not_of(chars2remove);
|
std::string::size_type pos = find_last_not_of(chars2remove);
|
||||||
if (pos != std::string::npos)
|
if (pos != std::string::npos)
|
||||||
{
|
{
|
||||||
erase(pos+1);
|
erase(pos + 1);
|
||||||
} else {
|
}
|
||||||
erase(begin(), end()); // make empty
|
else {
|
||||||
}
|
erase(begin(), end()); // make empty
|
||||||
}
|
}
|
||||||
return *this;
|
}
|
||||||
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
stdstr & stdstr::Trim (const char * chars2remove)
|
stdstr & stdstr::Trim(const char * chars2remove)
|
||||||
{
|
{
|
||||||
if (!empty())
|
if (!empty())
|
||||||
{
|
{
|
||||||
std::string::size_type pos = find_first_not_of(chars2remove);
|
std::string::size_type pos = find_first_not_of(chars2remove);
|
||||||
if (pos != std::string::npos)
|
if (pos != std::string::npos)
|
||||||
{
|
{
|
||||||
erase(0,pos);
|
erase(0, pos);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
erase(begin(), end()); // make empty
|
erase(begin(), end()); // make empty
|
||||||
}
|
}
|
||||||
|
|
||||||
pos = find_last_not_of(chars2remove);
|
pos = find_last_not_of(chars2remove);
|
||||||
if (pos != std::string::npos)
|
if (pos != std::string::npos)
|
||||||
{
|
{
|
||||||
erase(pos+1);
|
erase(pos + 1);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
erase(begin(), end()); // make empty
|
erase(begin(), end()); // make empty
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
stdstr & stdstr::FromUTF16 ( const wchar_t * UTF16Source, bool * bSuccess )
|
stdstr & stdstr::FromUTF16(const wchar_t * UTF16Source, bool * bSuccess)
|
||||||
{
|
{
|
||||||
bool bConverted = false;
|
bool bConverted = false;
|
||||||
|
|
||||||
if (UTF16Source == NULL)
|
if (UTF16Source == NULL)
|
||||||
{
|
{
|
||||||
*this = "";
|
*this = "";
|
||||||
bConverted = true;
|
bConverted = true;
|
||||||
}
|
}
|
||||||
else if(wcslen(UTF16Source) > 0)
|
else if (wcslen(UTF16Source) > 0)
|
||||||
{
|
{
|
||||||
DWORD nNeeded = WideCharToMultiByte(CP_UTF8, 0, UTF16Source, -1, NULL, 0, NULL, NULL);
|
DWORD nNeeded = WideCharToMultiByte(CP_UTF8, 0, UTF16Source, -1, NULL, 0, NULL, NULL);
|
||||||
if(nNeeded > 0)
|
if (nNeeded > 0)
|
||||||
{
|
{
|
||||||
char * buf = (char *)alloca(nNeeded + 1);
|
char * buf = (char *)alloca(nNeeded + 1);
|
||||||
if( buf != NULL )
|
if (buf != NULL)
|
||||||
{
|
{
|
||||||
memset(buf, 0, nNeeded + 1);
|
memset(buf, 0, nNeeded + 1);
|
||||||
|
|
||||||
nNeeded = WideCharToMultiByte(CP_UTF8, 0, UTF16Source, -1, buf, nNeeded, NULL, NULL);
|
nNeeded = WideCharToMultiByte(CP_UTF8, 0, UTF16Source, -1, buf, nNeeded, NULL, NULL);
|
||||||
if (nNeeded)
|
if (nNeeded)
|
||||||
{
|
{
|
||||||
*this = buf;
|
*this = buf;
|
||||||
bConverted = true;
|
bConverted = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (bSuccess)
|
if (bSuccess)
|
||||||
{
|
{
|
||||||
*bSuccess = bConverted;
|
*bSuccess = bConverted;
|
||||||
}
|
}
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::wstring stdstr::ToUTF16 ( bool * bSuccess)
|
std::wstring stdstr::ToUTF16(bool * bSuccess)
|
||||||
{
|
{
|
||||||
bool bConverted = false;
|
bool bConverted = false;
|
||||||
std::wstring res;
|
std::wstring res;
|
||||||
|
|
||||||
DWORD nNeeded = MultiByteToWideChar(CP_UTF8, 0, this->c_str(), (int)this->length(), NULL, 0);
|
DWORD nNeeded = MultiByteToWideChar(CP_UTF8, 0, this->c_str(), (int)this->length(), NULL, 0);
|
||||||
if(nNeeded > 0)
|
if (nNeeded > 0)
|
||||||
{
|
{
|
||||||
wchar_t * buf = (wchar_t *)alloca((nNeeded + 1) * sizeof(wchar_t));
|
wchar_t * buf = (wchar_t *)alloca((nNeeded + 1) * sizeof(wchar_t));
|
||||||
if( buf != NULL )
|
if (buf != NULL)
|
||||||
{
|
{
|
||||||
memset(buf, 0, (nNeeded + 1) * sizeof(wchar_t));
|
memset(buf, 0, (nNeeded + 1) * sizeof(wchar_t));
|
||||||
|
|
||||||
nNeeded = MultiByteToWideChar(CP_UTF8, 0, this->c_str(), (int)this->length(), buf, nNeeded);
|
nNeeded = MultiByteToWideChar(CP_UTF8, 0, this->c_str(), (int)this->length(), buf, nNeeded);
|
||||||
if (nNeeded)
|
if (nNeeded)
|
||||||
{
|
{
|
||||||
res = buf;
|
res = buf;
|
||||||
bConverted = true;
|
bConverted = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (bSuccess)
|
if (bSuccess)
|
||||||
{
|
{
|
||||||
*bSuccess = bConverted;
|
*bSuccess = bConverted;
|
||||||
}
|
}
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
|
@ -9,63 +9,62 @@ class stdstr;
|
||||||
|
|
||||||
typedef std::vector<stdstr> strvector;
|
typedef std::vector<stdstr> strvector;
|
||||||
|
|
||||||
class stdstr :
|
class stdstr :
|
||||||
public std::string
|
public std::string
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
stdstr();
|
stdstr();
|
||||||
stdstr( const std::string & str );
|
stdstr(const std::string & str);
|
||||||
stdstr( const stdstr & str );
|
stdstr(const stdstr & str);
|
||||||
stdstr( const char * str );
|
stdstr(const char * str);
|
||||||
|
|
||||||
strvector Tokenize ( char delimiter ) const;
|
strvector Tokenize(char delimiter) const;
|
||||||
strvector Tokenize ( const char * delimiter ) const;
|
strvector Tokenize(const char * delimiter) const;
|
||||||
void Format ( const char * strFormat, ... );
|
void Format(const char * strFormat, ...);
|
||||||
stdstr& ToLower ( void );
|
stdstr& ToLower(void);
|
||||||
stdstr& ToUpper ( void );
|
stdstr& ToUpper(void);
|
||||||
|
|
||||||
void Replace ( const char search, const char replace );
|
void Replace(const char search, const char replace);
|
||||||
void Replace ( const char * search, const char replace );
|
void Replace(const char * search, const char replace);
|
||||||
void Replace ( const std::string & search, const std::string & replace );
|
void Replace(const std::string & search, const std::string & replace);
|
||||||
|
|
||||||
stdstr & Trim ( const char * chars2remove = "\t " );
|
stdstr & Trim(const char * chars2remove = "\t ");
|
||||||
stdstr & TrimLeft ( const char * chars2remove = "\t " );
|
stdstr & TrimLeft(const char * chars2remove = "\t ");
|
||||||
stdstr & TrimRight ( const char * chars2remove = "\t " );
|
stdstr & TrimRight(const char * chars2remove = "\t ");
|
||||||
|
|
||||||
stdstr & FromUTF16 ( const wchar_t * UTF16Source, bool * bSuccess = NULL);
|
stdstr & FromUTF16(const wchar_t * UTF16Source, bool * bSuccess = NULL);
|
||||||
std::wstring ToUTF16 ( bool * bSuccess = NULL);
|
std::wstring ToUTF16(bool * bSuccess = NULL);
|
||||||
|
|
||||||
void ArgFormat( const char * strFormat, va_list & args);
|
void ArgFormat(const char * strFormat, va_list & args);
|
||||||
|
};
|
||||||
|
|
||||||
};
|
class stdstr_f : public stdstr
|
||||||
|
|
||||||
class stdstr_f: public stdstr
|
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
stdstr_f(const char * strFormat, ...)
|
stdstr_f(const char * strFormat, ...)
|
||||||
{
|
{
|
||||||
va_list args;
|
va_list args;
|
||||||
va_start(args, strFormat);
|
va_start(args, strFormat);
|
||||||
ArgFormat(strFormat,args);
|
ArgFormat(strFormat, args);
|
||||||
va_end(args);
|
va_end(args);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
class stdwstr_f : public std::wstring
|
class stdwstr_f : public std::wstring
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
stdwstr_f(const wchar_t * strFormat, ...)
|
stdwstr_f(const wchar_t * strFormat, ...)
|
||||||
{
|
{
|
||||||
va_list args;
|
va_list args;
|
||||||
va_start(args, strFormat);
|
va_start(args, strFormat);
|
||||||
|
|
||||||
wchar_t Msg[1000];
|
wchar_t Msg[1000];
|
||||||
_vsnwprintf(Msg, sizeof(Msg) - 1, strFormat, args);
|
_vsnwprintf(Msg, sizeof(Msg) - 1, strFormat, args);
|
||||||
|
|
||||||
va_end(args);
|
va_end(args);
|
||||||
|
|
||||||
this->assign(Msg);
|
this->assign(Msg);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef std::list<stdstr> strlist;
|
typedef std::list<stdstr> strlist;
|
||||||
|
|
Loading…
Reference in New Issue