C 字串類原始碼

2021-04-15 17:07:05 字數 3999 閱讀 9434

參考了c++builder中的ansistring, 實現了常用的功能,其它需要的可以自己新增.

/* cesstring.h

* 類似ansistring 的乙個類, 用標準c++編寫, 可以用在vc和cb中

*/#ifndef _cesstring_h_

#define _cesstring_h_

#include #include #include #include namespace cesstring

; lptstr m_psztext;

protected:

void __fastcall allocatebuffer(const uint ulen, bool brealloc = false)

}else

pstrrec->m_ubufferlen = ulen;

m_psztext = (tchar *)(pstrrec + 1);

if (!brealloc)

m_psztext[ulen] = _t('\0'); // set last character in buffer to 0

}void __fastcall safedelete()

}public:

cesstring() : m_psztext(null)

{};cesstring(const cesstring & src)

;cesstring(lpctstr pszsrc, uint ulen = 0)

;~cesstring()

;public:

strrec & __fastcall getrec() const

;uint __fastcall length() const

;inline void __fastcall settextlength(uint ulen) const

// c string operator

tchar* __fastcall c_str() const

bool __fastcall isempty() const

// modify string

cesstring & __fastcall insert(const cesstring& str, uint uindex)

uint ustrlen = lstrlen(str);

uint usrclen = length();

uint upos = uindex - 1;

this->allocatebuffer(ustrlen + usrclen, true);

memmove(m_psztext + upos + ustrlen, m_psztext + upos, usrclen - upos);

memcpy(m_psztext + upos, str.m_psztext, ustrlen);

settextlength(ustrlen + usrclen);

return *this;

}cesstring& __fastcall delete(uint uindex, uint count)

if (upos + count == length())

else

return *this;

}cesstring& __fastcall setlength(uint unewlength)

else

return *this;

}int __fastcall pos(const cesstring& substr) const

cesstring __fastcall lowercase() const

cesstring __fastcall uppercase() const

cesstring __fastcall trim() const

cesstring __fastcall trimleft() const

return tmp;

}cesstring __fastcall trimright() const

return tmp;

}cesstring __fastcall substring(uint index, uint count) const

int __fastcall toint() const

long lvalue = _tcstol(m_psztext, &pstop, nbase);

if (errno == erange || *pstop != _t('\0'))

return lvalue;

}int __fastcall tointdef(int defaultvalue) const

long lvalue = _tcstol(m_psztext, &pstop, nbase);

if (errno == erange || *pstop != _t('\0'))

return lvalue;

}double __fastcall todouble() const

return dvalue;

}#undef vprintf

#undef printf

#undef sprintf

int __cdecl cesstring::vprintf(lpctstr format, va_list paramlist)

int __cdecl cesstring::cat_vprintf(lpctstr format, va_list paramlist)

int __cdecl cesstring::printf(lpctstr format, ...)

cesstring& __cdecl cesstring::sprintf(lpctstr format, ...)

int __cdecl cesstring::cat_printf(lpctstr format, ...)

cesstring& __cdecl cesstring::cat_sprintf(lpctstr format, ...)

public:

operator lpctstr() const

// pointer to string

const cesstring& operator= (lpctstr psztext) // set a new text

const cesstring& operator= (const cesstring &str) // set a new text

const cesstring& operator+=(lpctstr psztext) // add a text

const cesstring& operator+=(cesstring str) // add a text

const cesstring operator+(lpctstr psztext) // add a text

const cesstring operator+(cesstring str) // add a text

// comparisons

bool operator ==(const cesstring& str) const

bool operator !=(const cesstring& str) const

bool operator <(const cesstring& str) const

bool operator >(const cesstring& str) const

bool operator <=(const cesstring& str) const

bool operator >=(const cesstring& str) const

tchar& operator (const uint idx)

return m_psztext[idx-1];

}};}

using namespace cesstring;

#endif //_cesstring_h_

左旋字串原始碼

include using namespace std void reverse char pbegin,char pend char reversesentence char pdata else if pend pend 0 else return pdata void leftreverse ...

字串處理函式原始碼

size t lm strlen const char str size t lm strlen2 const char str char lm strcpy char dest,const char src char lm strncpy char dest,const char src,size...

SDS字串原始碼分析

0.前言 這裡對redis底層字串的實現分析,但是看完其實現還沒有完整的乙個概念,即不太清楚作者為什麼要這樣子設計,只能窺知一點,需要看完redis如何使用再回頭來體會,有不足之處還望告知。涉及檔案 sds.h sds.c 1.資料結構 1 typedef char sds 23 struct sd...