String/Windows_String/String.hpp

572 lines
8.7 KiB
C++
Raw Permalink Normal View History

#pragma once
#include <iostream>
#include <cassert>
#include <cstring>
namespace Lenyiin
{
// ʵ<><CAB5>һ<EFBFBD><D2BB>֧<EFBFBD><D6A7><EFBFBD><EFBFBD>ɾ<EFBFBD><C9BE><EFBFBD>ĵ<EFBFBD> string
class String
{
public:
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
typedef char* iterator;
typedef const char* const_iterator;
typedef char* riterator;
typedef const char* const_riterator;
iterator begin() // <20><><EFBFBD><EFBFBD><EFBFBD>׵<EFBFBD>ַ
{
return _str;
}
iterator end() // <20><><EFBFBD><EFBFBD>β<EFBFBD><CEB2>ַ
{
return _str + _size;
}
const_iterator begin() const
{
return _str;
}
const_iterator end() const
{
return _str + _size;
}
riterator rbegin()
{
return _str + _size - 1;
}
riterator rend()
{
return _str - 1;
}
const_riterator rbegin() const
{
return _str + _size - 1;
}
const_riterator rend() const
{
return _str - 1;
}
public:
//// Ĭ<>Ϲ<EFBFBD><CFB9><EFBFBD><ECBAAF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
//String()
// : _str(nullptr), _size(0), _capacity(0)
//{
//}
//// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ĺ<EFBFBD><C4B9><EFBFBD><ECBAAF><EFBFBD><EFBFBD><EFBFBD><EFBFBD>C<EFBFBD><43><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>String<6E><67><EFBFBD><EFBFBD>
//String(const char* str) {
// if (str) {
// _size = _capacity = strlen(str);
// _str = new char[_size + 1]; // <20><><EFBFBD><EFBFBD><EFBFBD>ڴ棬+1<><31>Ϊ<EFBFBD>˴<CBB4><E6B4A2>ֹ<EFBFBD><D6B9>'\0'
// strcpy(_str, str); // <20><><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݸ<EFBFBD><DDB8>Ƶ<EFBFBD>dataָ<61><D6B8><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD>
// }
// else {
// _str = nullptr;
// _size = _capacity = 0;
// }
//}
// <20><><EFBFBD><EFBFBD>д<EFBFBD><D0B4>
// Ĭ<>Ϲ<EFBFBD><CFB9><EFBFBD><ECBAAF>
String(const char* str = "") // Ĭ<>Ϲ<EFBFBD><CFB9><EFBFBD><ECBAAF>
: _str(new char[strlen(str) + 1]), _size(strlen(str)), _capacity(_size)
{
strcpy(_str, str);
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><ECBAAF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EEBFBD>
//String(const String& s) // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><ECBAAF>
// : _str(new char[s._capacity + 1]), _size(s._size), _capacity(s._capacity)
//{
// strcpy(_str, s._str);
//}
// <20><><EFBFBD><EFBFBD>д<EFBFBD><D0B4>
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <09><><EFBFBD><EFBFBD>Ĭ<EFBFBD>Ϲ<EFBFBD><CFB9><EFBFBD>
String(const String& s)
: String(s._str)
{}
// <20><>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD>
//String &operator=(const String &s)
//{
// if (this != &s)
// {
// char *tmp = new char[s._capacity + 1];
// strcpy(tmp, s._str);
// delete[] _str;
// _str = tmp;
// _size = s._size;
// _capacity = s._capacity;
// }
// return *this;
//}
// <20><><EFBFBD><EFBFBD>д<EFBFBD><D0B4>
// <20><>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
void Swap(String& s)
{
std::swap(_str, s._str);
std::swap(_size, s._size);
std::swap(_capacity, s._capacity);
}
String& operator=(String s)
{
this->Swap(s);
return *this;
}
String& operator=(const char* str)
{
String s(str);
this->Swap(s);
return *this;
}
// <20>ƶ<EFBFBD><C6B6><EFBFBD><EFBFBD><EFBFBD><ECBAAF>
String(String&& s) noexcept
: _str(s._str), _size(s._size), _capacity(s._capacity)
{
s._str = nullptr;
s._size = s._capacity = 0;
}
// <20>ƶ<EFBFBD><C6B6><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
String& operator=(String&& s) noexcept
{
if (this != &s) {
delete[] _str; // <20>ͷŵ<CDB7>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD>
_str = s._str; // <20>ӹ<EFBFBD><D3B9><EFBFBD>Դ
_size = s._size;
_capacity = s._capacity;
s._str = nullptr; // <20><>other<65><72><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ΪĬ<CEAA><C4AC>״̬
s._size = s._capacity = 0;
}
return *this;
}
// <20><><EFBFBD><EFBFBD> [] <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
char& operator[](size_t pos)
{
assert(pos < _size);
return _str[pos];
}
const char& operator[](size_t pos) const
{
assert(pos < _size);
return _str[pos];
}
// <20><>ȡ˽<C8A1>г<EFBFBD>Ա
size_t size() const // <20><>ȡ<EFBFBD><C8A1>Ч<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD>
{
return _size;
}
size_t capacity() const // <20><>ȡ<EFBFBD><C8A1>Ч<EFBFBD><D0A7><EFBFBD><EFBFBD>
{
return _capacity;
}
const char* c_str() const // <20><><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
{
return _str;
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
~String()
{
delete[] _str;
_str = nullptr;
_size = _capacity = 0;
}
// <20><><EFBFBD>ٿռ<D9BF>
void reserve(size_t newcapacity)
{
if (newcapacity > _capacity)
{
char* newstr = new char[newcapacity + 1];
strcpy(newstr, _str);
delete[] _str;
_str = newstr;
_capacity = newcapacity;
}
}
// <20><><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD>С, <20><><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC>
void resize(size_t newsize, char ch = '\0')
{
if (newsize < _size)
{
_size = newsize;
_str[_size] = '\0';
}
else
{
if (newsize > _capacity)
{
reserve(newsize);
}
for (size_t i = _size; i < newsize; i++)
{
_str[i] = ch;
}
_size = newsize;
_str[_size] = '\0';
}
}
// β<><CEB2>
void push_back(char ch) // β<><CEB2>һ<EFBFBD><D2BB><EFBFBD>ַ<EFBFBD>
{
if (_size == _capacity)
{
size_t newcapacity = _capacity == 0 ? 4 : _capacity * 2;
reserve(newcapacity);
}
_str[_size++] = ch;
_str[_size] = '\0';
}
// ׷<><D7B7>
void append(const char ch)
{
push_back(ch);
}
void append(const char* str) // ׷<><D7B7>һ<EFBFBD><D2BB><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
{
size_t len = strlen(str);
if (_size + len > _capacity)
{
reserve(_size + len);
}
strcpy(_str + _size, str);
_size += len;
}
void append(const String& s) // ׷<><D7B7>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
{
append(s._str);
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> += <20><><EFBFBD><EFBFBD>
String& operator+=(const char ch)
{
this->push_back(ch);
return *this;
}
String& operator+=(const char* str)
{
this->append(str);
return *this;
}
String& operator+=(const String& s)
{
append(s);
return *this;
}
// <20><><EFBFBD><EFBFBD>λ<EFBFBD>ò<EFBFBD><C3B2><EFBFBD><EFBFBD>ַ<EFBFBD>
String& insert(size_t pos, char ch)
{
assert(pos <= _size);
if (_size == _capacity)
{
size_t newcapacity = _capacity == 0 ? 4 : _capacity * 2;
reserve(newcapacity);
}
int end = _size;
while (end >= (int)pos)
{
_str[end + 1] = _str[end];
--end;
}
_str[pos] = ch;
++_size;
return *this;
}
// <20><><EFBFBD><EFBFBD>λ<EFBFBD>ò<EFBFBD><C3B2><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
String& insert(size_t pos, const char* str)
{
assert(pos <= _size);
// 1. <20><><EFBFBD><EFBFBD><EFBFBD>ռ<D5BC><E4B2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
size_t len = strlen(str);
if (_size + len > _capacity)
{
reserve(_size + len);
}
// 2. Ų<><C5B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
int end = _size;
while (end >= (int)pos)
{
_str[end + len] = _str[end];
--end;
}
// 3. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
strncpy(_str + pos, str, len);
_size += len;
return *this;
}
// <20><><EFBFBD><EFBFBD>λ<EFBFBD>ò<EFBFBD><C3B2><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
String& insert(size_t pos, const String& s)
{
return insert(pos, s._str);
}
// ɾ<><C9BE>
String& erase(size_t pos = 0, size_t len = npos)
{
assert(pos < _size);
if (len >= _size - pos)
{
_str[pos] = '\0';
_size = pos;
}
else
{
while (pos <= _size - len)
{
_str[pos] = _str[pos + len];
++pos;
}
_size -= len;
}
return *this;
}
// βɾ
void pop_back()
{
assert(_size > 0);
--_size;
_str[_size] = '\0';
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> < <20><><EFBFBD><EFBFBD>
bool operator<(const String& s)
{
int ret = strcmp(_str, s._str);
return ret < 0;
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> == <20><><EFBFBD><EFBFBD>
bool operator==(const String& s)
{
int ret = strcmp(_str, s._str);
return ret == 0;
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <= <20><><EFBFBD><EFBFBD>
bool operator<=(const String& s)
{
return *this < s || *this == s;
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> > <20><><EFBFBD><EFBFBD>
bool operator>(const String& s)
{
int ret = strcmp(_str, s._str);
return ret > 0;
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> >= <20><><EFBFBD><EFBFBD>
bool operator>=(const String& s)
{
return !(*this < s);
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> != <20><><EFBFBD><EFBFBD>
bool operator!=(const String& s)
{
return !(*this == s);
}
// <20><><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD>
size_t find(char ch, size_t pos = 0) const
{
for (size_t i = pos; i < _size; i++)
{
if (_str[i] == ch)
{
return i;
}
}
return npos;
}
// <20><><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
size_t find(const char* str, size_t pos = 0) const
{
char* p = strstr(_str, str);
if (p == nullptr)
{
return npos;
}
else
{
return p - _str;
}
}
// <20><><EFBFBD>Ҷ<EFBFBD><D2B6><EFBFBD>
size_t find(const String& s, size_t pos = 0) const
{
return find(s._str, pos);
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
size_t rfind(char ch, size_t pos = npos) const
{
if (pos == npos)
{
pos = _size - 1;
}
for (int i = pos; i >= 0; i--)
{
if (_str[i] == ch)
{
return i;
}
}
return npos;
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
size_t rfind(const char* str, size_t pos = npos)
{
size_t len = strlen(str);
if (len > _size)
{
return npos;
}
if (pos >= _size)
{
pos = _size - 1;
}
for (size_t i = pos; i != npos; i--)
{
if (strncmp(_str + i, str, len) == 0)
{
return i;
}
}
return npos;
}
String substr(size_t pos = 0, size_t len = npos) const
{
assert(pos < _size);
if (len == npos || len + pos > _size)
{
len = _size - pos;
}
char* buffer = new char[len + 1];
strncpy(buffer, _str + pos, len);
buffer[len] = '\0';
return String(buffer);
}
// <20><><EFBFBD><EFBFBD>
void clear()
{
_size = 0;
_str[_size] = '\0';
}
private:
char* _str;
size_t _size; // <20><>¼<EFBFBD>Ѿ<EFBFBD><D1BE><EFBFBD>˶<EFBFBD><CBB6><EFBFBD><EFBFBD><EFBFBD>Ч<EFBFBD>ַ<EFBFBD>
size_t _capacity; // <20><>¼<EFBFBD>ܴ洢<DCB4><E6B4A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ч<EFBFBD>ַ<EFBFBD> '\0' <20><>β <09><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA>Ч<EFBFBD>ַ<EFBFBD>
public:
static size_t npos; // size_t -> unsigned long long -1 <20><>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
};
size_t String::npos = -1;
std::ostream& operator<<(std::ostream& _cout, const String& s)
{
for (size_t i = 0; i < s.size(); i++)
{
_cout << s[i];
}
return _cout;
}
std::istream& operator>>(std::istream& _cin, String& s)
{
while (true)
{
char ch;
//_cin >> ch;
ch = _cin.get();
//if (ch == '\n') // ͨ<><CDA8><EFBFBD><EFBFBD><EFBFBD>зָ<D0B7>
if (ch == ' ' || ch == '\n')
{
break;
}
else
{
s += ch;
}
}
return _cin;
}
}