CloverBootloader/rEFIt_UEFI/cpp_foundation/XStringW.h

227 lines
10 KiB
C
Raw Normal View History

//*************************************************************************************************
//*************************************************************************************************
//
// STRING
//
//*************************************************************************************************
//*************************************************************************************************
#if !defined(__XSTRINGW_H__)
#define __XSTRINGW_H__
#include "XToolsCommon.h"
2020-02-24 14:07:24 +01:00
#include "utf8Conversion.h"
#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();
class XStringW
{
protected:
wchar_t *m_data;
2020-03-31 17:59:35 +02:00
xsize m_len;
xsize m_allocatedSize;
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
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; }
public:
2020-03-31 17:59:35 +02:00
void Init(xsize aSize=0);
XStringW();
XStringW(const XStringW &aString);
// XStringW(const wchar_t *);
2020-03-31 17:59:35 +02:00
// XStringW(const wchar_t* S, xsize count);
// XStringW(const wchar_t);
// XStringW(const char*);
2020-02-24 14:07:24 +01:00
~XStringW();
protected:
2020-03-31 17:59:35 +02:00
wchar_t *CheckSize(xsize nNewSize, xsize nGrowBy = XStringWGrowByDefault);
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-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-31 17:59:35 +02:00
xsize length() const { return m_len; }
xsize size() const { return m_len; }
xsize allocatedSize() const { return m_allocatedSize; }
void SetLength(xsize len);
const wchar_t* s() { return m_data; }
/* Empty ? */
void setEmpty() { m_len = 0; }
bool isEmpty() const { return size() == 0; }
// bool IsNull() const { return size() == 0 ; }
// bool NotNull() const { return size() > 0 ; }
/* Cast */
operator const wchar_t *() const { return data(); }
#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;
// XString mbs() const;
2020-03-26 13:59:20 +01: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 ( length() > 0 ) return data()[length()-1]; else return 0; }
void RemoveLastEspCtrl();
void SetNull() { SetLength(0); };
2020-03-31 17:59:35 +02:00
void StrnCpy(const wchar_t *buf, xsize len);
void StrCpy(const wchar_t *buf);
2020-03-31 17:59:35 +02:00
void StrnCat(const wchar_t *buf, xsize len);
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-03-31 17:59:35 +02:00
void Insert(xsize pos, const XStringW& Str);
void vSWPrintf(const char* format, va_list va);
#ifndef _MSC_VER
void SWPrintf(const char* format, ...) __attribute__((__format__(__printf__, 2, 3)));
#else
void SWPrintf(const char* format, ...);
#endif // !__MSC_VER
const XStringW &operator =(const XStringW &aString);
// 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);
const XStringW& takeValueFrom(const char* S);
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;
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;
bool ExistIn(const XStringW &S) const { return IdxOf(S) != MAX_XSIZE; }
void Replace(wchar_t c1, wchar_t c2);
XStringW SubStringReplace(wchar_t c1, wchar_t c2);
int Compare(const wchar_t* S) const { return (int)memcmp(data(), S, min(wcslen(S), length())*sizeof(wchar_t)); }
bool Equal(const wchar_t* S) const { return Compare(S) == 0; };
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); }
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;
// 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;
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; }
friend XStringW operator + (const wchar_t *p1, const XStringW& p2) { XStringW s; s.StrCat(p1); s.StrCat(p2); return s; }
// // 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; }
// == operator
friend bool operator == (const XStringW& s1, const XStringW& s2) { return s1.Compare(s2) == 0; }
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; }
friend bool operator != (const XStringW& s1, const XStringW& s2) { return s1.Compare(s2) != 0; }
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; }
friend bool operator < (const XStringW& s1, const XStringW& s2) { return s1.Compare(s2) < 0; }
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; }
friend bool operator > (const XStringW& s1, const XStringW& s2) { return s1.Compare(s2) > 0; }
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; }
friend bool operator <= (const XStringW& s1, const XStringW& s2) { return s1.Compare(s2) <= 0; }
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; }
friend bool operator >= (const XStringW& s1, const XStringW& s2) { return s1.Compare(s2) >= 0; }
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);
#ifndef _MSC_VER
2020-03-31 17:59:35 +02:00
XStringW SWPrintf(const char* format, ...) __attribute__((__format__(__printf__, 1, 2)));
#else
2020-03-31 17:59:35 +02:00
XStringW SWPrintf(const char* format, ...);
#endif // !__MSC_VER
2020-03-31 17:59:35 +02:00
XStringW SubString(const wchar_t *S, xsize pos, xsize count);
XStringW CleanCtrl(const XStringW &S);
#endif