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);
374 zfmemcpy(d->d.buf, s, len *
sizeof(T_Char));
375 d->d.buf[len] =
'\0';
394 return this->
insert(insertAt, s.cString(), s.length());
399 ,
ZF_IN const void *s
402 if(insertAt >= this->
length()) {
407 len = _ZFP_zfstring_len((
const T_Char *)s);
411 _prepareWrite(lenTmp + len);
412 d->length = (
zfuint)(lenTmp + len);
413 zfmemmove(d->d.buf + insertAt + len, d->d.buf + insertAt, (lenTmp - insertAt) *
sizeof(T_Char));
414 zfmemcpy(d->d.buf + insertAt, s, len *
sizeof(T_Char));
415 d->d.buf[d->length] =
'\0';
428 return this->
replace(replacePos, replaceLen, s.cString(), s.length());
434 ,
ZF_IN const void *s
437 if(replacePos >= this->
length()) {
442 if(replaceLen > lenTmp - replacePos) {
443 replaceLen = lenTmp - replacePos;
446 len = _ZFP_zfstring_len((
const T_Char *)s);
448 if(len > replaceLen) {
449 _prepareWrite(lenTmp + len - replaceLen);
452 _prepareWrite(lenTmp);
454 zfmemmove(d->d.buf + replacePos + len, d->d.buf + replacePos + replaceLen, (lenTmp - replacePos - replaceLen) *
sizeof(T_Char));
455 zfmemcpy(d->d.buf + replacePos, s, len *
sizeof(T_Char));
456 d->length = (
zfuint)(lenTmp + len - replaceLen);
457 d->d.buf[d->length] =
'\0';
473 return d->length == 0;
489 if(d->capacity >= 128 && d->length <= 32) {
500 if(len > lenTmp - pos) {
504 _prepareWrite(lenTmp);
505 zfmemmove(d->d.buf + pos, d->d.buf + pos + len, (lenTmp - pos - len) *
sizeof(T_Char));
507 d->d.buf[d->length] =
'\0';
515 if(d->refCount == 1) {
516 if(d->capacity > 0) {
536 return d == s.d ? 0 : _ZFP_zfstring_cmp(this->
cString(), s.cString());
540 ZF_IN const T_Char *s
544 len = s ? _ZFP_zfstring_len(s) : 0;
546 if(this->
length() < len) {
549 else if(this->
length() > len) {
553 return _ZFP_zfstring_ncmp(this->
cString(), s, len);
571 _prepareWrite(this->
length());
572 T_Char *ret = d->d.buf;
589 _prepareWrite(this->
length());
596 _prepareWrite(this->length());
601 _ZFP_zfstringD<T_Char> *d;
614 ZF_IN const void *sLiteral
617 _ZFP_zfstringD<T_Char> *d =
zfpoolNew(_ZFP_zfstringD<T_Char>);
618 d->d.ptr = (
const T_Char *)sLiteral;
628 static _ZFP_zfstringD<T_Char> *_ZFP_Empty(
void) {
629 static _ZFP_zfstringH<T_Char> d;
634 capacity = ((capacity / 16) + 1) * 16;
636 else if(capacity < 64) {
637 capacity = ((capacity / 32) + 1) * 32;
639 else if(capacity < 1024) {
640 capacity = ((capacity / 128) + 1) * 128;
643 capacity = ((capacity / 1024) + 1) * 1024;
648 _capacityOptimize(capacity);
650 if(d->refCount == 1) {
651 if(d->capacity > 0) {
652 d->d.buf = (T_Char *)
zfrealloc(d->d.buf, capacity *
sizeof(T_Char));
655 const T_Char *ptr = d->d.ptr;
656 d->d.buf = (T_Char *)
zfmalloc(capacity *
sizeof(T_Char));
657 if(ptr && keepContents) {
658 zfmemcpy(d->d.buf, ptr, capacity *
sizeof(T_Char));
666 _ZFP_zfstringD<T_Char> *dTmp = d;
668 d->length = dTmp->length;
669 d->d.buf = (T_Char *)
zfmalloc(capacity *
sizeof(T_Char));
670 if(dTmp->length > 0 && keepContents) {
671 zfmemcpy(d->d.buf, dTmp->d.ptr, dTmp->length *
sizeof(T_Char));
672 d->d.buf[dTmp->length] =
'\0';
678 d->capacity = (
zfuint)capacity;
684 if(capacity >= d->capacity || d->refCount > 1) {
685 _capacityChange(capacity,
zftrue);
694 #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:569
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:535
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:494
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:464
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:431
zfint compare(const T_Char *s, zfindex len=((zfindex) -1)) const
compare with another string
Definition zfstring.h:539
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:595
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:581
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:604
T_Char * zfunsafe_buffer(void)
directly access internal writable buffer
Definition zfstring.h:588
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:390
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:383
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:468
zfindex capacity(void) const
get current capacity (including tail '\0')
Definition zfstring.h:484
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:397
void capacity(zfindex capacity)
ensure the string's capacity (including tail '\0'), note the result capacity is not ensured same as r...
Definition zfstring.h:478
void capacityTrim(void)
trim to a proper capacity to save memory
Definition zfstring.h:488
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:512
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:561
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:613
zfbool isEmpty(void) const
true if empty
Definition zfstring.h:472
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:423
zft_zfstring< T_Char > & append(T_Char c)
append string
Definition zfstring.h:265