2020-02-15 15:51:18 +01:00
//*************************************************************************************************
//*************************************************************************************************
//
// STRING
//
//*************************************************************************************************
//*************************************************************************************************
# if !defined(__XSTRINGW_H__)
# define __XSTRINGW_H__
2020-02-23 21:23:31 +01:00
# include "XToolsCommon.h"
2020-04-09 17:46:17 +02:00
# include "unicode_conversions.h"
2020-02-15 15:51:18 +01:00
# define LPATH_SEPARATOR L'\\'
2020-03-31 17:59:35 +02:00
extern xsize XStringWGrowByDefault ;
2020-03-12 15:40:38 +01:00
//extern void __GLOBAL__sub_I_XStringW();
2020-02-15 15:51:18 +01:00
class XStringW
{
protected :
wchar_t * m_data ;
2020-03-31 17:59:35 +02:00
xsize m_len ;
xsize m_allocatedSize ;
2020-02-15 15:51:18 +01:00
2020-03-26 13:59:20 +01:00
// convenience method. Did it this way to avoid #define in header. They can have an impact on other headers
2020-03-21 14:12:26 +01:00
xsize min ( xsize x1 , xsize x2 ) const { if ( x1 < x2 ) return x1 ; return x2 ; }
2020-03-26 13:59:20 +01:00
xsize max ( xsize x1 , xsize x2 ) const { if ( x1 > x2 ) return x1 ; return x2 ; }
// Next 2 methods are protected intentionally. They are const method returning non-const pointer. That's intentional, but dangerous. Do not expose to public.
// It's better practice, if you need a non-const pointer for low-level access, to use dataSized and ahev to specify the size
wchar_t * _data ( unsigned int ui ) const { if ( ui > = m_allocatedSize ) panic ( " wchar_t* data(unsigned int ui=0) -> ui >= m_allocatedSize " ) ; return m_data + ui ; }
wchar_t * _data ( int i ) const { if ( i < 0 ) panic ( " wchar_t* data(int i) -> i < 0 " ) ; if ( ( unsigned int ) i > = m_allocatedSize ) panic ( " wchar_t* data(int i) -> i >= m_allocatedSize " ) ; return m_data + i ; }
wchar_t * _data ( unsigned long ui ) const { if ( ui > = m_allocatedSize ) panic ( " wchar_t* data(unsigned long ui=0) -> ui >= m_allocatedSize " ) ; return m_data + ui ; }
wchar_t * _data ( long i ) const { if ( i < 0 ) panic ( " wchar_t* data(long i) -> i < 0 " ) ; if ( ( unsigned long ) i > = m_allocatedSize ) panic ( " wchar_t* data(long i) -> i >= m_allocatedSize " ) ; return m_data + i ; }
wchar_t * _data ( xsize ui ) const { if ( ui > = m_allocatedSize ) panic ( " wchar_t* data(xsize ui=0) -> ui >= m_allocatedSize " ) ; return m_data + ui ; }
wchar_t * _data ( xisize i ) const { if ( i < 0 ) panic ( " wchar_t* data(xisize i) -> i < 0 " ) ; if ( ( xsize ) i > = m_allocatedSize ) panic ( " wchar_t* data(xisize i) -> i >= m_allocatedSize " ) ; return m_data + i ; }
2020-03-21 14:12:26 +01:00
2020-02-15 15:51:18 +01:00
public :
2020-03-31 17:59:35 +02:00
void Init ( xsize aSize = 0 ) ;
2020-02-15 15:51:18 +01:00
XStringW ( ) ;
XStringW ( const XStringW & aString ) ;
2020-03-10 17:50:55 +01:00
// XStringW(const wchar_t *);
2020-03-31 17:59:35 +02:00
// XStringW(const wchar_t* S, xsize count);
2020-03-10 17:50:55 +01:00
// XStringW(const wchar_t);
// XStringW(const char*);
2020-02-24 14:07:24 +01:00
2020-02-15 15:51:18 +01:00
~ XStringW ( ) ;
protected :
2020-03-31 17:59:35 +02:00
wchar_t * CheckSize ( xsize nNewSize , xsize nGrowBy = XStringWGrowByDefault ) ;
2020-02-15 15:51:18 +01:00
public :
2020-03-26 13:59:20 +01:00
const wchar_t * wc_str ( ) const { return m_data ; } // equivalent as std::string
const wchar_t * data ( xsize ui = 0 ) const { return m_data + ui ; } // do not multiply by sizeof(wchar_t), it's done by the compiler.
const wchar_t * data ( xisize i ) const { if ( i < 0 ) panic ( " const wchar_t *data(INTN i=0) const -> i < 0 " ) ; return m_data + i ; } // do not multiply by sizeof(wchar_t), it's done by the compiler.
2020-02-15 15:51:18 +01:00
2020-03-26 13:59:20 +01:00
wchar_t * dataSized ( xsize ui , xsize sizeMin , xsize nGrowBy = XStringWGrowByDefault ) { CheckSize ( ui + sizeMin , nGrowBy ) ; return _data ( ui ) ; }
wchar_t * dataSized ( xisize i , xsize sizeMin , xsize nGrowBy = XStringWGrowByDefault ) { if ( i < 0 ) panic ( " wchar_t* dataSized(xisize i, xsize sizeMin, xsize nGrowBy) -> i < 0 " ) ; CheckSize ( ( xsize ) i + sizeMin , nGrowBy ) ; return _data ( i ) ; }
wchar_t * forgetDataWithoutFreeing ( ) ;
2020-03-21 14:12:26 +01:00
2020-04-09 18:32:46 +02:00
// xsize length() const { return m_len; }
2020-03-31 17:59:35 +02:00
xsize size ( ) const { return m_len ; }
2020-04-05 14:25:39 +02:00
xsize sizeInBytes ( ) const { return m_len * sizeof ( wchar_t ) ; }
2020-03-31 17:59:35 +02:00
xsize allocatedSize ( ) const { return m_allocatedSize ; }
void SetLength ( xsize len ) ;
2020-03-10 12:51:04 +01:00
const wchar_t * s ( ) { return m_data ; }
2020-02-15 15:51:18 +01:00
2020-03-11 15:23:58 +01:00
/* Empty ? */
2020-03-10 12:51:04 +01:00
void setEmpty ( ) { m_len = 0 ; }
bool isEmpty ( ) const { return size ( ) = = 0 ; }
2020-04-09 18:32:46 +02:00
bool notEmpty ( ) const { return ! isEmpty ( ) ; }
2020-02-15 15:51:18 +01:00
/* Cast */
2020-04-09 18:32:46 +02:00
// operator const wchar_t *() const { return data(); }
2020-02-15 15:51:18 +01:00
# if defined(__APPLE__) && defined(__OBJC__)
operator NSString * ( ) const { return [ [ [ NSString alloc ] initWithBytes : data ( ) length : length ( ) * sizeof ( wchar_t ) encoding : NSUTF32LittleEndianStringEncoding ] autorelease ] ; }
# endif
int ToInt ( ) const ;
2020-03-31 17:59:35 +02:00
xsize ToUInt ( ) const ;
2020-02-15 15:51:18 +01:00
// XString mbs() const;
2020-04-09 18:32:46 +02:00
// /* wchar_t [] */
// wchar_t operator [](int i) const { return *_data(i); }
// wchar_t operator [](unsigned int ui) const { return *_data(ui); }
// wchar_t operator [](long i) const { return *_data(i); }
// wchar_t operator [](unsigned long ui) const { return *_data(ui); }
// wchar_t operator [](xisize i) const { return *data(i); }
// wchar_t operator [](xsize ui) const { return *data(ui); }
//
// /* wchar_t& [] */
// wchar_t& operator [](int i) { return *_data(i); }
// wchar_t& operator [](unsigned int ui) { return *_data(ui); }
// wchar_t& operator [](long i) { return *_data(i); }
// wchar_t& operator [](unsigned long ui) { return *_data(ui); }
// wchar_t& operator [](xisize i) { return *_data(i); }
// wchar_t& operator [](xsize ui) { return *_data(ui); }
wchar_t LastChar ( ) const { if ( size ( ) > 0 ) return data ( ) [ size ( ) - 1 ] ; else return 0 ; }
2020-02-15 15:51:18 +01:00
void RemoveLastEspCtrl ( ) ;
void SetNull ( ) { SetLength ( 0 ) ; } ;
2020-03-31 17:59:35 +02:00
void StrnCpy ( const wchar_t * buf , xsize len ) ;
2020-02-15 15:51:18 +01:00
void StrCpy ( const wchar_t * buf ) ;
2020-03-31 17:59:35 +02:00
void StrnCat ( const wchar_t * buf , xsize len ) ;
2020-02-15 15:51:18 +01:00
void StrCat ( const wchar_t * buf ) ;
void StrCat ( const XStringW & uneXStringW ) ;
2020-03-31 17:59:35 +02:00
void Delete ( xsize pos , xsize count = 1 ) ;
2020-02-15 15:51:18 +01:00
2020-03-31 17:59:35 +02:00
void Insert ( xsize pos , const XStringW & Str ) ;
2020-02-15 15:51:18 +01:00
2020-03-31 18:10:30 +02:00
void vSWPrintf ( const char * format , va_list va ) ;
void SWPrintf ( const char * format , . . . ) __attribute__ ( ( __format__ ( __printf__ , 2 , 3 ) ) ) ;
2020-02-15 15:51:18 +01:00
const XStringW & operator = ( const XStringW & aString ) ;
2020-03-10 17:50:55 +01:00
// const XStringW &operator =(const wchar_t* S) {fdsf};
// const XStringW &operator =(wchar_t);
const XStringW & takeValueFrom ( const wchar_t * S ) ;
2020-03-31 17:59:35 +02:00
const XStringW & takeValueFrom ( const wchar_t * S , xsize count ) ;
2020-03-10 17:50:55 +01:00
const XStringW & takeValueFrom ( const char * S ) ;
2020-02-15 15:51:18 +01:00
const XStringW & operator + = ( const XStringW & ) ;
const XStringW & operator + = ( const wchar_t * S ) ;
const XStringW & operator + = ( wchar_t ) ;
2020-03-31 17:59:35 +02:00
XStringW SubString ( xsize pos , xsize count ) const ;
xsize IdxOf ( wchar_t c , xsize Pos = 0 ) const ;
xsize IdxOf ( const XStringW & S , xsize Pos = 0 ) const ;
xsize RIdxOf ( const wchar_t c , xsize Pos = MAX_XSIZE ) const ;
xsize RIdxOf ( const XStringW & S , xsize Pos = MAX_XSIZE ) const ;
2020-02-15 15:51:18 +01:00
void ToLower ( bool FirstCharIsCap = false ) ;
bool IsLetters ( ) const ;
bool IsLettersNoAccent ( ) const ;
bool IsDigits ( ) const ;
2020-03-31 17:59:35 +02:00
bool IsDigits ( xsize pos , xsize count ) const ;
2020-02-15 15:51:18 +01:00
2020-02-23 21:23:31 +01:00
bool ExistIn ( const XStringW & S ) const { return IdxOf ( S ) ! = MAX_XSIZE ; }
2020-02-15 15:51:18 +01:00
void Replace ( wchar_t c1 , wchar_t c2 ) ;
XStringW SubStringReplace ( wchar_t c1 , wchar_t c2 ) ;
2020-04-09 18:32:46 +02:00
int Compare ( const wchar_t * S ) const { return ( int ) memcmp ( data ( ) , S , min ( wcslen ( S ) , size ( ) ) * sizeof ( wchar_t ) ) ; }
2020-02-15 15:51:18 +01:00
bool Equal ( const wchar_t * S ) const { return Compare ( S ) = = 0 ; } ;
2020-03-21 14:12:26 +01:00
bool BeginingEqual ( const wchar_t * S ) const { return ( memcmp ( data ( ) , S , wcslen ( S ) ) = = 0 ) ; }
2020-03-31 17:59:35 +02:00
bool SubStringEqual ( xsize Pos , const wchar_t * S ) const { return ( memcmp ( data ( Pos ) , S , wcslen ( S ) ) = = 0 ) ; }
2020-02-15 15:51:18 +01:00
XStringW basename ( ) const ;
XStringW dirname ( ) const ;
2020-03-31 17:59:35 +02:00
// bool ReadFromBuf(const char *buf, xsize *idx, xsize count);
// bool WriteToBuf(char *buf, xsize *idx, xsize count) const;
2020-02-15 15:51:18 +01:00
// bool ReadFromFILE(FILE *fp);
// bool WriteToFILE(FILE *fp) const;
//
// bool ReadFromXBuffer(XRBuffer &unXBuffer); // Impossible de mettre le XBuffer en const car il y a une variable d'instance de XBuffer incrémentée par ReadFromXBuffer
// void CatToXBuffer(XBuffer *unXBuffer) const;
2020-03-31 17:59:35 +02:00
// void WriteToXBuffer(XBuffer *unXBuffer, xsize *idx) const;
2020-02-15 15:51:18 +01:00
public :
// + operator
// with XStringW
friend XStringW operator + ( const XStringW & p1 , const XStringW & p2 ) { XStringW s ; s = p1 ; s + = p2 ; return s ; }
// with const wchar_t
friend XStringW operator + ( const XStringW & p1 , const wchar_t * p2 ) { XStringW s ; s = p1 ; s + = p2 ; return s ; }
2020-03-10 17:50:55 +01:00
friend XStringW operator + ( const wchar_t * p1 , const XStringW & p2 ) { XStringW s ; s . StrCat ( p1 ) ; s . StrCat ( p2 ) ; return s ; }
2020-02-15 15:51:18 +01:00
// // with wchar_t
// friend XStringW operator + (const XStringW& p1, wchar_t p2 ) { XStringW s; s=p1; s+=p2; return s; }
// friend XStringW operator + (wchar_t p1, const XStringW& p2 ) { XStringW s; s=p1; s+=p2; return s; }
2020-03-21 14:12:26 +01:00
2020-02-15 15:51:18 +01:00
// == operator
2020-04-09 18:32:46 +02:00
friend bool operator = = ( const XStringW & s1 , const XStringW & s2 ) { return s1 . Compare ( s2 . _data ( 0 ) ) = = 0 ; }
2020-02-15 15:51:18 +01:00
friend bool operator = = ( const XStringW & s1 , const wchar_t * s2 ) { return s1 . Compare ( s2 ) = = 0 ; }
friend bool operator = = ( const wchar_t * s1 , const XStringW & s2 ) { return s2 . Compare ( s1 ) = = 0 ; }
2020-04-09 18:32:46 +02:00
friend bool operator ! = ( const XStringW & s1 , const XStringW & s2 ) { return s1 . Compare ( s2 . _data ( 0 ) ) ! = 0 ; }
2020-02-15 15:51:18 +01:00
friend bool operator ! = ( const XStringW & s1 , const wchar_t * s2 ) { return s1 . Compare ( s2 ) ! = 0 ; }
friend bool operator ! = ( const wchar_t * s1 , const XStringW & s2 ) { return s2 . Compare ( s1 ) ! = 0 ; }
2020-04-09 18:32:46 +02:00
friend bool operator < ( const XStringW & s1 , const XStringW & s2 ) { return s1 . Compare ( s2 . _data ( 0 ) ) < 0 ; }
2020-02-15 15:51:18 +01:00
friend bool operator < ( const XStringW & s1 , const wchar_t * s2 ) { return s1 . Compare ( s2 ) < 0 ; }
friend bool operator < ( const wchar_t * s1 , const XStringW & s2 ) { return s2 . Compare ( s1 ) > 0 ; }
2020-04-09 18:32:46 +02:00
friend bool operator > ( const XStringW & s1 , const XStringW & s2 ) { return s1 . Compare ( s2 . _data ( 0 ) ) > 0 ; }
2020-02-15 15:51:18 +01:00
friend bool operator > ( const XStringW & s1 , const wchar_t * s2 ) { return s1 . Compare ( s2 ) > 0 ; }
friend bool operator > ( const wchar_t * s1 , const XStringW & s2 ) { return s2 . Compare ( s1 ) < 0 ; }
2020-04-09 18:32:46 +02:00
friend bool operator < = ( const XStringW & s1 , const XStringW & s2 ) { return s1 . Compare ( s2 . _data ( 0 ) ) < = 0 ; }
2020-02-15 15:51:18 +01:00
friend bool operator < = ( const XStringW & s1 , const wchar_t * s2 ) { return s1 . Compare ( s2 ) < = 0 ; }
friend bool operator < = ( const wchar_t * s1 , const XStringW & s2 ) { return s2 . Compare ( s1 ) > = 0 ; }
2020-04-09 18:32:46 +02:00
friend bool operator > = ( const XStringW & s1 , const XStringW & s2 ) { return s1 . Compare ( s2 . _data ( 0 ) ) > = 0 ; }
2020-02-15 15:51:18 +01:00
friend bool operator > = ( const XStringW & s1 , const wchar_t * s2 ) { return s1 . Compare ( s2 ) > = 0 ; }
friend bool operator > = ( const wchar_t * s1 , const XStringW & s2 ) { return s2 . Compare ( s1 ) < = 0 ; }
} ;
2020-03-31 17:59:35 +02:00
extern const XStringW NullXStringW ;
XStringW operator " " _XSW ( const wchar_t * s , size_t len ) ;
XStringW SWPrintf ( const char * format , . . . ) __attribute__ ( ( __format__ ( __printf__ , 1 , 2 ) ) ) ;
XStringW SubString ( const wchar_t * S , xsize pos , xsize count ) ;
2020-02-15 15:51:18 +01:00
XStringW CleanCtrl ( const XStringW & S ) ;
# endif