在实际开发过程中,C++string类使用起来有很多不方便的地方,笔者根据根据这些不足简单的扩展了这个类,如增加与数字之间的相互转化和格式化字符串。不足的地方望指正。读者也可以根据自己需求继续扩展。
将C语言梳理一下,分布在以下10个章节中:
/*
Author: wuqiang
Email: debugroot@126.com
Description:exstring is a subclass of basic_string.It is added some userful
operations,such as toUpper,toLower,toNumber,fromNumber,format,etc.It can also
convert between basic_string seamlessly,which is very important for compatibility.
And it is almostly a wrapper of some C++ standard library,so there should be no bugs.
If you find some,please let me known.It is totally free,you can use it in any way you desire.
*/
#pragma once
#include
#include
#include
#include
#include
using namespace std;
#ifndef INLINE
#define INLINE inline
#endif //INLINE
static ios_base::fmtflags BaseFlag(int base)
{
return (base == 16) (ios_base::hex) :
( (base == 8) (ios_base::oct) : (ios_base::dec) );
}
template struct ex_char_traits
{
};
template<> struct ex_char_traits
{
static INLINE int ct_vscprintf(const char* format, va_list argptr )
{
return _vscprintf(format, argptr);
}
static INLINE int ct_vstprintf_s(char* buffer, size_t numberOfElements,
const char* format, va_list argptr)
{
return vsprintf_s(buffer, numberOfElements, format, argptr);
}
};
template<> struct ex_char_traits
{
static INLINE int ct_vscprintf(const wchar_t* format, va_list argptr )
{
return _vscwprintf(format, argptr);
}
static INLINE int ct_vstprintf_s(wchar_t* buffer, size_t numberOfElements,
const wchar_t* format, va_list argptr)
{
return vswprintf_s(buffer, numberOfElements, format, argptr);
}
};
template
Type ConvertToNumber(basic_stringstream<_Elem, _Traits, _Ax>& ss,
Type t, int base)
{
ss.setf(BaseFlag(base), ios_base::basefield);
ss >> t;
return t;
}
template
float ConvertToNumber(basic_stringstream<_Elem, _Traits, _Ax>& ss,
float t, int/*ignore base*/)
{
ss >> t;
return t;
}
template
double ConvertToNumber(basic_stringstream<_Elem, _Traits, _Ax>& ss,
double t, int/*ignore base*/)
{
ss >> t;
return t;
}
template
class basic_exstring : public basic_string<_Elem, _Traits, _Ax>
{
public:
typedef basic_exstring<_Elem, _Traits, _Ax, _ExTraits> _Myt;
typedef basic_string<_Elem, _Traits, _Ax> _Mybase;
#pragma region "constructor"
//所有构造函数的行为同basic_string
explicit INLINE _Myt(const _Ax& al = _Ax())
:_Mybase(al)
{
}
INLINE _Myt(const _Myt& rhs)
:_Mybase(rhs)
{
}
INLINE _Myt(const _Myt& rhs, size_type pos, size_type n,const _Ax& al = _Ax())
:_Mybase(rhs, pos, n, al)
{
}
INLINE _Myt(const _Elem *s, size_type n, const _Ax& al = _Ax())
:_Mybase(s, n, al)
{
}
INLINE _Myt(const _Elem *s, const _Ax& al = _Ax())
:_Mybase(s, al)
{
}
INLINE _Myt(size_type n, _Elem c, const _Ax& al = _Ax())
:_Mybase(n, c, al)
{
}
INLINE _Myt(const_iterator first, const_iterator last,const _Ax& al = _Ax())
:_Mybase(first, last, al)
{
}
//string(wstring)转化为exstring(exwstring)
INLINE _Myt(const _Mybase& base)
:_Mybase(base)
{
}
#pragma endregion //constructor
#pragma region "general operation"
//所有字符转为大写,改变自身
_Myt& toUpper()
{
transform(begin(), end(), begin(), toupper);
return *this;
}
//所有字符转为大写,不改变自身
_Myt toUpper() const
{
_Myt s;
transform(begin(), end(), s.begin(), toupper);
return s;
}
//所有字符转为小写,改变自身
_Myt& toLower()
{
transform(begin(), end(), begin(), tolower);
return *this;
}
//所有字符转为大写,不改变自身
_Myt toLower() const
{
_Myt s(_Mysize, _Elem());
transform(begin(), end(), s.begin(), tolower);
return s;
}
//将所有oldStr替换为newStr
_Myt& replace(const _Myt& oldStr, const _Myt& newStr)
{
if (oldStr.empty())
re