6#ifndef _ZFI_zfstring_h_
7#define _ZFI_zfstring_h_
17template<
typename T_Char>
23template<
typename T_Char>
24zfint _ZFP_zfstring_cmp(
25 ZF_IN const T_Char *s1
26 ,
ZF_IN const T_Char *s2
28 while(*s1 && *s2 && *s1 == *s2) {++s1, ++s2;}
31template<
typename T_Char>
32zfint _ZFP_zfstring_ncmp(
33 ZF_IN const T_Char *s1
34 ,
ZF_IN const T_Char *s2
37 while(--len && *s1 && *s2 && *s1 == *s2) {++s1, ++s2;}
45inline zfint _ZFP_zfstring_cmp(
51inline zfint _ZFP_zfstring_ncmp(
59template<
typename T_Char>
70 _ZFP_zfstringD(
void) : refCount(1), capacity(0), length(0), d() {
71 static T_Char buf[1] = {0};
75template<
typename T_Char>
78 _ZFP_zfstringH(
void) : d(
zfpoolNew(_ZFP_zfstringD<T_Char>)) {}
81 _ZFP_zfstringD<T_Char> *d;
88template<
typename T_Char>
111 this->
assign(s.cString(), len);
119 if(pos < s.length()) {
120 if(len > s.length() - pos) {
121 len = s.length() - pos;
123 this->
assign(s.cString() + pos, len);
153 this->
assign(s + pos, len);
165 if(d->refCount == 1) {
178 inline operator const T_Char * (void)
const {
179 return this->isEmpty() ?
zfnull : this->cString();
184 _ZFP_zfstringD<T_Char> *dTmp = d;
187 if(dTmp->refCount == 1) {
202 zfbool operator == (
ZF_IN const T_Char *ref)
const {
return (this->compare(ref) == 0);}
203 zfbool operator != (
ZF_IN const T_Char *ref)
const {
return (this->compare(ref) != 0);}
220 template<
typename T_Int>
222 return this->cString() + offset;
234 _prepareWrite(this->
length());
241 return d->d.ptr[pos];
245 template<
typename T_Int>
246 inline T_Char operator [] (
ZF_IN T_Int pos)
const {
247 return this->get(pos);
258 _ZFP_zfstringD<T_Char> *dTmp = d;
266 _prepareWrite(d->length + 1);
267 d->d.buf[d->length] = c;
268 d->d.buf[++(d->length)] =
'\0';
274 _appendImpl(s.cString(), s.length());
283 len = (len <= s.length() ? len : s.length());
285 _appendImpl(s.cString(), len);
295 if(offset > s.length()) {
298 if(len > s.length() - offset) {
299 len = s.length() - offset;
302 _appendImpl(s.cString() + offset, len);
313 len = _ZFP_zfstring_len((
const T_Char *)s);
326 _prepareWrite(d->length + len);
327 zfmemcpy(d->d.buf + d->length, s, len *
sizeof(T_Char));
328 d->d.buf[d->length += (
zfuint)len] =
'\0';
334 this->operator = (s);
342 return this->
assign(s.cString(), len <= s.length() ? len : s.length());
350 if(offset > s.length()) {
353 if(len > s.length() - offset) {
354 len = s.length() - offset;
356 return this->
assign(s.cString() + offset, len);
365 len = _ZFP_zfstring_len((
const T_Char *)s);
372 if(len >= d->capacity || d->refCount > 1
373 || (d->capacity >= 128 && len < d->
capacity / 3)
378 zfmemcpy(d->d.buf, s, len *
sizeof(T_Char));
379 d->d.buf[len] =
'\0';
398 return this->
insert(insertAt, s.cString(), s.length());
403 ,
ZF_IN const void *s
406 if(insertAt >= this->
length()) {
411 len = _ZFP_zfstring_len((
const T_Char *)s);
415 _prepareWrite(lenTmp + len);
416 d->length = (
zfuint)(lenTmp + len);
417 zfmemmove(d->d.buf + insertAt + len, d->d.buf + insertAt, (lenTmp - insertAt) *
sizeof(T_Char));
418 zfmemcpy(d->d.buf + insertAt, s, len *
sizeof(T_Char));
419 d->d.buf[d->length] =
'\0';
432 return this->
replace(replacePos, replaceLen, s.cString(), s.length());
438 ,
ZF_IN const void *s
441 if(replacePos >= this->
length()) {
446 if(replaceLen > lenTmp - replacePos) {
447 replaceLen = lenTmp - replacePos;
450 len = _ZFP_zfstring_len((
const T_Char *)s);
452 if(len > replaceLen) {
453 _prepareWrite(lenTmp + len - replaceLen);
456 _prepareWrite(lenTmp);
458 zfmemmove(d->d.buf + replacePos + len, d->d.buf + replacePos + replaceLen, (lenTmp - replacePos - replaceLen) *
sizeof(T_Char));
459 zfmemcpy(d->d.buf + replacePos, s, len *
sizeof(T_Char));
460 d->length = (
zfuint)(lenTmp + len - replaceLen);
461 d->d.buf[d->length] =
'\0';
477 return d->length == 0;
493 if(d->capacity >= 128 && d->length < d->capacity / 3) {
504 if(len > lenTmp - pos) {
508 _prepareWrite(lenTmp);
509 zfmemmove(d->d.buf + pos, d->d.buf + pos + len, (lenTmp - pos - len) *
sizeof(T_Char));
511 d->d.buf[d->length] =
'\0';
519 if(d->refCount == 1) {
520 if(d->capacity >= 128) {
526 else if(d->capacity > 0) {
546 return d == s.d ? 0 : _ZFP_zfstring_cmp(this->
cString(), s.cString());
550 ZF_IN const T_Char *s
554 len = s ? _ZFP_zfstring_len(s) : 0;
556 if(this->
length() < len) {
559 else if(this->
length() > len) {
563 return _ZFP_zfstring_ncmp(this->
cString(), s, len);
581 _prepareWrite(this->
length());
582 T_Char *ret = d->d.buf;
599 _prepareWrite(this->
length());
606 _prepareWrite(this->length());
611 _ZFP_zfstringD<T_Char> *d;
624 ZF_IN const void *sLiteral
627 _ZFP_zfstringD<T_Char> *d =
zfpoolNew(_ZFP_zfstringD<T_Char>);
628 d->d.ptr = (
const T_Char *)sLiteral;
638 static _ZFP_zfstringD<T_Char> *_ZFP_Empty(
void) {
639 static _ZFP_zfstringH<T_Char> d;
644 capacity = ((capacity / 16) + 1) * 16;
646 else if(capacity < 64) {
647 capacity = ((capacity / 32) + 1) * 32;
649 else if(capacity < 1024) {
650 capacity = ((capacity / 128) + 1) * 128;
653 capacity = ((capacity / 1024) + 1) * 1024;
658 _capacityOptimize(capacity);
660 if(d->refCount == 1) {
661 if(d->capacity > 0) {
662 d->d.buf = (T_Char *)
zfrealloc(d->d.buf, capacity *
sizeof(T_Char));
665 const T_Char *ptr = d->d.ptr;
666 d->d.buf = (T_Char *)
zfmalloc(capacity *
sizeof(T_Char));
667 if(ptr && keepContents) {
668 zfmemcpy(d->d.buf, ptr, capacity *
sizeof(T_Char));
676 _ZFP_zfstringD<T_Char> *dTmp = d;
678 d->length = dTmp->length;
679 d->d.buf = (T_Char *)
zfmalloc(capacity *
sizeof(T_Char));
680 if(dTmp->length > 0 && keepContents) {
681 zfmemcpy(d->d.buf, dTmp->d.ptr, dTmp->length *
sizeof(T_Char));
682 d->d.buf[dTmp->length] =
'\0';
688 d->capacity = (
zfuint)capacity;
694 if(len >= d->capacity || d->refCount > 1) {
695 _capacityChange(len,
zftrue);
704 #define zftext(s) zfstring::shared(s)
#define ZFCoreMutexLocker()
util method to lock current block
Definition ZFCoreMutex.h:95
zfindex zfslen(const zfchar *s)
strlen wrapper as zfchar type
Definition ZFCoreTypeDef_CharType.h:144
_ZFT_t_zfchar zfchar
char wrapper
Definition ZFCoreTypeDef_CharType.h:17
zfint zfsncmp(const zfchar *s1, const zfchar *s2, zfindex count)
strncmp wrapper as zfchar type
Definition ZFCoreTypeDef_CharType.h:158
zfint zfscmp(const zfchar *s1, const zfchar *s2)
strcmp wrapper as zfchar type
Definition ZFCoreTypeDef_CharType.h:152
#define zfclassLikePOD
shows the class is not a POD type, but you may use it like a POD except memset it to 0
Definition ZFCoreTypeDef_ClassType.h:41
#define zffree(ptr)
same as free defined for future use, do nothing if ptr is NULL
Definition ZFCoreTypeDef_ClassType.h:112
#define ZF_IN
dummy macro that shows the param used as required input
Definition ZFCoreTypeDef_ClassType.h:180
#define ZF_IN_OPT
dummy macro that shows the param used as optional input
Definition ZFCoreTypeDef_ClassType.h:184
void * zfmemmove(void *dst, const void *src, zfindex size)
wrapper to memmove
Definition ZFCoreTypeDef_ClassType.h:142
#define zfclassNotPOD
shows the class is not a POD type, you should not memset it or declare it in stack or copy value by c...
Definition ZFCoreTypeDef_ClassType.h:48
#define ZF_IN_OUT
dummy macro that shows the param used as required input and output
Definition ZFCoreTypeDef_ClassType.h:196
#define zfrealloc(oldPtr, newSize)
same as realloc defined for future use
Definition ZFCoreTypeDef_ClassType.h:106
void * zfmemcpy(void *dst, const void *src, zfindex size)
wrapper to memcpy
Definition ZFCoreTypeDef_ClassType.h:140
#define zfmalloc(size)
same as malloc defined for future use
Definition ZFCoreTypeDef_ClassType.h:100
#define zfnullT
type for zfnull, can be used for function overload
Definition ZFCoreTypeDef_CoreType.h:85
_ZFT_t_zfbool zfbool
bool type
Definition ZFCoreTypeDef_CoreType.h:103
_ZFT_t_zfindex zfindex
similar to size_t, used for index and size only
Definition ZFCoreTypeDef_CoreType.h:154
#define zftrue
bool true type
Definition ZFCoreTypeDef_CoreType.h:107
_ZFT_t_zfint zfint
same as int, see zfindex
Definition ZFCoreTypeDef_CoreType.h:165
#define zfindexMax()
(zfindex)-1, indicate a max index value, see zfindex
Definition ZFCoreTypeDef_CoreType.h:159
#define zffalse
bool false type
Definition ZFCoreTypeDef_CoreType.h:111
#define zfnull
same as NULL, defined for future use
Definition ZFCoreTypeDef_CoreType.h:88
_ZFT_t_zfuint zfuint
same as unsigned int, see zfindex
Definition ZFCoreTypeDef_CoreType.h:169
#define zfpoolDelete(obj)
see zfpoolNew
Definition ZFMemPool.h:38
#define zfpoolNew(T_Type,...)
internal use only, for allocating internal types for performance
Definition ZFMemPool.h:37
#define ZF_NAMESPACE_GLOBAL_BEGIN
begin namespace ZFFramework
Definition ZFNamespace.h:97
#define ZF_NAMESPACE_GLOBAL_END
end namespace ZFFramework
Definition ZFNamespace.h:98
ZFUIMargin & operator+=(ZFUIMargin &v0, const ZFUIMargin &v1)
v0 += v1
Definition ZFUITypeDef.h:325
ZFUIMargin operator+(const ZFUIMargin &v0, const ZFUIMargin &v1)
v0 + v1
Definition ZFUITypeDef.h:310
low level string container
Definition zfstring.h:89
void * zfunsafe_bufferGiveUp(void)
give up the buffer's ownership and return the buffer, you must free it manually by zfunsafe_bufferFre...
Definition zfstring.h:579
zft_zfstring< T_Char > & assign(const zft_zfstring< T_Char > &s, zfindex len)
replace all content of the string
Definition zfstring.h:338
zfint compare(const zft_zfstring< T_Char > &s) const
compare with another string
Definition zfstring.h:545
zft_zfstring(const zft_zfnullT &dummy)
construct empty string
Definition zfstring.h:157
T_Char get(zfindex pos) const
get char at index
Definition zfstring.h:240
void remove(zfindex pos, zfindex len=((zfindex) -1))
remove part of the string
Definition zfstring.h:498
zft_zfstring< T_Char > & assign(const zft_zfstring< T_Char > &s, zfindex offset, zfindex len)
replace all content of the string
Definition zfstring.h:345
zft_zfstring< zfchar > & assign(const zft_zfstring< zfchar > &s)
Definition zfstring.h:333
const T_Char * cString(void) const
access string value
Definition zfstring.h:468
void swap(zft_zfstring< T_Char > &ref)
swap internal data without deep copy, designed for performance
Definition zfstring.h:256
zft_zfstring< T_Char > & append(const void *s, zfindex len=((zfindex) -1))
append string
Definition zfstring.h:307
zft_zfstring< T_Char > & replace(zfindex replacePos, zfindex replaceLen, const void *s, zfindex len=((zfindex) -1))
replace string in range
Definition zfstring.h:435
zfint compare(const T_Char *s, zfindex len=((zfindex) -1)) const
compare with another string
Definition zfstring.h:549
zft_zfstring< T_Char > & append(const zft_zfstring< T_Char > &s)
append string
Definition zfstring.h:272
void zfunsafe_length(zfindex length)
directly modify the string's length
Definition zfstring.h:605
zft_zfstring(const T_Char *s, zfindex len)
copy content from another string
Definition zfstring.h:137
static void zfunsafe_bufferFree(void *buf)
free buffer returned by zfunsafe_bufferGiveUp
Definition zfstring.h:591
zft_zfstring(const T_Char *s)
copy content from another string
Definition zfstring.h:127
static const zft_zfstring< T_Char > & Empty(void)
global null string ref for impl
Definition zfstring.h:614
T_Char * zfunsafe_buffer(void)
directly access internal writable buffer
Definition zfstring.h:598
zft_zfstring(const zft_zfstring< T_Char > &s)
copy content from another string
Definition zfstring.h:99
zft_zfstring< T_Char > & insert(zfindex insertAt, const zft_zfstring< T_Char > &s)
insert string
Definition zfstring.h:394
void set(zfindex pos, T_Char c)
change char at index
Definition zfstring.h:230
zft_zfstring< T_Char > & append(const zft_zfstring< T_Char > &s, zfindex offset, zfindex len)
append string
Definition zfstring.h:290
zft_zfstring< T_Char > & assign(const zft_zfnullT &dummy)
replace all content of the string
Definition zfstring.h:387
zft_zfstring(const zft_zfstring< T_Char > &s, zfindex pos, zfindex len)
copy content from another string
Definition zfstring.h:114
zfindex length(void) const
Definition zfstring.h:472
zfindex capacity(void) const
get current capacity (including tail '\0')
Definition zfstring.h:488
zft_zfstring< T_Char > & assign(const void *s, zfindex len=((zfindex) -1))
replace all content of the string
Definition zfstring.h:359
zft_zfstring< T_Char > & insert(zfindex insertAt, const void *s, zfindex len=((zfindex) -1))
insert string
Definition zfstring.h:401
void capacity(zfindex capacity)
Definition zfstring.h:482
void capacityTrim(void)
trim to a proper capacity to save memory
Definition zfstring.h:492
zft_zfstring(const T_Char *s, zfindex pos, zfindex len)
copy content from another string
Definition zfstring.h:147
void removeAll(void)
Definition zfstring.h:516
zft_zfstring< T_Char > & append(const zft_zfstring< T_Char > &s, zfindex len)
append string
Definition zfstring.h:279
const void * buffer(void) const
return internal buffer
Definition zfstring.h:571
zft_zfstring(const zft_zfstring< T_Char > &s, zfindex len)
copy content from another string
Definition zfstring.h:106
static zft_zfstring< T_Char > shared(const void *sLiteral, zfindex length=((zfindex) -1))
explicitly create from literal string, you must ensure the literal's life exceeds the returned string
Definition zfstring.h:623
zfbool isEmpty(void) const
true if empty
Definition zfstring.h:476
zft_zfstring(void)
construct an empty string
Definition zfstring.h:92
zft_zfstring< T_Char > & replace(zfindex replacePos, zfindex replaceLen, const zft_zfstring< T_Char > &s)
replace string in range
Definition zfstring.h:427
zft_zfstring< T_Char > & append(T_Char c)
append string
Definition zfstring.h:265