自定义c++字符串(二)

#include<string.h>
#include<assert.h>
#include <malloc.h>
#include<iostream>
using namespace std;
/*
* 备注:
* 1、内存申请相关函数概念
  <1> alloca是向栈申请内存,因此无需释放.
  <2> malloc分配的内存是位于堆中的,并且没有初始化内存的内容,因此基本上malloc之后,调用函数memset来初始化这部分的内存空间.
  <3> calloc则将初始化这部分的内存,设置为0.
  <4> realloc则对malloc申请的内存进行大小的调整.
  <5> 申请的内存最终需要通过函数free来释放.
* 2、该运算符把expression转换为type-id类型,但没有运行时类型检查来保证转换的安全性
* 3、 strchr() 将会找出 str 字符串中第一次出现的字符 c 的地址,然后将该地址返回。
* 4、 strcat() 会将参数 src 字符串复制到参数 dest 所指的字符串尾部;dest 最后的结束字符 NULL 会被覆盖掉,并在连接后的字符串的尾部再增加一个 NULL
* 5、 operator const char*() const是类型转换函数的定义
*/
namespace UiLib {
enum { MAX_LOCAL_STRING_LEN = 20, MAX_LOCAL_SUBSTR_LEN = 20 };
class UIString {
public:
char* m_pc;
char m_buffer[MAX_LOCAL_STRING_LEN + 1];
public:
// 构造函数
UIString() : m_pc(m_buffer){
m_buffer[0] = '\0';
}
UIString(const char ch) : m_pc(m_buffer){
m_buffer[0] = ch;
m_buffer[1] = '\0';
}
UIString(char* pc, int nLen) : m_pc(m_buffer){
m_buffer[0] = '\0';
assign(pc, nLen);
}
UIString(const UIString& src) : m_pc(m_buffer) {
m_buffer[0] = '\0';
assign(src.m_pc, -1);
}
~UIString(){
if (m_pc != m_buffer) free(m_pc);
}
// 赋值
void assign(char* pc, int cchMax) {
if (isEmpty(pc)) {
if (m_pc != m_buffer) {
free(m_pc);
m_pc = m_buffer;
}
m_buffer[0] = '\0';
return;
}
cchMax = (cchMax < 0 ?  (int)strlen(pc) : cchMax < (int) strlen(pc)?cchMax:strlen(pc));
if (cchMax >= MAX_LOCAL_STRING_LEN) {
if (m_pc == m_buffer) {
m_pc = NULL;
}
m_pc = static_cast<char*>(realloc(m_pc, (cchMax + 1) * sizeof(char)));
} else {
if (m_pc != m_buffer) {
free(m_pc);
m_pc = m_buffer;
}
}
strncpy(m_buffer, pc, cchMax);
m_buffer[cchMax] = '\0';
}
// 增
void append(char* pc) {
if (isEmpty(pc)) {
return;
}
int nNewLength = getLength() + (int)strlen(pc);
if (nNewLength >= MAX_LOCAL_STRING_LEN) {
if (m_pc == m_buffer) {
m_pc = static_cast<char*>(malloc((nNewLength + 1) * sizeof(char)));
strcpy(m_pc, m_buffer);
strcat(m_pc, pc);
} else {
m_pc = static_cast<char*>(realloc(m_pc, (nNewLength + 1) * sizeof(char)));
strcat(m_pc, pc);
}
} else {
if (m_pc != m_buffer) {
free(m_pc);
m_pc = m_buffer;
}
strcat(m_buffer, pc); // 将pc的指追加到m_buffer的末尾
}
}
void append(char c) {
int nNewLength = getLength() + 1;
if (nNewLength >= MAX_LOCAL_STRING_LEN) {
if (m_pc == m_buffer) {
m_pc = static_cast<char*>(malloc((nNewLength + 1) * sizeof(char)));
strcpy(m_pc, m_buffer);
m_pc[nNewLength - 2] = c;
m_pc[nNewLength - 1] = '\0';
}
else {
m_pc = static_cast<char*>(realloc(m_pc, (nNewLength + 1) * sizeof(char)));
m_pc[nNewLength - 2] = c;
m_pc[nNewLength - 1] = '\0';
}
}
else {
if (m_pc != m_buffer) {
free(m_pc);
m_pc = m_buffer;
}
m_pc[nNewLength - 2] = c;
m_pc[nNewLength - 1] = '\0';
}
}
// 比较
int compare(char* pc) const {
return strcmp(m_pc, pc);
}
int compareNoCase(char* pc) const {
return _stricmp(m_pc, pc);
}
// 改
int replace(char* pcFrom, char* pcTo) {
// UIString sTemp;
int nCount = 0;
int iPos = find(pcFrom, 0);
if (iPos < 0) return 0;
int cchFrom = (int)strlen(pcFrom);
int cchTo = (int)strlen(pcTo);
while (iPos >= 0) {
UIString sTemp(m_pc, iPos);
sTemp += pcTo;
sTemp += mid(iPos + cchFrom, getLength());
assign(sTemp, - 1);
iPos = find(pcFrom, iPos + cchTo);
nCount++;
}
return nCount;
}
UIString left(int iLength) const {
if (iLength < 0) iLength = 0;
if (iLength > getLength()) iLength = getLength();
return UIString(m_pc, iLength);
}
UIString mid(int iPos, int iLength) const {
if (iLength < 0) iLength = getLength() - iPos;
if (iPos + iLength > getLength()) iLength = getLength() - iPos;
if (iLength <= 0) return UIString();
return UIString(m_pc + iPos, iLength);
}
UIString right(int iLength) const {
int iPos = getLength() - iLength;
if (iPos < 0) {
iPos = 0;
iLength = getLength();
}
return UIString(m_pc + iPos, iLength);
}
void setAt(int nIndex, char ch) {
// assert(nIndex >= 0 && nIndex<getLength());
m_pc[nIndex] = ch;
}
// 查
char getAt(int nIndex) const {
return m_pc[nIndex];
}
int find(char ch, int iPos /*= 0*/) const {
if (iPos != 0 && (iPos < 0 || iPos >= getLength())) return -1;
char* cp = strchr(m_pc + iPos, ch);
if (cp == NULL) return -1;
return (int)(cp - m_pc);
}
int find(char* pcSub, int iPos /*= 0*/) const {
if (iPos != 0 && (iPos < 0 || iPos >= getLength())) return -1;
char* cp = strstr(m_pc + iPos, pcSub);
if (cp == NULL) return -1;
return (int)(cp - m_pc);
}
int rFind(char ch) const {
// char* pc = strchr(m_pc, ch);
int len = getLength();
int pos = len - 1;
while (pos >= 0 && !isEmpty(m_pc[pos])) {
if (m_pc[pos] == ch) {
break;
} else {
pos--;
}
}
return pos;
// if (pc == NULL) return -1;
// return (int)(pc - m_pc);
}
// 操作符重载
operator char*() const{return m_pc;}
bool operator == (char* pc) const { return (compare(pc) == 0); }
bool operator != (char* pc) const { return (compare(pc) != 0); }
bool operator <= (char* pc) const { return (compare(pc) <= 0); }
bool operator <  (char* pc) const { return (compare(pc) <  0); }
bool operator >= (char* pc) const { return (compare(pc) >= 0); }

bool operator >  (char* pc) const { return (compare(pc) >  0); }

char operator[] (int nIndex)  const {
assert(nIndex < getLength());
return m_pc[nIndex];
}
const UIString& operator=(char* pc) {
if (!isEmpty(pc)) {
assign(pc, -1);
} else {
empty();
}
return *this;
}
const UIString& operator=(const char ch) {
empty();
if (!isEmpty(ch)) {
m_buffer[0] = ch;
m_buffer[1] = '\0';
}
return *this;
}
UIString operator+(const UIString & src) const {
UIString sTemp = *this;
if (!isEmpty(src)) {
sTemp.append(src);
}
return sTemp;
}
UIString operator+(char c) const {
if (!isEmpty(c)) {
UIString sTemp = *this;
sTemp.append(c);
return sTemp;
}
return *this;
}
const UIString& operator+=(const UIString& src) {
if (!isEmpty(src)) {
append(src);
}
return *this;
}
const UIString& operator+=(char* pc) { 
if (!isEmpty(pc)) {
append(pc);
}
return *this;
}
const UIString& operator+=(const char ch) {
if (!isEmpty(ch)) {
char str[] = { ch, '\0' };
append(str);
}
return *this;
}
// 子串操作
char* subStr(const int start, const int length) const{
assert(start >= 0 && getLength() > start + length);
char* pc = static_cast<char*>(malloc((length + 1) * sizeof(char)));
strncpy(pc, m_pc + start, length);
pc[length] = '\0';
return pc;
}
// 基本操作
void makeUpper() {
_strupr(m_pc);
}
void makeLower() { 
_strlwr(m_pc);
}
bool isEmpty() const { 
return NULL == m_pc || m_pc[0] == '\0';
}
bool isEmpty(char* pc) const { 
return NULL == pc || pc[0] == '\0';
}
bool isEmpty(char c) const{ 
return NULL == c;
}
void empty() {           
if (m_pc != m_buffer) {
free(m_pc);
m_pc = m_buffer;
}
m_buffer[0] = '\0';
}
int getLength() const { 
return strlen(m_pc);
}
char* getData() const { 
return m_pc;
}
};
} // namespace UiLib
int main() {
UiLib::UIString uiString, aStr;
uiString.append("abMabceeehiijMabc");
uiString.replace("Mab", "NNN");
cout << uiString.getData() << endl;
char* c = uiString.subStr(2, 3);
cout << c << endl;
free(c);
system("pause");
return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值