无聊,写了一下unicode的string实现。
//删除,插入,查找,替换,取子串, Trim, 迭代器
// 还差 替换和 char转换unicode, char或许是utf-8, int 和保留功能有点重合
class String
{
typedef struct _format_args
{
int prec;
int width;
wchar_t fill;
}_fargs;
public:
String()
:_size(0), _capacity(_size+1), _data(new wchar_t[_capacity])
{
*_data = L'\0';
}
// 容器需要
String(const String& rhs)
:_size(rhs._size), _capacity(_size+1), _data(new wchar_t[_capacity])
{
_copy(_data, rhs._data, _size);
}
String(const wchar_t *str)
{
for(_size = 0; str[_size]; ++_size);
_capacity = _size + 1;
_data = new wchar_t[_capacity];
_copy(_data, str, _size);
}
String(const wchar_t ch)
:_size(1), _capacity(_size+1), _data(new wchar_t[_capacity])
{
*_data = ch;
*(_data+1) = L'\0';
}
//这里有必要么?。
String(int val)
:_size(0), _capacity(12), _data(new wchar_t[_capacity])
{
bool sign = false;
if(val < 0){
sign = true;
val = -val;
}
wchar_t *p = _uitoa(_data, val, 10, sign);
_size = _data - p;
}
String(const char *str)
{
unsigned int code_page = 936;
_size = MultiByteToWideChar(code_page, 0, str, -1, NULL, 0);
_capacity = _size;
_data = new wchar_t[_size];
MultiByteToWideChar(code_page, 0, str, -1, _data, _size--);
}
~String()
{
delete[] _data;
}
//////////////////////////////////////////////////////
String& operator=(String rhs) // pass by value
{
swap(rhs);
_size = rhs._size;
_capacity = _size + 1;
return *this;
}
String operator+(const String &rhs)
{
String tmp(_data); //没有进行测同,因为无意义
tmp += rhs;
return tmp;
}
String& operator+=(const String& rhs)
{
_data = _renew(_data, _size, _size + rhs._size);
_copy(_data+_size, rhs._data, rhs._size);
_size += rhs._size;
_capacity = _size + 1;
return *this;
}
wchar_t& operator[](size_t index) const
{
return _data[index];
}
bool operator==(const String& rhs)
{
return _cmp(_data, rhs._data) == 0;
}
bool operator >(const String& rhs)
{
return _cmp(_data, rhs._data) > 0;
}
bool operator < (const String& rhs)
{
return _cmp(_data, rhs._data) < 0;
}
//删除,插入,查找,替换,取子串, Trim, 迭代器
// 还差 替换和 char转换unicode, char或许是utf-8, int 和保留功能有点重合
class String
{
typedef struct _format_args
{
int prec;
int width;
wchar_t fill;
}_fargs;
public:
String()
:_size(0), _capacity(_size+1), _data(new wchar_t[_capacity])
{
*_data = L'\0';
}
// 容器需要
String(const String& rhs)
:_size(rhs._size), _capacity(_size+1), _data(new wchar_t[_capacity])
{
_copy(_data, rhs._data, _size);
}
String(const wchar_t *str)
{
for(_size = 0; str[_size]; ++_size);
_capacity = _size + 1;
_data = new wchar_t[_capacity];
_copy(_data, str, _size);
}
String(const wchar_t ch)
:_size(1), _capacity(_size+1), _data(new wchar_t[_capacity])
{
*_data = ch;
*(_data+1) = L'\0';
}
//这里有必要么?。
String(int val)
:_size(0), _capacity(12), _data(new wchar_t[_capacity])
{
bool sign = false;
if(val < 0){
sign = true;
val = -val;
}
wchar_t *p = _uitoa(_data, val, 10, sign);
_size = _data - p;
}
String(const char *str)
{
unsigned int code_page = 936;
_size = MultiByteToWideChar(code_page, 0, str, -1, NULL, 0);
_capacity = _size;
_data = new wchar_t[_size];
MultiByteToWideChar(code_page, 0, str, -1, _data, _size--);
}
~String()
{
delete[] _data;
}
//////////////////////////////////////////////////////
String& operator=(String rhs) // pass by value
{
swap(rhs);
_size = rhs._size;
_capacity = _size + 1;
return *this;
}
String operator+(const String &rhs)
{
String tmp(_data); //没有进行测同,因为无意义
tmp += rhs;
return tmp;
}
String& operator+=(const String& rhs)
{
_data = _renew(_data, _size, _size + rhs._size);
_copy(_data+_size, rhs._data, rhs._size);
_size += rhs._size;
_capacity = _size + 1;
return *this;
}
wchar_t& operator[](size_t index) const
{
return _data[index];
}
bool operator==(const String& rhs)
{
return _cmp(_data, rhs._data) == 0;
}
bool operator >(const String& rhs)
{
return _cmp(_data, rhs._data) > 0;
}
bool operator < (const String& rhs)
{
return _cmp(_data, rhs._data) < 0;
}