7 #ifndef __platform_string_h__ 8 #define __platform_string_h__ 13 #define FESTRING_DEBUG FALSE 14 #define FESTRING_LOCALSIZE 16 //* unshared for small strings 17 #define FEMEMRCHR(s,c,n) memrchr(s,c,n) 19 inline void *fe_memrchr(
const void *s,
int c,
size_t n)
22 while(r>=s && *r!=c) { r--; }
25 #define FEMEMRCHR(s,c,n) fe_memrchr(s,c,n) 28 #if FE_OS==FE_WIN32 || FE_OS==FE_WIN64 30 #define FESTRCMP_MB(x,y) _mbscmp(x,y) 31 #define FESTRNCMP_MB(x,y,z) _mbsncmp(x,y,z) 32 #define FESTR_MB2UPPER(x) _mbsupr((unsigned char*)x) 33 #define FESTR_MB2LOWER(x) _mbslwr((unsigned char*)x) 34 #define FEVSNPRINTF(x,y,z,a) _vsnprintf(x,y,z,a) 35 #define FESTRCPY(dest,size,src) strcpy_s((char *)dest,size,(char *)src) 37 #define FEVSNPRINTF(x,y,z,a) vsnprintf(x,y,z,a) 38 #define FESTRCPY(dest,size,src) strcpy((char *)dest,(char *)src) 41 #if defined(__linux__) || FE_OS==FE_OSX 43 #define FESTRCMP_MB(x,y) strcmp((char *)x,(char *)y) 44 #define FESTRNCMP_MB(x,y,z) strncmp((char *)x,(char *)y,z) 47 #if defined(FE_OS) && FE_OS==FE_FREEBSD 48 #define FESTRCMP_MB(x,y) strcmp((char *)x,(char *)y) 49 #define FESTRNCMP_MB(x,y,z) strncmp((char *)x,(char *)y,z) 52 #define FESTRLEN(x) strlen((char *)x) 54 #define FESTRING_I8 char // presumably 7-bit ASCII 55 #define FESTRING_U8 unsigned char // presumably multibyte 57 #if FE_SSE>=1 && FE_COMPILER==FE_GNU 59 typedef int v4si __attribute__ ((vector_size (16)));
63 #define FE_STRING2(x) #x 64 #define FE_STRING(x) FE_STRING2(x) 132 class FE_DL_PUBLIC Rep
135 void newBuffer(U32 size);
136 void deleteBuffer(
void);
139 FESTRING_U8* m_pBuffer;
150 String(
const FESTRING_I8* operand);
153 String(
const FESTRING_U8* operand);
160 #if FALSE //* Meyers More Effective C++ #5: No! 164 operator const FESTRING_I8 *(void)
const 169 operator const FESTRING_U8 *(void)
const 178 {
return operator=((
const FESTRING_U8 *)operand); };
181 String& operator=(
const FESTRING_U8* operand);
184 BWORD equals(
const String &operand)
const;
189 I32 compare(
const String &operand)
const;
191 BWORD operator<(
const String &operand)
const 192 {
return compare(operand) < 0; }
197 String pathname(
void)
const;
202 String basename(
void)
const;
214 String prechop(I32 count)
const;
226 String chop(I32 count)
const;
229 BWORD contains(
String find)
const;
251 std::map<String,String>& a_rMap,
252 BWORD a_alsoUseEnv=FALSE,
253 BWORD a_throwMissing=FALSE)
const;
254 String substituteEnv(
void)
256 std::map<String,String> map;
257 return substituteEnv(map,TRUE,FALSE);
269 BWORD a_keepStart=FALSE,BWORD a_keepEnd=FALSE);
278 BWORD dotMatch(
const String &operand)
const;
287 U32 length(
void)
const;
290 BWORD empty(
void)
const;
293 String& sPrintf(
const char* fmt, ...);
298 String& catf(
const char* fmt, ...);
301 String& cat(
const char* operand);
307 String& cat(std::list<String> &strList,
String sep =
"");
313 void resize(U32 size,
char c=
' ');
319 {
return maybeQuote(quote,quote); }
325 {
return maybeUnQuote(quote,quote); }
328 BWORD eachDelimiter=FALSE);
330 I32 lines(
void)
const;
331 String line(I32 a_lineNumber)
const;
334 std::list<String> &a_list);
352 const FESTRING_I8* c_str(
void)
const {
return (FESTRING_I8*)rawU8(); };
355 const FESTRING_U8* rawU8(
void)
const 356 { FEASSERT(!m_pRep || m_pRep->m_pBuffer);
357 return m_pRep? m_pRep->m_pBuffer: m_pLocal; }
361 void output(std::ostream &ostrm)
const;
362 void input(std::istream &istrm);
365 {
return cat(operand); }
367 int integer(
void)
const;
368 Real real(
void)
const;
369 bool match(
const String a_pattern)
const;
370 bool search(
const String a_pattern)
const;
372 const String a_replacement)
const;
373 String convertGlobToRegex(
void)
const;
376 U64 computeFowlerNollVo1(
void)
const;
386 bool operator()(
const String& rString1,
const String& rString2)
388 return (rString1.
compare(rString2)<0);
393 FESTRING_U8* buffer(
void)
394 { FEASSERT(!m_pRep || m_pRep->m_pBuffer);
395 return m_pRep? m_pRep->m_pBuffer: m_pLocal; }
397 void create(
const FESTRING_I8* operand);
401 void forceCase(BWORD upper);
404 void deleteRep(
void);
406 #ifdef FE_SSE_STRING_GNU 410 FESTRING_U8 m_pLocal[FESTRING_LOCALSIZE];
412 #elif FE_CPLUSPLUS >= 201103L 414 FESTRING_U8 m_pLocal[FESTRING_LOCALSIZE];
416 FESTRING_U8 m_pLocal[FESTRING_LOCALSIZE];
427 inline void String::init(
void)
431 memset(m_pLocal,0,FESTRING_LOCALSIZE);
440 inline void String::create(
const FESTRING_I8* operand)
442 m_length=FESTRLEN(operand);
443 U32 bytes=m_length+1;
444 if(bytes<=FESTRING_LOCALSIZE)
447 FESTRCPY(m_pLocal,FESTRING_LOCALSIZE,operand);
448 if(bytes<FESTRING_LOCALSIZE)
450 memset(m_pLocal+bytes,0,FESTRING_LOCALSIZE-bytes);
456 m_pRep->newBuffer(bytes);
457 FEASSERT(m_pRep->m_pBuffer);
458 FESTRCPY(m_pRep->m_pBuffer,bytes,operand);
459 memset(m_pLocal,0,FESTRING_LOCALSIZE);
475 "String::String(FESTRING_I8*) create 0x%p 0x%p \"%s\"\n",
476 this,m_pRep,m_pRep->m_pBuffer);
488 create((
const FESTRING_I8*)operand);
493 "String::String(FESTRING_U8*) create 0x%p 0x%p \"%s\"\n",
494 this,m_pRep,m_pRep->m_pBuffer);
501 #if FE_OS==FE_WIN32 || FE_OS==FE_WIN64 502 sprintf_s((FESTRING_I8*)m_pLocal,16,
"%-15d",operand);
504 sprintf((FESTRING_I8*)m_pLocal,
"%-15d",operand);
506 m_length=FESTRLEN(m_pLocal);
510 inline String::~String(
void)
514 FEASSERT(m_pRep->m_references>0);
516 #ifdef FE_COUNT_ASM_IMPL 517 if(!(feAsmSwapIncr((
int*)(&m_pRep->m_references),-1)-1))
519 if(--m_pRep->m_references == 0)
523 fe_fprintf(stderr,
"~String destroy 0x%p 0x%p \"%s\"\n",
524 this,m_pRep,m_pRep->m_pBuffer);
535 FEASSERT(m_pRep->m_references>0);
537 #ifdef FE_COUNT_ASM_IMPL 538 if(!(feAsmSwapIncr((
int*)(&m_pRep->m_references),-1)-1))
540 if(--m_pRep->m_references == 0)
544 fe_fprintf(stderr,
"String::operator=(FESTRING_U8*)" 545 " destroy 0x%p 0x%p \"%s\"\n",
546 this,m_pRep,m_pRep->m_pBuffer);
573 create((
const FESTRING_I8*)operand);
577 fe_fprintf(stderr,
"String::operator=(FESTRING_U8*) create 0x%p 0x%p %s\n",
578 this,m_pRep,m_pRep->m_pBuffer);
589 FEASSERT(operand.m_pRep->m_references>=0);
591 #ifdef FE_COUNT_ASM_IMPL 592 feAsmIncr((
int*)(&operand.m_pRep->m_references),1);
594 operand.m_pRep->m_references++;
600 FEASSERT(m_pRep->m_references>0);
602 #ifdef FE_COUNT_ASM_IMPL 603 if(!(feAsmSwapIncr((
int*)(&m_pRep->m_references),-1)-1))
605 if(--m_pRep->m_references == 0)
610 "String::operator=(String) destroy 0x%p 0x%p \"%s\"\n",
611 this,m_pRep,m_pRep->m_pBuffer);
617 m_length=operand.m_length;
619 if(m_pLocal==operand.m_pLocal)
622 fe_fprintf(stderr,
"String::operator=(FESTRING_U8*)" 623 " string=string (local) \"%s\"\n",m_pLocal);
628 FEASSERT(m_pLocal>operand.m_pLocal+FESTRING_LOCALSIZE ||
629 operand.m_pLocal>m_pLocal+FESTRING_LOCALSIZE);
633 #if FE_CPLUSPLUS >= 201103L 634 U64* p1=
reinterpret_cast<U64*
>(m_pLocal);
635 const U64* p2=
reinterpret_cast<const U64*
>(operand.m_pLocal);
639 U32* p1=
reinterpret_cast<U32*
>(m_pLocal);
640 const U32* p2=
reinterpret_cast<const U32*
>(operand.m_pLocal);
647 m_pRep=operand.m_pRep;
653 "String::operator=(FESTRING_U8*)" 654 " copy 0x%p 0x%p \"%s\"\n",
655 this,m_pRep,m_pRep->m_pBuffer);
669 return !m_pLocal[0] && !m_pRep;
672 #if defined(__GNUC__) 673 #pragma GCC diagnostic push 674 #pragma GCC diagnostic ignored "-Wstrict-aliasing" 677 #if defined(__GNUC__) && ( __GNUC__==4 && __GNUC_MINOR__>=2 && __GNUC_MINOR__<5) 678 #pragma GCC diagnostic ignored "-Wstrict-aliasing" 682 if(m_length!=operand.m_length)
687 #if FESTRING_LOCALSIZE==16 688 if(!m_pRep && !operand.m_pRep)
690 #ifdef FE_SSE_STRING_GNU 692 v4si comp=__builtin_ia32_pcmpeqd128(
694 *(v4si*)operand.m_pLocal);
696 U32* pComp4=
reinterpret_cast<U32*
>(&comp);
699 fe_printf(
"pComp4 0x%x 0x%x 0x%x 0x%x\n",
700 pComp4[0],pComp4[1],pComp4[2],pComp4[3]);
702 long unsigned int* pComp2=
reinterpret_cast<long unsigned int*
>(&comp);
703 fe_printf(
"\n\"%s\" \"%s\" %d\n",m_pLocal,operand.m_pLocal,
704 !~(pComp2[0]&pComp2[1]));
712 return (!~(pComp4[0]&pComp4[1]&pComp4[2]&pComp4[3]));
713 #elif FE_CPLUSPLUS >= 201103L 714 const U64* p1=
reinterpret_cast<const U64*
>(m_pLocal);
715 const U64* p2=
reinterpret_cast<const U64*
>(operand.m_pLocal);
716 return (p1[0]==p2[0] && p1[1]==p2[1]);
719 const U32* p1=
reinterpret_cast<const U32*
>(m_pLocal);
720 const U32* p2=
reinterpret_cast<const U32*
>(operand.m_pLocal);
721 return (p1[0]==p2[0] && p1[1]==p2[1] &&
722 p1[2]==p2[2] && p1[3]==p2[3]);
727 return !FESTRCMP_MB(rawU8(),operand.
rawU8());
730 #if defined(__GNUC__) 731 #pragma GCC diagnostic pop 736 return FESTRCMP_MB(rawU8(),operand.
rawU8());
741 return cat(operand.
c_str());
753 return ((
const String)s1).equals(s2);
771 return !((
const String)s1).equals(s2);
783 return result.
cat(s2);
798 inline String operator+(
const String& s1,
const float& f)
804 inline String operator+(
const float& f,
const String& s1)
822 inline String print(
char* a_buffer)
827 inline String print(U32 a_integer)
834 inline String print(I32 a_integer)
842 inline String print(
int a_integer)
850 inline String print(F32 a_float)
857 inline String print(F64 a_double)
880 #define c_print(x) fe::print(x).c_str() const FESTRING_I8 * c_str(void) const
Return the contents of the 8-bit buffer cast as signed bytes.
Definition: String.h:352
void forceLowercase(void)
Force all applicable characters to lower case.
Definition: String.h:283
Sort an Array of Strings.
Definition: String.h:384
String & operator=(const String &operand)
Compare to another String.
Definition: String.h:584
kernel
Definition: namespace.dox:3
void vsPrintf(std::string &target, const char *fmt, va_list ap, int &size)
Format text into a fe::String.
Definition: debug.cc:252
BWORD operator!=(const DualString &s1, const DualString &s2)
Compare two DualString's (reverse logic)
Definition: DualString.h:229
I32 compare(const String &operand) const
Standard string compare: returns -1, 0, or 1 if the string is alphabetically less than...
Definition: String.h:734
BWORD operator==(const DualString &s1, const DualString &s2)
Compare two DualString's.
Definition: DualString.h:208
void forceUppercase(void)
Force all applicable characters to upper case.
Definition: String.h:281
const FESTRING_U8 * rawU8(void) const
Return the contents of the 8-bit buffer cast as unsigned bytes.
Definition: String.h:355
String & cat(const char *operand)
Append the current String with the given text.
Definition: String.cc:545
BWORD empty(void) const
Returns true if the contents have zero length.
Definition: String.h:667
String & sPrintf(const char *fmt,...)
Populate the string in the manner of sprintf().
Definition: String.cc:529
Automatically reference-counted string container.
Definition: String.h:128
BWORD equals(const String &operand) const
checks if the string is equivalent
Definition: String.h:680
String & operator=(const FESTRING_I8 *operand)
Compare to a signed byte buffer (using 8-bit).
Definition: String.h:177
String(void)
Construct an empty string.
Definition: String.h:422
U32 length(void) const
Return the number of represented characters, but not necessarily the size of any buffer.
Definition: String.h:662