/* * Copyright (c) 2019 Jief Luce. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * */ #if !defined(__XSTRINGABSTRACT_H__) #define __XSTRINGABSTRACT_H__ #include #include "XToolsCommon.h" #include "unicode_conversions.h" #ifndef DEBUG_ALL #define DEBUG_XStringAbstract 0 #else #define DEBUG_XStringAbstract DEBUG_ALL #endif #if DEBUG_XStringAbstract == 0 #define DBG_XSTRING(...) #else #define DBG_XSTRING(...) DebugLog(DEBUG_XStringAbstract, __VA_ARGS__) #endif //#if __WCHAR_MAX__ <= 0xFFFFu // #define wchar_cast char16_t //#else // #define wchar_cast char32_t //#endif #define asciiToLower(ch) (((ch >= L'A') && (ch <= L'Z')) ? ((ch - L'A') + L'a') : ch) #define asciiToUpper(ch) (((ch >= L'a') && (ch <= L'z')) ? ((ch - L'a') + L'A') : ch) template int XStringAbstract__startWith(const S* src, const O* other, bool ignoreCase) { size_t nb = 0; const S* src2 = src; const O* other2 = other; char32_t src_char32; char32_t other_char32; other2 = get_char32_from_string(other2, &other_char32); if ( !other_char32 ) return true; // startWith with empty string is considered true src2 = get_char32_from_string(src2, &src_char32); while ( other_char32 ) { if ( ignoreCase ) { src_char32 = asciiToLower(src_char32); other_char32 = asciiToLower(other_char32); } if ( src_char32 != other_char32 ) return false; src2 = get_char32_from_string(src2, &src_char32); other2 = get_char32_from_string(other2, &other_char32); nb += 1; }; return src_char32 != 0; } template int XStringAbstract__compare(const S* src, const O* other, bool ignoreCase) { // size_t len_s = length_of_utf_string(src); // size_t len_other = length_of_utf_string(other); size_t nb = 0; const S* src2 = src; const O* other2 = other; char32_t src_char32; char32_t other_char32; src2 = get_char32_from_string(src2, &src_char32); other2 = get_char32_from_string(other2, &other_char32); while ( src_char32 ) { if ( ignoreCase ) { src_char32 = asciiToLower(src_char32); other_char32 = asciiToLower(other_char32); } if ( src_char32 != other_char32 ) break; src2 = get_char32_from_string(src2, &src_char32); other2 = get_char32_from_string(other2, &other_char32); nb += 1; }; if ( src_char32 == other_char32 ) return 0; return src_char32 > other_char32 ? 1 : -1; } template int XStringAbstract__ncompare(const S* src, const O* other, size_t n, bool ignoreCase) { if ( n == 0 ) return 0; // string of 0 length are equal. const S* src2 = src; const O* other2 = other; char32_t src_char32; char32_t other_char32; src2 = get_char32_from_string(src2, &src_char32); other2 = get_char32_from_string(other2, &other_char32); size_t nb = 1; while ( src_char32 && nb < n ) { if ( ignoreCase ) { src_char32 = asciiToLower(src_char32); other_char32 = asciiToLower(other_char32); } if ( src_char32 != other_char32 ) break; src2 = get_char32_from_string(src2, &src_char32); other2 = get_char32_from_string(other2, &other_char32); nb += 1; }; if ( src_char32 == other_char32 ) return 0; return src_char32 > other_char32 ? 1 : -1; } template size_t XStringAbstract__indexOf(const O** s, const P* other, size_t offsetRet, bool toLower) { size_t Idx = 0; char32_t s_char32; char32_t other_char32; do { const O* s2 = *s; const P* other2 = other; do { s2 = get_char32_from_string(s2, &s_char32); other2 = get_char32_from_string(other2, &other_char32); if ( toLower ) { s_char32 = asciiToLower(s_char32); other_char32 = asciiToLower(other_char32); } } while ( s_char32 && other_char32 && s_char32 == other_char32 ); if ( other_char32 == 0 ) return Idx+offsetRet; *s = get_char32_from_string(*s, &s_char32); Idx++; } while (s_char32); return MAX_XSIZE; } template size_t XStringAbstract__indexOf(const O* s, size_t Pos, const P* other, bool toLower) { if ( *other == 0 ) return Pos; char32_t char32 = 1; for ( size_t Idx=0 ; Idx size_t XStringAbstract__rindexOf(const O* s, size_t Pos, const P* other, bool toLower) { if ( *other == 0 ) return Pos > length_of_utf_string(s) ? length_of_utf_string(s) : Pos; size_t index = XStringAbstract__indexOf(&s, other, 0, toLower); size_t prev_index = index; // initialize to index in case of index is already == Pos char32_t char32; s = get_char32_from_string(s, &char32); while ( char32 && index < Pos ) { prev_index = index; index = XStringAbstract__indexOf(&s, other, index+1, toLower); s = get_char32_from_string(s, &char32); }; if ( index == Pos ) return index; if ( prev_index <= Pos ) return prev_index; return MAX_XSIZE; } template class __String { public: typedef T char_t; typedef ThisXStringClass xs_t; protected: T *m_data; // convenience method. Did it this way to avoid #define in header. They can have an impact on other headers size_t Xmin(size_t x1, size_t x2) const { if ( x1 < x2 ) return x1; return x2; } size_t Xmax(size_t x1, size_t x2) const { if ( x1 > x2 ) return x1; return x2; } // Methods _data is protected intentionally. They are const method returning non-const pointer. That's intentional, but dangerous. Do not expose to public. // If you need a non-const pointer for low-level access, to use dataSized and have to specify the size // pos is counted in logical char template T* _data(IntegralType pos) const { if ( pos<0 ) panic("T* data(int i) -> i < 0"); size_t offset = size_of_utf_string_len(m_data, (unsigned_type(IntegralType))pos); // If pos is too big, size_of_utf_string_len returns the end of the string return m_data + offset; } public: // T* memoryOffset(size_t i) { // // } public: constexpr __String(const T* s) : m_data((T*)s) {} public: // constexpr __String() : m_data(&nullChar) { } constexpr __String(const __String&) = delete; constexpr __String() = delete; // no assignement, no destructor template T* data(IntegralType pos) const { return _data(pos); } size_t length() const { return length_of_utf_string(m_data); } // size_t sizeZZ() const { return size_of_utf_string(m_data); } size_t sizeInNativeChars() const { return size_of_utf_string(m_data); } size_t sizeInBytes() const { return size_of_utf_string(m_data)*sizeof(T); } size_t sizeInBytesIncludingTerminator() const { return (size_of_utf_string(m_data)+1)*sizeof(T); } // usefull for unit tests const T* wc_str() const { return m_data; } const T* c_str() const { return m_data; } const T* s() const { return m_data; } const T* data() const { return m_data; } // todo delete /* Empty ? */ bool isEmpty() const { return m_data == nullptr || *m_data == 0; } bool notEmpty() const { return !isEmpty(); } //--------------------------------------------------------------------- cast // int ToInt() const; // size_t ToUInt() const; //--------------------------------------------------------------------- charAt, [] template char32_t char32At(IntegralType i) const { if (i < 0) { panic("__String::char32At(size_t i) : i < 0. System halted\n"); } size_t nb = 0; const T *p = m_data; char32_t char32; do { p = get_char32_from_string(p, &char32); if (!char32) { if ( (unsigned_type(IntegralType))i == nb ) return 0; // no panic if we want to access the null terminator panic("__String::char32At(size_t i) : i >= length(). System halted\n"); } nb += 1; } while (nb <= (unsigned_type(IntegralType))i); return char32; } template char16_t char16At(IntegralType i) const { char32_t char32 = char32At(i); if ( char32 >= 0x10000 ) return 0xFFFD; // � REPLACEMENT CHARACTER used to replace an unknown, unrecognized or unrepresentable character return (char16_t)char32; } /* [] */ template char32_t operator [](IntegralType i) const { return char32At(i); } char32_t lastChar() const { if ( length() > 0 ) return char32At(length()-1); else return 0; } // /* copy ctor */ // __String(const __String &S) { Init(0); takeValueFrom(S); } // /* ctor */ // template // explicit __String(const __String& S) { Init(0); takeValueFrom(S); } //// template //// explicit __String(const O* S) { Init(0); takeValueFrom(S); } /* Copy Assign */ // Only other XString, no litteral at the moment. // __String& operator =(const __String& S) { strcpy(S.s()); return *this; } // /* Assign */ // template // ThisXStringClass& operator =(const __String& S) { strcpy(S.s()); return *((ThisXStringClass*)this); } //// template //// ThisXStringClass& operator =(const O* S) { strcpy(S); return *this; } //--------------------------------------------------------------------- indexOf, rindexOf, contains, subString /* indexOf */ size_t indexOf(char32_t char32Searched, size_t Pos = 0) const { char32_t buf[2] = { char32Searched, 0}; return XStringAbstract__indexOf(m_data, Pos, buf, false); } template size_t indexOf(const O* S, size_t Pos = 0) const { return XStringAbstract__indexOf(m_data, Pos, S, false); } template size_t indexOf(const __String& S, size_t Pos = 0) const { return indexOf(S.s(), Pos); } /* IC */ size_t indexOfIC(char32_t char32Searched, size_t Pos = 0) const { char32_t buf[2] = { char32Searched, 0}; return XStringAbstract__indexOf(m_data, Pos, buf, true); } template size_t indexOfIC(const O* S, size_t Pos = 0) const { return XStringAbstract__indexOf(m_data, Pos, S, true); } template size_t indexOfIC(const __String& S, size_t Pos = 0) const { return indexOfIC(S.s(), Pos); } /* rindexOf */ size_t rindexOf(const char32_t char32Searched, size_t Pos = MAX_XSIZE-1) const { char32_t buf[2] = { char32Searched, 0}; return XStringAbstract__rindexOf(m_data, Pos, buf, false); } template size_t rindexOf(const O* S, size_t Pos = MAX_XSIZE-1) const { return XStringAbstract__rindexOf(m_data, Pos, S, false); } template size_t rindexOf(const __String& S, size_t Pos = MAX_XSIZE-1) const { return rindexOf(S.s(), Pos); } /* IC */ size_t rindexOfIC(const char32_t char32Searched, size_t Pos = MAX_XSIZE-1) const { char32_t buf[2] = { char32Searched, 0}; return XStringAbstract__rindexOf(m_data, Pos, buf, true); } template size_t rindexOfIC(const O* S, size_t Pos = MAX_XSIZE-1) const { return XStringAbstract__rindexOf(m_data, Pos, S, true); } template size_t rindexOfIC(const __String& S, size_t Pos = MAX_XSIZE-1) const { return rindexOf(S.s(), Pos); } template bool contains(const __String& S) const { return indexOf(S) != MAX_XSIZE; } template bool contains(const O* S) const { return indexOf(S) != MAX_XSIZE; } template size_t containsIC(const __String& S) const { return indexOfIC(S) != MAX_XSIZE; } template size_t containsIC(const O* S) const { return indexOfIC(S) != MAX_XSIZE; } ThisXStringClass subString(size_t pos, size_t count) const { // if ( pos > length() ) return ThisXStringClass(); // if ( count > length()-pos ) count = length()-pos; ThisXStringClass ret; const T* src = m_data; char32_t char32 = 1; while ( char32 && pos > 0 ) { src = get_char32_from_string(src, &char32); pos -= 1; }; ret.strncat(src, count); return ret; } template bool startWith(const __String& otherS) const { return XStringAbstract__startWith(m_data, otherS.m_data, false); } template bool startWith(const O* other) const { return XStringAbstract__startWith(m_data, other, false); } template bool startWithIC(const __String& otherS) const { return XStringAbstract__startWith(m_data, otherS.m_data, true); } template bool startWithIC(const O* other) const { return XStringAbstract__startWith(m_data, other, true); } //--------------------------------------------------------------------- String modification void lowerAscii() { T* s = m_data; while ( *s ) { *s = asciiToLower(*s); s++; } } void upperAscii() { T* s = m_data; while ( *s ) { *s = asciiToUpper(*s); s++; } } void trim() { T* start = 0; size_t count = 0; T* s = m_data; while ( *s && unsigned_type(T)(*s) <= 32 ) s++; start = s; while ( *s && unsigned_type(T)(*s) > 32 ) s++; count = uintptr_t(s - start); memmove(m_data, start, count*sizeof(T)); m_data[count] = 0; } //--------------------------------------------------------------------- ThisXStringClass basename() const { size_t lastSepPos = MAX_XSIZE; size_t pos = 0; const T *p = m_data; char32_t char32; p = get_char32_from_string(p, &char32); while ( char32 ) { if ( char32 == U'/' || char32 == U'\\' ) lastSepPos = pos; pos += 1; p = get_char32_from_string(p, &char32); }; if ( lastSepPos == MAX_XSIZE ) { if ( p == m_data ) return ThisXStringClass().takeValueFrom("."); } return subString(lastSepPos+1, MAX_XSIZE); } // ThisXStringClass dirname() const // { // size_t idx = rindexOf('/'); // if ( idx == MAX_XSIZE ) return ThisXStringClass(); // return subString(0, idx); // } // void deleteCountCharsAt(size_t pos, size_t count=1); //{ // if ( pos < size() ) { // if ( count != MAX_XSIZE && pos + count < size() ) { // memmove( _data(pos), data(pos+count), (size()-pos-count)*sizeof(T)); // memmove handles overlapping memory move // setLength(size()-count);/* data()[length()-count]=0 done in setLength */ // }else{ // setSize(pos);/* data()[pos]=0 done in setLength */ // } // } //} // void insert(const __String& Str, size_t pos); //{ // if ( pos < size() ) { // CheckSize(size()+Str.size()); // memmove(_data(pos + Str.size()), data(pos), (size()-pos)*sizeof(T)); // memmove(_data(pos), Str.data(), Str.size()*sizeof(T)); // setLength(size()+Str.size()); // }else{ // StrCat(Str); // } //} // void ToLower(bool FirstCharIsCap = false); // bool IsLetters() const; // bool IsLettersNoAccent() const; // bool IsDigits() const; //{ // const T *p; // // p = data(); // if ( !*p ) return false; // for ( ; *p ; p+=1 ) { // if ( *p < '0' ) return false; // if ( *p > '9' ) return false; // } // return true; //} // bool IsDigits(size_t pos, size_t count) const; //{ // const T *p; // const T *q; // // if ( pos >= size() ) { // return false; // } // if ( pos+count > size() ) { // return false; // } // p = data() + pos; // q = p + count; // for ( ; p < q ; p+=1 ) { // if ( *p < '0' ) return false; // if ( *p > '9' ) return false; // } // return true; //} // void Replace(T c1, T c2) // { // T* p; // // p = s(); // while ( *p ) { // if ( *p == c1 ) *p = c2; // p += 1; // } // } // __String SubStringReplace(T c1, T c2); //{ // T* p; // __String Result; // // p = s(); // while ( *p ) { // if ( *p == c1 ) Result += c2; // else Result += *p; // p++; // } // return Result; //} //--------------------------------------------------------------------- template int strcmp(const O* S) const { return XStringAbstract__compare(m_data, S, false); } // int Compare(const char* S) const { return ::Compare(m_data, S); } // int Compare(const char16_t* S) const { return ::Compare(m_data, S); }; // int Compare(const char32_t* S) const { return ::Compare(m_data, S); }; // int Compare(const wchar_t* S) const { return ::Compare(m_data, S); }; // template bool equal(const __String& S) const { return XStringAbstract__compare(m_data, S.s(), false) == 0; } template bool equal(const O* S) const { return XStringAbstract__compare(m_data, S, false) == 0; } template bool equalIC(const __String& S) const { return XStringAbstract__compare(m_data, S.s(), true) == 0; } template bool equalIC(const O* S) const { return XStringAbstract__compare(m_data, S, true) == 0; } // bool SubStringEqual(size_t Pos, const T* S) const { return (memcmp(data(Pos), S, wcslen(S)) == 0); } public: // == operator template bool operator == (const __String& s2) const { return (*this).strcmp(s2.s()) == 0; } // template // bool operator == (const O* s2) const { return (*this).strcmp(s2) == 0; } // template // friend bool operator == (const O* s1, ThisXStringClass& s2) { return s2.strcmp(s1) == 0; } template bool operator != (const __String& s2) const { return !(*this == s2); } // template // bool operator != (const O* s2) const { return !(*this == s2); } // template // friend bool operator != (const O* s1, const ThisXStringClass& s2) { return s2.strcmp(s1) != 0; } template bool operator < (const __String& s2) const { return (*this).strcmp(s2.s()) < 0; } // template // bool operator < (const O* s2) const { return (*this).strcmp(s2) < 0; } // template // friend bool operator < (const O* s1, const ThisXStringClass& s2) { return s2.strcmp(s1) > 0; } template bool operator > (const __String& s2) const { return (*this).strcmp(s2.s()) > 0; } // template // bool operator > (const O* s2) const { return (*this).strcmp(s2) > 0; } // template // friend bool operator > (const O* s1, const ThisXStringClass& s2) { return s2.strcmp(s1) < 0; } template bool operator <= (const __String& s2) const { return (*this).strcmp(s2.s()) <= 0; } // template // bool operator <= (const O* s2) const { return (*this).strcmp(s2) <= 0; } // template // friend bool operator <= (const O* s1, const ThisXStringClass& s2) { return s2.strcmp(s1) >= 0; } template bool operator >= (const __String& s2) const { return (*this).strcmp(s2.s()) >= 0; } // template // bool operator >= (const O* s2) const { return (*this).strcmp(s2) >= 0; } // template // friend bool operator >= (const O* s1, const ThisXStringClass& s2) { return s2.strcmp(s1) <= 0; } }; //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx template class LString : public __String { public: protected: constexpr LString(const T* s) : __String(s) {}; constexpr LString() = delete; constexpr LString(const LString& L) : __String(L.m_data) {}; // no assignement, no destructor }; //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx #define m_data __String::m_data template class XStringAbstract : public __String { static T nullChar; protected: size_t m_allocatedSize; //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx // Init , Alloc //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx // // void Init(size_t aSize=0) // { // //DBG_XSTRING("Init aSize=%d\n", aSize); // // We don't allocate any memory at first. To not have to test all the time if m_data is null, we init it to an empty string // m_data = &nullChar; // m_allocatedSize = 0; // // if aSize == 0, nothing is done, because m_allocatedSize == aSize // CheckSize(aSize, 0); // } /* * nNewSize doesn't include null terminator. Alloc(0) will allocate 1 char. * NOTE : m_allocatedSize WILL include the the null terminator. Alloc(0) makes m_allocatedSize == 1 */ void Alloc(size_t nNewSize) { if ( m_allocatedSize == 0 ) m_data = (T*)malloc( (nNewSize+1)*sizeof(T) ); else m_data = (T*)Xrealloc(m_data, (nNewSize+1)*sizeof(T), (m_allocatedSize+1)*sizeof(T)); if ( !m_data ) { panic("XStringAbstract::Alloc(%zu) : Xrealloc(%" PRIuPTR ", %lu, %zd) returned NULL. System halted\n", nNewSize, uintptr_t(m_data), (nNewSize+1)*sizeof(T), (m_allocatedSize+1)*sizeof(T)); } m_allocatedSize = nNewSize; } // public: T *CheckSize(size_t nNewSize, size_t nGrowBy = XStringGrowByDefault) // nNewSize is in number of chars, NOT bytes { //DBG_XSTRING("CheckSize: m_size=%d, nNewSize=%d\n", m_size, nNewSize); if ( m_allocatedSize < nNewSize ) { nNewSize += nGrowBy; if ( m_allocatedSize == 0 ) { //if ( *m_data ) { size_t len = __String::length(); if ( nNewSize < len ) nNewSize = len; T* m_dataSav = m_data; m_data = NULL; Alloc(nNewSize); utf_string_from_utf_string(m_data, m_allocatedSize, m_dataSav); }else{ Alloc(nNewSize); } // m_data[m_allocatedSize] = 0; // we allocated one more char (nNewSize+1). This \0 is an extra precaution. It's not for the normal null terminator. All string operation must considered that only m_allocatedSize bytes were allocated. } return m_data; } // void setSize(size_t newSize) // nNewSize is in number of chars, NOT bytes // { // //DBG_XSTRING("setLength(%d)\n", len); // CheckSize(newSize); // // if ( len >= size() ) { // // DBG_XSTRING("__String::setLength(size_t len) : len >= size() (%d != %d). System halted\n", len, size()); // // panic(); // // } // m_data[newSize] = 0; // we may rewrite a 0 in nullChar, if no memory were allocated. That's ok. // } public: /* default ctor */ XStringAbstract() : __String(&nullChar), m_allocatedSize(0) {} /* copy ctor */ XStringAbstract(const XStringAbstract& S) : __String(&nullChar), m_allocatedSize(0) { if ( S.m_data && !S.m_allocatedSize ) { m_data = S.m_data; }else{ takeValueFrom(S); } } ~XStringAbstract() { //DBG_XSTRING("Destructor :%ls\n", data()); if ( m_allocatedSize > 0 ) free((void*)m_data); } /* ctor */ template explicit XStringAbstract(const LString& S) : __String(S.s()), m_allocatedSize(0) {} template explicit XStringAbstract(const XStringAbstract& S) : __String(&nullChar), m_allocatedSize(0) { takeValueFrom(S); } template explicit XStringAbstract(const LString& S) : __String(&nullChar), m_allocatedSize(0) { takeValueFrom(S); } // TEMPORARILY DISABLED // template // explicit __String(const O* S) { Init(0); takeValueFrom(S); } // /* Copy Assign */ // Only other XString, no litteral at the moment. XStringAbstract& operator=(const XStringAbstract &S) { takeValueFrom(S); return *this; } /* Assign */ template ThisXStringClass& operator =(const __String& S) { strcpy(S.s()); return *((ThisXStringClass*)this); } // TEMPORARILY DISABLED // template // ThisXStringClass& operator =(const O* S) { strcpy(S); return *this; } protected: ThisXStringClass& takeValueFromLiteral (const T* s) { if ( m_allocatedSize > 0 ) panic("XStringAbstract::takeValueFromLiteral -> m_allocatedSize > 0"); m_data = (T*)s; return *((ThisXStringClass*)this); } public: size_t allocatedSize() const { return m_allocatedSize; } void setEmpty() { if ( m_allocatedSize <= 0 ) m_data = &nullChar; else m_data[0] = 0; } template T* dataSized(IntegralType size) { if ( size<0 ) panic("T* dataSized() -> i < 0"); if ( (unsigned_type(IntegralType))size > MAX_XSIZE ) panic("T* dataSized() -> i > MAX_XSIZE"); CheckSize((size_t)size); return __String::_data(0); } // // // Pos is counted in logical char but size is counted in physical char (char, char16_t, char32_t or wchar_t) // template // T* dataSized(IntegralType1 pos, IntegralType2 size) // { // if ( pos<0 ) panic("T* dataSized(xisize i, size_t sizeMin, size_t nGrowBy) -> i < 0"); // if ( size<0 ) panic("T* dataSized(xisize i, size_t sizeMin, size_t nGrowBy) -> i < 0"); // size_t offset = size_of_utf_string_len(m_data, (typename _xtools__make_unsigned::type)pos); // If pos is too big, size_of_utf_string_len returns the end of the string // CheckSize(offset + (typename _xtools__make_unsigned::type)size); // return _data(pos); // } T* forgetDataWithoutFreeing() { T* ret = m_data; m_data = &nullChar; m_allocatedSize = 0; return ret; } //--------------------------------------------------------------------- strcat, strcpy, operator = /* strcpy char */ template void strcpy(O otherChar) { if ( otherChar != 0) { size_t newSize = utf_size_of_utf_string_len(m_data, &otherChar, 1); CheckSize(newSize+1, 0); utf_string_from_utf_string_len(m_data, m_allocatedSize, &otherChar, 1); m_data[newSize] = 0; }else{ setEmpty(); } } /* strcpy */ template void strcpy(const O* other) { if ( other && *other ) { size_t newSize = utf_size_of_utf_string(m_data, other); CheckSize(newSize+1, 0); utf_string_from_utf_string(m_data, m_allocatedSize, other); m_data[newSize] = 0; }else{ setEmpty(); } } /* strncpy */ template void strncpy(const O* other, size_t other_len) { if ( other && *other && other_len > 0 ) { size_t newSize = utf_size_of_utf_string_len(m_data, other, other_len); CheckSize(newSize+1, 0); utf_string_from_utf_string_len(m_data, m_allocatedSize, other, other_len); m_data[newSize] = 0; }else{ setEmpty(); } } /* strcat char */ template void strcat(O otherChar) { if ( otherChar ) { size_t currentSize = size_of_utf_string(m_data); size_t newSize = currentSize + utf_size_of_utf_string_len(m_data, &otherChar, 1); CheckSize(newSize+1, 0); utf_string_from_utf_string_len(m_data+currentSize, m_allocatedSize, &otherChar, 1); m_data[newSize] = 0; }else{ // nothing to do } } /* strcat char* */ template void strcat(const O* other) { if ( other && *other ) { size_t currentSize = size_of_utf_string(m_data); // size is number of T, not in bytes size_t newSize = currentSize + utf_size_of_utf_string(m_data, other); // size is number of T, not in bytes CheckSize(newSize+1, 0); utf_string_from_utf_string(m_data+currentSize, m_allocatedSize-currentSize, other); m_data[newSize] = 0; }else{ // nothing to do } } /* strcat __String */ template void strcat(const __String& other) { size_t currentSize = size_of_utf_string(m_data); // size is number of T, not in bytes size_t newSize = currentSize + utf_size_of_utf_string(m_data, other.s()); // size is number of T, not in bytes CheckSize(newSize+1, 0); utf_string_from_utf_string(m_data+currentSize, m_allocatedSize-currentSize, other.s()); m_data[newSize] = 0; } /* strncat */ template void strncat(const O* other, size_t other_len) { if ( other && *other && other_len > 0 ) { size_t currentSize = size_of_utf_string(m_data); size_t newSize = currentSize + utf_size_of_utf_string_len(m_data, other, other_len); CheckSize(newSize+1, 0); utf_string_from_utf_string_len(m_data+currentSize, m_allocatedSize, other, other_len); m_data[newSize] = 0; }else{ // nothing to do } } /* takeValueFrom */ template ThisXStringClass& takeValueFrom(const __String& S) { strcpy(S.s()); return *((ThisXStringClass*)this); } template ThisXStringClass& takeValueFrom(const O* S) { strcpy(S); return *((ThisXStringClass*)this); } template ThisXStringClass& takeValueFrom(const O C) { strcpy(C); return *((ThisXStringClass*)this); } template ThisXStringClass& takeValueFrom(const __String& S, size_t len) { strncpy(S.data(0), len); return *((ThisXStringClass*)this); } template ThisXStringClass& takeValueFrom(const O* S, size_t len) { strncpy(S, len); return *((ThisXStringClass*)this); } /* += */ template ThisXStringClass& operator += (const __String& S) { strcat(S.s()); return *((ThisXStringClass*)this); } template ThisXStringClass& operator += (O S) { strcat(S); return *((ThisXStringClass*)this); } template ThisXStringClass& operator += (const O* S) { strcat(S); return *((ThisXStringClass*)this); } }; template T XStringAbstract::nullChar = 0; //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ///* __String + char32_t */ //template //XStringClass1 operator + (const __String& p1, char32_t p2) { XStringClass1 s; s.takeValueFrom(p1); s.strcat(p2); return s; } // ///* __String + __String */ //template //XStringClass1 operator + (const __String& p1, const __String& p2) { XStringClass1 s; s.takeValueFrom(p1); s.strcat(p2); return s; } // ///* char* + __String */ //template //XStringClass2 operator + (const CharType1* p1, const __String& p2) { XStringClass2 s; s.takeValueFrom(p1); s.strcat(p2); return s; } // ///* __String + char* */ //template //XStringClass1 operator + (const __String& p1, const CharType2* p2) { XStringClass1 s; s.takeValueFrom(p1); s.strcat(p2); return s; } template _xtools__true_type is_base_of_test_func( Base* ); template _xtools__false_type is_base_of_test_func( void* ); template auto test_pre_is_base_of(int) -> decltype(is_base_of_test_func(static_cast(nullptr))); template< class, class = _xtools__void_t<>, class = _xtools__void_t<> > struct __string_type { typedef void type; }; template< typename T > struct __string_type, _xtools__void_t> { typedef __String type; }; #define is___String_t(x) decltype(test_pre_is_base_of< typename __string_type::type , x>(0)) #define is___String(x) is___String_t(x)::value template< class, class = _xtools__void_t<>, class = _xtools__void_t<> > struct __lstring_type { typedef void type; }; template< typename T > struct __lstring_type, _xtools__void_t> { typedef LString type; }; #define is___LString_t(x) decltype(test_pre_is_base_of< typename __lstring_type::type , x>(0)) #define is___LString(x) is___LString_t(x)::value /* __string_class_or::type is T1 is T1 is a subclass of __String. If T1 is not a subclass of __String, returns T2 if it's a subclass of __String */ template struct __string_class_or; template struct __string_class_or { /*typedef double type;*/ }; template struct __string_class_or { typedef typename T1::xs_t type; }; template struct __string_class_or { typedef typename T2::xs_t type; }; //------------------------------------------------------- + operator template typename __string_class_or::type operator + (T1 p1, T2 p2) { typename __string_class_or::type s; s.takeValueFrom(p1); s.strcat(p2); return s; } //------------------------------------------------------- #undef DBG_XSTRING #undef asciiToLower #undef m_data #endif // __XSTRINGABSTRACT_H__