Accueil > > > GSTRING - GESTION DES CHAINES DE CARACTÈRES
GSTRING - GESTION DES CHAINES DE CARACTÈRES
Information sur la source
Description
Voici une petite classe permettant de gérer les chaines de caractères tout comme les std::string, avec une multitude de méthodes rajoutes. J'attends vos avis et idées d'améliorations, les autres classe de ma lib suivront quand elles seront finis ou pratiquement fini comme celle-ci.
Source
- #ifndef __GSTRING_H__
- # define __GSTRING_H__
-
- #include <string>
- #include <iostream>
-
- #include "GChar.h"
- #include "GException.h"
-
- #if defined (WIN32) | defined (_WIN32) | defined (__WIN32) | defined (WIN64) | defined (__WIN64)
- #include "atlbase.h"
- #include "atlstr.h"
- #include "comutil.h"
- #endif
-
- class GStringList;
-
- class GString
- {
- public:
- enum SplitOption
- {
- SKIP_EMPTY_PARTS = 0x001,
- KEEP_EMPTY_PARTS = 0x002
- };
- enum TrimOption
- {
- TRIM_BLANK = 0x001,
- TRIM_SPACE = 0x002,
- TRIM_TAB = 0x003,
- TRIM_ENTER = 0x004,
- TRIM_NUMBER = 0x005,
- TRIM_ALPHA = 0x006,
- TRIM_ALPHANUM = 0x007
- };
- enum IndexOption
- {
- INDEX_ALL_CHAR = 0x001,
- INDEX_THIS_CHAR = 0x002
- };
- enum CaseOption
- {
- CASE_SENSITIVE = 0x001,
- CASE_INSENSITIVE = 0x002
- };
-
- // CONSTRUCTEURS
-
- /* OK */ GString(void);
- /* OK */ GString(const char *);
- /* OK */ GString(const GString &);
- /* OK */ GString(const GString *);
- /* OK */ GString(bool test);
- /* OK */ GString(const std::string &);
- /* OK */ GString(const GChar &);
- /* OK */ GString(const GChar *);
- #if defined (WIN32) | defined (_WIN32) | defined (__WIN32) | defined (WIN) | defined (WIN64) | defined (__WIN64)
- /* OK */ GString(const std::wstring &);
- /* OK */ GString(const wchar_t *);
- /* OK */ GString(const CString &);
- #endif
- /* OK */ GString(char);
- /* OK */ GString(unsigned char);
- /* OK */ GString(int);
- /* OK */ GString(unsigned int);
- /* OK */ GString(long);
- /* OK */ GString(unsigned long);
- /* OK */ GString(short);
- /* OK */ GString(unsigned short);
- /* OK */ GString(float , unsigned int = 6);
- /* OK */ GString(double , unsigned int = 6);
- /* OK */ ~GString(void);
-
- // --== CONVERSIONS ==--
-
- #if defined (WIN32) | defined (_WIN32) | defined (__WIN32) | defined (WIN) | defined (WIN64) | defined (__WIN64)
- /* OK */ std::wstring &ToWString(void) const;
- /* OK */ wchar_t *ToWChar_t(void) const;
- /* OK */ CString &ToCString(void) const;
- /* OK */ LPCSTR ToLPCSTR(void) const;
- #endif
- /* OK */ bool ToBool(void) const;
- /* OK */ std::string &ToStdString(void) const;
- /* OK */ char *ToChar(void) const;
-
- // --== SURCHARGES ==--
-
- /* OK */ bool operator==(const GString &);
- /* OK */ GString &operator=(const GString &);
- /* OK */ GString &operator+=(const GString &);
- /* OK */ GString &operator+(const GString &);
- /* OK */ char operator[](unsigned int);
- /* OK */ bool operator>(const GString &);
- /* OK */ bool operator<(const GString &);
- /* OK */ friend std::ostream &operator<<(std::ostream &out, const GString &);
- /* OK */ friend std::ostream &operator<<(std::ostream &out, const GString *);
-
- // --== METHODES ==--
-
- /* OK */ unsigned int Length(void) const;
- /* OK */ unsigned int Size(void) const;
- /* OK */ unsigned int Count(const GString &, CaseOption = GString::CASE_SENSITIVE);
- /* OK */ int FirstIndexOf(char, IndexOption = GString::INDEX_ALL_CHAR);
- /* OK */ int LastIndexOf(char, IndexOption = GString::INDEX_ALL_CHAR);
- /* OK */ int FirstIndexOf(char, char, IndexOption = GString::INDEX_ALL_CHAR);
- /* OK */ int LastIndexOf(char, char, IndexOption = GString::INDEX_ALL_CHAR);
- /* OK */ int Find(const GString &, CaseOption = GString::CASE_SENSITIVE);
- /* OK */ int Find(const GString &, unsigned int, CaseOption = GString::CASE_SENSITIVE);
- /* OK */ bool IsNumeric(void);
- /* OK */ bool Compare(const GString &);
- /* OK */ bool IsAlpha(void);
- /* OK */ bool EndWith(const GString &);
- /* OK */ bool StartWith(const GString &);
- /* OK */ bool IsEmpty(void) const;
- /* OK */ GString &Fill(char) const;
- /* OK */ GString &Fill(char, unsigned int) const;
- /* OK */ GString &ToUpper(void) const;
- /* OK */ GString &ToLower(void) const;
- /* OK */ static GString &Join(GStringList &, const GString &, SplitOption = GString::SKIP_EMPTY_PARTS);
- /* OK */ static GString &Join(GStringList &, SplitOption = GString::SKIP_EMPTY_PARTS);
- /* OK */ static GString &Implode(GStringList &, const GString &, SplitOption = GString::SKIP_EMPTY_PARTS);
- /* OK */ static GString &Implode(GStringList &, SplitOption = GString::SKIP_EMPTY_PARTS);
- /* OK */ GString &Remove(unsigned int, unsigned int) const;
- /* OK */ GString &Remove(const GString &, CaseOption = GString::CASE_SENSITIVE);
- /* OK */ GString &Substr(unsigned int, unsigned int) const;
- /* OK */ GString &RightJustified(unsigned int, char = ' ');
- /* OK */ GString &LeftJustified(unsigned int, char = ' ');
- /* OK */ GString &Insert(unsigned int, const GString &) const;
- /* OK */ GString &Replace(const GString &, const GString &, CaseOption = GString::CASE_SENSITIVE);
- /* OK */ GString &RightTrim(TrimOption c = GString::TRIM_BLANK);
- /* OK */ GString &LeftTrim(TrimOption c = GString::TRIM_BLANK);
- /* OK */ GString &Trim(TrimOption c = GString::TRIM_BLANK);
- /* OK */ GStringList &Split(const GString &, SplitOption = GString::SKIP_EMPTY_PARTS , CaseOption = GString::CASE_SENSITIVE);
- /* OK */ GStringList &Explode(const GString &, SplitOption = GString::SKIP_EMPTY_PARTS , CaseOption = GString::CASE_SENSITIVE);
- /* OK */ void Clear(void);
- /* OK */ void Truncate(unsigned int);
-
- private:
- char *_str;
- unsigned int _size;
- };
-
- #endif
-
-
-
- #include "GString.h"
- #include "GStringList.h"
- // CONSTRUCTEURS
- GString::GString(void)
- {
- this->_str = new char [1];
- this->_str[0] = 0;
- this->_size = 0;
- }
-
- GString::GString(const char *str)
- {
- this->_size = 0;
- while (str[this->_size])
- this->_size++;
- this->_str = new char[this->_size];
- for (unsigned int i = 0; i < this->_size + 1; ++i)
- this->_str[i] = str[i];
- }
-
- GString::GString(const GString &s)
- {
- this->_size = s._size;
- this->_str = new char[this->_size + 1];
- for (unsigned int i = 0; i < this->_size + 1; ++i)
- this->_str[i] = s._str[i];
- }
- GString::GString(const GString *s)
- {
- this->_size = s->_size;
- this->_str = new char[this->_size + 1];
- for (unsigned int i = 0; i < this->_size + 1; ++i)
- this->_str[i] = s->_str[i];
- }
- GString::GString(bool test)
- {
- if (test == true)
- {
- this->_str = new char[5];
- this->_str[0] = 't';
- this->_str[1] = 'r';
- this->_str[2] = 'u';
- this->_str[3] = 'e';
- this->_str[4] = 0;
- this->_size = 4;
- }
- else
- {
- this->_str = new char[6];
- this->_str[0] = 'f';
- this->_str[1] = 'a';
- this->_str[2] = 'l';
- this->_str[3] = 's';
- this->_str[4] = 'e';
- this->_str[5] = 0;
- this->_size = 5;
- }
- }
-
- GString::GString(const std::string &s)
- {
- this->_size = s.size();
- this->_str = new char[this->_size + 1];
- for (unsigned int i = 0; i < this->_size + 1; i++)
- this->_str[i] = s[i];
- }
-
- GString::GString(const GChar &c)
- {
- this->_str = new char[2];
- this->_str[0] = c.GetChar();
- this->_str[1] = 0;
- this->_size = 1;
- }
- GString::GString(const GChar *c)
- {
- this->_str = new char[2];
- this->_str[0] = c->GetChar();
- this->_str[1] = 0;
- this->_size = 1;
- }
- GString::GString(char c)
- {
- this->_str = new char[2];
- this->_str[0] = c;
- this->_str[1] = 0;
- this->_size = 1;
- }
- GString::GString(unsigned char c)
- {
- this->_str = new char[2];
- this->_str[0] = c;
- this->_str[1] = 0;
- this->_size = 1;
- }
- #if defined (WIN32) | defined (_WIN32) | defined (__WIN32) | defined (WIN) | defined (WIN64) | defined (__WIN64)
- GString::GString(const std::wstring &wstr)
- {
- this->_size = wstr.size();
- this->_str = new char[this->_size + 1];
- for (unsigned int i = 0; i < this->_size + 1; ++i)
- this->_str[i] = (char)wstr[i];
- }
-
- GString::GString(const wchar_t *str)
- {
- unsigned int i = 0;
- while(str[i])
- i++;
- this->_size = i;
- this->_str = new char[this->_size + 1];
- for (unsigned int i = 0; i < this->_size + 1; ++i)
- this->_str[i] = (char)str[i];
- }
- GString::GString(const CString &s)
- {
- this->_size = s.GetLength();
- this->_str = (char*)LPCTSTR(s);
- }
-
- #endif
- GString::~GString(void)
- {
-
- }
-
- GString::GString(float nbr, unsigned int precision)
- {
- if (precision > 9)
- precision = 9;
- float save = nbr;
- unsigned int i = 0;
- while ((int)save > 0)
- {
- save /= 10;
- i++;
- }
- save = nbr;
- this->_size = i + precision + 1;
- this->_str = new char[this->_size + 1];
- i = 0;
- for (; (int)save > 0; ++i)
- {
- this->_str[this->_size - i - 1 - precision - 1] = ((int)save % 10) + '0';
- save /= 10;
- }
- this->_str[i++] = '.';
- save = nbr - (int)nbr;
- for (unsigned int j = 0; j < precision; ++j, ++i)
- {
- save *= 10;
- this->_str[i] = ((int)save % 10) + '0';
- }
- this->_str[i] = 0;
- }
- GString::GString(double nbr, unsigned int precision)
- {
- if (precision > 9)
- precision = 9;
- double save = nbr;
- unsigned int i = 0;
- while ((int)save > 0)
- {
- save /= 10;
- i++;
- }
- save = nbr;
- this->_size = i + precision + 1;
- this->_str = new char[this->_size + 1];
- i = 0;
- for (; (int)save > 0; ++i)
- {
- this->_str[this->_size - i - 1 - precision - 1] = ((int)save % 10) + '0';
- save /= 10;
- }
- this->_str[i++] = '.';
- save = nbr - (int)nbr;
- for (unsigned int j = 0; j < precision; ++j, ++i)
- {
- save *= 10;
- this->_str[i] = ((int)save % 10) + '0';
- }
- this->_str[i] = 0;
- }
- GString::GString(int nbr)
- {
- int saveNbr = nbr;
- bool isNeg = false;
- this->_size = 0;
- if (nbr < 0)
- {
- isNeg = true;
- nbr *= -1;
- saveNbr *= -1;
- this->_size++;
- }
- while (nbr > 0)
- {
- nbr /= 10;
- this->_size++;
- }
- this->_str = new char[this->_size];
- if (isNeg == true)
- this->_str[0] = '-';
- for (unsigned int i = 0; i < this->_size && saveNbr > 0; ++i)
- {
- this->_str[this->_size - i - 1] = (saveNbr % 10) + '0';
- saveNbr /= 10;
- }
- this->_str[this->_size] = 0;
- }
-
- GString::GString(unsigned int nbr)
- {
- unsigned int saveNbr = nbr;
- this->_size = 0;
- while (nbr > 0)
- {
- nbr /= 10;
- this->_size++;
- }
- this->_str = new char[this->_size];
- for (unsigned int i = 0; i < this->_size && saveNbr > 0; ++i)
- {
- this->_str[this->_size - i - 1] = (saveNbr % 10) + '0';
- saveNbr /= 10;
- }
- this->_str[this->_size] = 0;
- }
- GString::GString(short nbr)
- {
- short saveNbr = nbr;
- bool isNeg = false;
- this->_size = 0;
- if (nbr < 0)
- {
- isNeg = true;
- nbr *= -1;
- saveNbr *= -1;
- this->_size++;
- }
- while (nbr > 0)
- {
- nbr /= 10;
- this->_size++;
- }
- this->_str = new char[this->_size];
- if (isNeg == true)
- this->_str[0] = '-';
- for (unsigned int i = 0; i < this->_size && saveNbr > 0; ++i)
- {
- this->_str[this->_size - i - 1] = (saveNbr % 10) + '0';
- saveNbr /= 10;
- }
- this->_str[this->_size] = 0;
- }
-
- GString::GString(unsigned short nbr)
- {
- unsigned short saveNbr = nbr;
- this->_size = 0;
- while (nbr > 0)
- {
- nbr /= 10;
- this->_size++;
- }
- this->_str = new char[this->_size];
- for (unsigned int i = 0; i < this->_size && saveNbr > 0; ++i)
- {
- this->_str[this->_size - i - 1] = (saveNbr % 10) + '0';
- saveNbr /= 10;
- }
- this->_str[this->_size] = 0;
- }
- GString::GString(long nbr)
- {
- long saveNbr = nbr;
- bool isNeg = false;
- this->_size = 0;
- if (nbr < 0)
- {
- isNeg = true;
- nbr *= -1;
- saveNbr *= -1;
- this->_size++;
- }
- while (nbr > 0)
- {
- nbr /= 10;
- this->_size++;
- }
- this->_str = new char[this->_size];
- if (isNeg == true)
- this->_str[0] = '-';
- for (unsigned int i = 0; i < this->_size && saveNbr > 0; ++i)
- {
- this->_str[this->_size - i - 1] = (saveNbr % 10) + '0';
- saveNbr /= 10;
- }
- this->_str[this->_size] = 0;
- }
-
- GString::GString(unsigned long nbr)
- {
- unsigned long saveNbr = nbr;
- this->_size = 0;
- while (nbr > 0)
- {
- nbr /= 10;
- this->_size++;
- }
- this->_str = new char[this->_size];
- for (unsigned int i = 0; i < this->_size && saveNbr > 0; ++i)
- {
- this->_str[this->_size - i - 1] = (saveNbr % 10) + '0';
- saveNbr /= 10;
- }
- this->_str[this->_size] = 0;
- }
- // METHODE
-
- unsigned int GString::Length(void) const
- {
- return (this->_size);
- }
-
- unsigned int GString::Size(void) const
- {
- return (this->_size);
- }
-
-
- unsigned int GString::Count(const GString &find, CaseOption op)
- {
- int pos = 0;
- int nbr = 0;
- while (true)
- {
- pos = this->Find(find, pos, op);
- if (pos == -1)
- return (nbr);
- nbr++;
- pos += find.Size();
- }
- }
- int GString::LastIndexOf(char c , IndexOption op)
- {
- if (op == GString::INDEX_ALL_CHAR)
- {
- for (int i = this->Length() - 1; i > 0; --i)
- {
- if (this->_str[i] == c)
- return (i);
- }
- }
- else if (op == GString::INDEX_THIS_CHAR)
- {
- for (int i = this->Length() - 1; i > 0; --i)
- {
- if (this->_str[i] != c)
- return (i - 1);
- }
- }
- return (-1);
- }
- int GString::LastIndexOf(char b , char e, IndexOption op)
- {
- for (unsigned int i = this->Length() - 1; i > 0; ++i)
- {
- if (op == GString::INDEX_ALL_CHAR)
- {
- if (this->_str[i] >= b && this->_str[i] <= e)
- return (i);
- }
- else if (op == GString::INDEX_THIS_CHAR)
- {
- if (this->_str[i] < b || this->_str[i] > e)
- return (i - 1);
- }
- }
- return (-1);
- }
- int GString::FirstIndexOf(char c, IndexOption op)
- {
- if (op == GString::INDEX_ALL_CHAR)
- {
- for (unsigned int i = 0; i < this->Length(); ++i)
- {
- if (this->_str[i] == c)
- return (i);
- }
- }
- else if (op == GString::INDEX_THIS_CHAR)
- {
- for (unsigned int i = 0; i < this->Length(); ++i)
- {
- if (this->_str[i] != c)
- return (i);
- }
- }
- return (-1);
- }
- int GString::FirstIndexOf(char c, char d, IndexOption op)
- {
- if (op == GString::INDEX_ALL_CHAR)
- {
- for (unsigned int i = 0; i < this->Length(); ++i)
- {
- if (this->_str[i] >= c && this->_str[i] <= d)
- return (i);
- }
- }
- else if (op == GString::INDEX_THIS_CHAR)
- {
- for (unsigned int i = 0; i < this->Length(); ++i)
- {
- if (this->_str[i] < c || this->_str[i] > d)
- return (i);
- }
- }
- return (-1);
- }
- int GString::Find(const GString &f, CaseOption c)
- {
- if (f.Size() > this->_size)
- return (-1);
- int k = -1;
- for (unsigned int i = 0, j = 0; i < this->_size + 1; ++i)
- {
- if (f._str[j] == 0)
- return (k);
- GChar c1(this->_str[i]);
- GChar c2(f._str[j]);
- if (c == GString::CASE_INSENSITIVE)
- {
- c1 = c1.ToLower();
- c2 = c2.ToLower();
- }
- if (c1 == c2)
- {
- if (k == -1)
- k = i;
- j++;
- }
- else
- {
- k = -1;
- j = 0;
- }
- }
- return (-1);
- }
- int GString::Find(const GString &f, unsigned int pos, CaseOption c)
- {
- if (f.Size() > this->_size)
- return (-1);
- int k = -1;
- for (unsigned int i = pos, j = 0; i < this->_size + 1; ++i)
- {
- if (f._str[j] == 0)
- return (k);
- GChar c1(this->_str[i]);
- GChar c2(f._str[j]);
- if (c == GString::CASE_INSENSITIVE)
- {
- c1 = c1.ToLower();
- c2 = c2.ToLower();
- }
- if (c1 == c2)
- {
- if (k == -1)
- k = i;
- j++;
- }
- else
- {
- k = -1;
- j = 0;
- }
- }
- return (-1);
- }
- void GString::Clear(void)
- {
- this->_str = new char[1];
- this->_str[0] = 0;
- this->_size = 0;
- }
- void GString::Truncate(unsigned int pos)
- {
- if (pos > this->_size)
- return ;
- for (unsigned int i = pos; i < this->_size; ++i)
- this->_str[i] = 0;
- }
- bool GString::IsNumeric(void)
- {
- for (unsigned int i = 0; i < this->_size; ++i)
- {
- if (this->_str[i] < '0' || this->_str[i] > '9')
- return (false);
- }
- return (true);
- }
- bool GString::IsAlpha(void)
- {
- for (unsigned int i = 0; i < this->_size; ++i)
- {
- if (this->_str[i] < 'a' || this->_str[i] > 'z')
- return (false);
- if (this->_str[i] < 'A' || this->_str[i] > 'Z')
- return (false);
- }
- return (true);
- }
- bool GString::IsEmpty(void) const
- {
- if (this->_size)
- return (false);
- return (true);
- }
- bool GString::StartWith(const GString &str)
- {
- unsigned int i = 0;
- if (str.Length() > this->Length())
- return (false);
- while (this->_str[i])
- {
- if (this->_str[i] != str._str[i])
- return (false);
- }
- return (true);
- }
-
- bool GString::EndWith(const GString &str)
- {
- unsigned int i = 0;
- unsigned int size1 = this->Length() - 1;
- unsigned int size2 = str.Length() - 1;
- if ( size2 > size1)
- return (false);
- while (this->_str[size1 - i])
- {
- if (this->_str[size1 - i] != str._str[size2 - i])
- return (false);
- }
- return (true);
- }
-
- bool GString::Compare(const GString &s)
- {
- if (s.Size() != this->Size())
- return (false);
- for (unsigned int i = 0; i < this->Size(); ++i)
- {
- if (this->_str[i] != s._str[i])
- return (false);
- }
- return (true);
- }
-
- GStringList &GString::Split(const GString &f, SplitOption splitop , CaseOption caseop)
- {
- GStringList *vect = new GStringList;
- int pos = 0, pos1 = 0, pos2 = 0;
- while (true)
- {
- pos1 = pos;
- pos = this->Find(f, pos, caseop);
- if (pos == -1)
- break;
- pos2 = pos;
- GString ne = this->Substr(pos1, pos2);
- if ((splitop == GString::SKIP_EMPTY_PARTS && ne.Size() != 0) || splitop == GString::KEEP_EMPTY_PARTS)
- vect->PushBack(ne);
- pos += f.Size();
- }
- if (pos1 != 0)
- {
- GString ne = this->Substr(pos1, this->Size());
- if ((splitop == GString::SKIP_EMPTY_PARTS && ne.Size() != 0) || splitop == GString::KEEP_EMPTY_PARTS)
- vect->PushBack(ne);
- }
- return (*vect);
- }
- GString &GString::Fill(char c) const
- {
- unsigned int size = this->Length();
- char *newStr = new char[size + 1];
- for (unsigned int i = 0; i < size; ++i)
- newStr[i] = c;
- newStr[size] = 0;
- GString *G = new GString(newStr);
- return (*G);
- }
- GString &GString::Fill(char c, unsigned int n) const
- {
- unsigned int size = this->Length();
- char *newStr = new char[size + 1];
- for (unsigned int i = 0; i < size; ++i)
- {
- if (i < n)
- newStr[i] = c;
- else
- newStr[i] = this->_str[i];
- }
- newStr[this->_size] = 0;
- GString *G = new GString(newStr);
- return (*G);
- }
- GString &GString::ToUpper(void) const
- {
- char *newStr = new char[this->_size + 1];
- for (unsigned int i = 0; i < this->_size; ++i)
- {
- if (this->_str[i] >= 'a' && this->_str[i] <= 'z')
- newStr[i] = this->_str[i] - 32;
- else
- newStr[i] = this->_str[i];
- }
- newStr[this->_size] = 0;
- GString *G = new GString(newStr);
- return (*G);
- }
- GString &GString::ToLower(void) const
- {
- char *newStr = new char[this->_size + 1];
- for (unsigned int i = 0; i < this->_size; ++i)
- {
- if (this->_str[i] >= 'A' && this->_str[i] <= 'Z')
- newStr[i] = this->_str[i] + 32;
- else
- newStr[i] = this->_str[i];
- }
- newStr[this->_size] = 0;
- GString *G = new GString(newStr);
- return (*G);
- }
- GString &GString::Substr(unsigned int d, unsigned int f) const
- {
- if (f > this->_size)
- {
- GString *G = new GString(this->_str);
- return (*G);
- }
- char *newStr = new char[f - d + 1];
- for (unsigned int i = d, j = 0; i < f; ++i)
- newStr[j++] = this->_str[i];
- newStr[f - d] = 0;
- GString *G = new GString(newStr);
- return (*G);
- }
-
- GString &GString::Implode(GStringList &list, const GString &sep, SplitOption op)
- {
- GString *n = new GString();
- for (unsigned int i = 0; i < list.Size(); ++i)
- {
- if (op == GString::SKIP_EMPTY_PARTS && list[i].Size() == 0)
- continue;
- if (i != 0)
- *n += sep;
- *n += list[i];
- }
- return (*n);
- }
-
- GString &GString::Join(GStringList &list, const GString &sep, SplitOption op)
- {
- return (GString::Implode(list, sep, op));
- }
-
- GString &GString::Implode(GStringList &list, SplitOption op)
- {
- GString *n = new GString();
- for (unsigned int i = 0; i < list.Size(); ++i)
- {
- if (op == GString::SKIP_EMPTY_PARTS && list[i].Size() == 0)
- continue;
- *n += list[i];
- }
- return (*n);
- }
-
- GString &GString::Join(GStringList &list, SplitOption op)
- {
- return (GString::Implode(list, op));
- }
-
- GString &GString::Remove(unsigned int pos, unsigned int nb) const
- {
- if (pos + nb > this->_size)
- {
- GString *G = new GString(this->_str);
- return (*G);
- }
- char *newStr = new char[this->_size - nb];
- for (unsigned int i = 0, j = 0; i < this->_size; ++i)
- {
- if (i < pos || i >= pos + nb)
- newStr[j++] = this->_str[i];
- }
- newStr[this->_size - nb] = 0;
- GString *G = new GString(newStr);
- return (*G);
- }
- GString &GString::Remove(const GString &g, CaseOption c)
- {
- int pos = this->Find(g, c);
- if (pos == -1)
- return (*(new GString(this->_str)));
- return (this->Remove(pos, g.Size()));
- }
- GString &GString::RightJustified(unsigned int nbr, char c)
- {
- if (nbr <= this->_size)
- return (*(new GString(this->_str)));
- char *newStr = new char[nbr + 1];
- for (unsigned int i = 0, j = 0; i < nbr; ++i)
- {
- if (i < nbr - this->_size)
- newStr[i] = c;
- else
- newStr[i] = this->_str[j++];
- }
- newStr[nbr - this->_size + 1] = 0;
- GString *G = new GString(newStr);
- return (*G);
- }
- GString &GString::LeftJustified(unsigned int nbr, char c)
- {
- if (nbr <= this->_size)
- return (*(new GString(this->_str)));
- char *newStr = new char[nbr + 1];
- for (unsigned int i = 0; i < nbr; ++i)
- {
- if (i < this->_size)
- newStr[i] = this->_str[i];
- else
- newStr[i] = c;
- }
- newStr[nbr - this->_size + 1] = 0;
- GString *G = new GString(newStr);
- return (*G);
- }
-
-
-
- GString &GString::Insert(unsigned int pos, const GString &c) const
- {
- if (pos > this->_size)
- return (*(new GString(this->_str)));
- char *newStr = new char[this->_size + c.Size()];
- for (unsigned int i = 0, j = 0; i < this->_size + 1; ++i)
- {
- if (pos == i)
- {
- for (; j < c.Size(); ++j)
- newStr[i + j] = c._str[j];
- newStr[i + j] = this->_str[i];
- }
- else
- newStr[i + j] = this->_str[i];
- }
- GString *G = new GString(newStr);
- return (*G);
- }
-
- GString &GString::Replace(const GString &f, const GString &r, CaseOption op)
- {
- GString *g = new GString(this->_str);
- int pos = 1;
- while (pos != -1)
- {
- pos = g->Find(f, op);
- if (pos != -1)
- {
- *g = g->Remove(pos, f.Size());
- *g = g->Insert(pos, r);
- }
- }
- return (*g);
- }
- GString &GString::LeftTrim(TrimOption op)
- {
- int indexEnd = 0;
- if (op == GString::TRIM_BLANK)
- {
- for (unsigned int i = 0; i < this->_size; ++i)
- {
- if (this->_str[i] != '\n' && this->_str[i] != '\t' && this->_str[i] != ' ')
- {
- indexEnd = i;
- break;
- }
- }
- }
- else if (op == GString::TRIM_SPACE)
- indexEnd = this->FirstIndexOf(' ', GString::INDEX_THIS_CHAR);
- else if (op == GString::TRIM_TAB)
- indexEnd = this->FirstIndexOf('\t', GString::INDEX_THIS_CHAR);
- else if (op == GString::TRIM_ENTER)
- indexEnd = this->FirstIndexOf('\n', GString::INDEX_THIS_CHAR);
- else if (op == GString::TRIM_NUMBER)
- indexEnd = this->FirstIndexOf('0', '9', GString::INDEX_THIS_CHAR);
- else if (op == GString::TRIM_ALPHA)
- {
- for (unsigned int i = 0; i < this->_size; ++i)
- {
- if (this->_str[i] < 'A' || (this->_str[i] > 'Z' && this->_str[i] < 'a') || this->_str[i] > 'z')
- {
- indexEnd = i;
- break;
- }
- }
- }
- else if (op == GString::TRIM_ALPHANUM)
- {
- for (unsigned int i = 0; i < this->_size; ++i)
- {
- if (this->_str[i] < '0' || (this->_str[i] > '9' && this->_str[i] < 'A') || (this->_str[i] > 'Z' && this->_str[i] < 'a') || this->_str[i] > 'z')
- {
- indexEnd = i;
- break;
- }
- }
- }
- return (this->Substr(indexEnd, this->Size()));
- }
- GString &GString::RightTrim(TrimOption op)
- {
- int indexEnd = 0;
- if (op == GString::TRIM_BLANK)
- {
- for (int i = this->_size - 1; i > 0; --i)
- {
- if (this->_str[i] != '\n' && this->_str[i] != '\t' && this->_str[i] != ' ')
- {
- indexEnd = i;
- break;
- }
- }
- }
- else if (op == GString::TRIM_SPACE)
- indexEnd = this->LastIndexOf(' ', GString::INDEX_THIS_CHAR);
- else if (op == GString::TRIM_TAB)
- indexEnd = this->LastIndexOf('\t', GString::INDEX_THIS_CHAR);
- else if (op == GString::TRIM_ENTER)
- indexEnd = this->LastIndexOf('\n', GString::INDEX_THIS_CHAR);
- else if (op == GString::TRIM_NUMBER)
- indexEnd = this->LastIndexOf('0', '9', GString::INDEX_THIS_CHAR);
- else if (op == GString::TRIM_ALPHA)
- {
- for (int i = this->_size - 1; i > 0; --i)
- {
- if (this->_str[i] < 'A' || (this->_str[i] > 'Z' && this->_str[i] < 'a') || this->_str[i] > 'z')
- {
- indexEnd = i;
- break;
- }
- }
- }
- else if (op == GString::TRIM_ALPHANUM)
- {
- for (int i = this->_size -1; i > 0; --i)
- {
- if (this->_str[i] < '0' || (this->_str[i] > '9' && this->_str[i] < 'A') || (this->_str[i] > 'Z' && this->_str[i] < 'a') || this->_str[i] > 'z')
- {
- indexEnd = i;
- break;
- }
- }
- }
- return (this->Substr(0, indexEnd));
- }
- GString &GString::Trim(TrimOption op)
- {
- GString *g = new GString();
- *g = this->RightTrim(op);
- *g = g->LeftTrim(op);
- return (*g);
- }
- // CONVERSION
-
- #if defined (WIN32) | defined (_WIN32) | defined (__WIN32) | defined (WIN64) | defined (__WIN64)
- std::wstring &GString::ToWString(void) const
- {
- unsigned int size = this->_size;
- wchar_t *ws = new wchar_t[size + 1];
- for (unsigned int i = 0; i < size + 1; i++)
- ws[i] = this->_str[i];
- std::wstring *s = new std::wstring(ws);
- return (*s);
- }
- CString &GString::ToCString(void) const
- {
- CString *s = new CString(this->_str);
- return (*s);
- }
- wchar_t *GString::ToWChar_t(void) const
- {
- unsigned int size = this->_size;
- wchar_t *ws = new wchar_t[size + 1];
- for (unsigned int i = 0; i < size + 1; i++)
- ws[i] = this->_str[i];
- return (ws);
- }
- LPCSTR GString::ToLPCSTR(void) const
- {
- LPCSTR test = this->_str;
- return (test);
-
- }
-
- #endif
- bool GString::ToBool(void) const
- {
- GString f("false");
- if (f == GString(this->_str))
- return (false);
- GString t("true");
- if (t == GString(this->_str))
- return (true);
- throw GException(GException::NOT_A_BOOLEEN);
- }
- std::string &GString::ToStdString(void) const
- {
- std::string *s = new std::string(this->_str);
- return (*s);
- }
-
- char *GString::ToChar(void) const
- {
- char *newStr = new char[this->Size() + 1];
- for (unsigned int i = 0; i < this->Size() + 1; ++i)
- newStr[i] = this->_str[i];
- return (newStr);
- }
-
-
- // SURCHARGES OPERATEURS
-
- std::ostream &operator<<(std::ostream &out, const GString &s)
- {
- out << s._str;
- return (out);
- }
-
- std::ostream &operator<<(std::ostream &out, const GString *s)
- {
- out << s->_str;
- return (out);
- }
- char GString::operator[](unsigned int j)
- {
- return (this->_str[j]);
- }
- bool GString::operator==(const GString &s)
- {
- return (this->Compare(s));
- }
-
- bool GString::operator>(const GString &s)
- {
- for (unsigned int i = 0; i < this->_size + 1; ++i)
- {
- if (this->_str[i] > s._str[i])
- return (true);
- if (this->_str[i] < s._str[i])
- return (false);
- }
- return (false);
- }
- bool GString::operator<(const GString &s)
- {
- for (unsigned int i = 0; i < this->_size + 1; ++i)
- {
- if (this->_str[i] < s._str[i])
- return (true);
- if (this->_str[i] > s._str[i])
- return (false);
- }
- return (false);
- }
- GString &GString::operator=(const GString &s)
- {
- this->_size = s.Size();
- this->_str = new char[this->_size + 1];
- for (unsigned int i = 0; i < this->_size + 1; ++i)
- this->_str[i] = s._str[i];
- return (*this);
- }
- GString &GString::operator+=(const GString &s)
- {
- this->_size += s.Size();
- char *newStr = new char[this->_size + 1];
- unsigned int i = 0;
- for (; i < this->_size - s.Size(); ++i)
- newStr[i] = this->_str[i];
- for (unsigned int j = 0; j < s.Size() + 1; ++j, ++i)
- newStr[i] = s._str[j];
- this->_str = newStr;
- return (*this);
- }
- GString &GString::operator+(const GString &s)
- {
- this->_size += s.Size();
- char *newStr = new char[this->_size + 1];
- unsigned int i = 0;
- for (; i < this->_size - s.Size(); ++i)
- newStr[i] = this->_str[i];
- for (unsigned int j = 0; j < s.Size() + 1; ++j, ++i)
- newStr[i] = s._str[j];
- this->_str = newStr;
- return (*(new GString(newStr)));
- }
-
-
-
#ifndef __GSTRING_H__
# define __GSTRING_H__
#include <string>
#include <iostream>
#include "GChar.h"
#include "GException.h"
#if defined (WIN32) | defined (_WIN32) | defined (__WIN32) | defined (WIN64) | defined (__WIN64)
#include "atlbase.h"
#include "atlstr.h"
#include "comutil.h"
#endif
class GStringList;
class GString
{
public:
enum SplitOption
{
SKIP_EMPTY_PARTS = 0x001,
KEEP_EMPTY_PARTS = 0x002
};
enum TrimOption
{
TRIM_BLANK = 0x001,
TRIM_SPACE = 0x002,
TRIM_TAB = 0x003,
TRIM_ENTER = 0x004,
TRIM_NUMBER = 0x005,
TRIM_ALPHA = 0x006,
TRIM_ALPHANUM = 0x007
};
enum IndexOption
{
INDEX_ALL_CHAR = 0x001,
INDEX_THIS_CHAR = 0x002
};
enum CaseOption
{
CASE_SENSITIVE = 0x001,
CASE_INSENSITIVE = 0x002
};
// CONSTRUCTEURS
/* OK */ GString(void);
/* OK */ GString(const char *);
/* OK */ GString(const GString &);
/* OK */ GString(const GString *);
/* OK */ GString(bool test);
/* OK */ GString(const std::string &);
/* OK */ GString(const GChar &);
/* OK */ GString(const GChar *);
#if defined (WIN32) | defined (_WIN32) | defined (__WIN32) | defined (WIN) | defined (WIN64) | defined (__WIN64)
/* OK */ GString(const std::wstring &);
/* OK */ GString(const wchar_t *);
/* OK */ GString(const CString &);
#endif
/* OK */ GString(char);
/* OK */ GString(unsigned char);
/* OK */ GString(int);
/* OK */ GString(unsigned int);
/* OK */ GString(long);
/* OK */ GString(unsigned long);
/* OK */ GString(short);
/* OK */ GString(unsigned short);
/* OK */ GString(float , unsigned int = 6);
/* OK */ GString(double , unsigned int = 6);
/* OK */ ~GString(void);
// --== CONVERSIONS ==--
#if defined (WIN32) | defined (_WIN32) | defined (__WIN32) | defined (WIN) | defined (WIN64) | defined (__WIN64)
/* OK */ std::wstring &ToWString(void) const;
/* OK */ wchar_t *ToWChar_t(void) const;
/* OK */ CString &ToCString(void) const;
/* OK */ LPCSTR ToLPCSTR(void) const;
#endif
/* OK */ bool ToBool(void) const;
/* OK */ std::string &ToStdString(void) const;
/* OK */ char *ToChar(void) const;
// --== SURCHARGES ==--
/* OK */ bool operator==(const GString &);
/* OK */ GString &operator=(const GString &);
/* OK */ GString &operator+=(const GString &);
/* OK */ GString &operator+(const GString &);
/* OK */ char operator[](unsigned int);
/* OK */ bool operator>(const GString &);
/* OK */ bool operator<(const GString &);
/* OK */ friend std::ostream &operator<<(std::ostream &out, const GString &);
/* OK */ friend std::ostream &operator<<(std::ostream &out, const GString *);
// --== METHODES ==--
/* OK */ unsigned int Length(void) const;
/* OK */ unsigned int Size(void) const;
/* OK */ unsigned int Count(const GString &, CaseOption = GString::CASE_SENSITIVE);
/* OK */ int FirstIndexOf(char, IndexOption = GString::INDEX_ALL_CHAR);
/* OK */ int LastIndexOf(char, IndexOption = GString::INDEX_ALL_CHAR);
/* OK */ int FirstIndexOf(char, char, IndexOption = GString::INDEX_ALL_CHAR);
/* OK */ int LastIndexOf(char, char, IndexOption = GString::INDEX_ALL_CHAR);
/* OK */ int Find(const GString &, CaseOption = GString::CASE_SENSITIVE);
/* OK */ int Find(const GString &, unsigned int, CaseOption = GString::CASE_SENSITIVE);
/* OK */ bool IsNumeric(void);
/* OK */ bool Compare(const GString &);
/* OK */ bool IsAlpha(void);
/* OK */ bool EndWith(const GString &);
/* OK */ bool StartWith(const GString &);
/* OK */ bool IsEmpty(void) const;
/* OK */ GString &Fill(char) const;
/* OK */ GString &Fill(char, unsigned int) const;
/* OK */ GString &ToUpper(void) const;
/* OK */ GString &ToLower(void) const;
/* OK */ static GString &Join(GStringList &, const GString &, SplitOption = GString::SKIP_EMPTY_PARTS);
/* OK */ static GString &Join(GStringList &, SplitOption = GString::SKIP_EMPTY_PARTS);
/* OK */ static GString &Implode(GStringList &, const GString &, SplitOption = GString::SKIP_EMPTY_PARTS);
/* OK */ static GString &Implode(GStringList &, SplitOption = GString::SKIP_EMPTY_PARTS);
/* OK */ GString &Remove(unsigned int, unsigned int) const;
/* OK */ GString &Remove(const GString &, CaseOption = GString::CASE_SENSITIVE);
/* OK */ GString &Substr(unsigned int, unsigned int) const;
/* OK */ GString &RightJustified(unsigned int, char = ' ');
/* OK */ GString &LeftJustified(unsigned int, char = ' ');
/* OK */ GString &Insert(unsigned int, const GString &) const;
/* OK */ GString &Replace(const GString &, const GString &, CaseOption = GString::CASE_SENSITIVE);
/* OK */ GString &RightTrim(TrimOption c = GString::TRIM_BLANK);
/* OK */ GString &LeftTrim(TrimOption c = GString::TRIM_BLANK);
/* OK */ GString &Trim(TrimOption c = GString::TRIM_BLANK);
/* OK */ GStringList &Split(const GString &, SplitOption = GString::SKIP_EMPTY_PARTS , CaseOption = GString::CASE_SENSITIVE);
/* OK */ GStringList &Explode(const GString &, SplitOption = GString::SKIP_EMPTY_PARTS , CaseOption = GString::CASE_SENSITIVE);
/* OK */ void Clear(void);
/* OK */ void Truncate(unsigned int);
private:
char *_str;
unsigned int _size;
};
#endif
#include "GString.h"
#include "GStringList.h"
// CONSTRUCTEURS
GString::GString(void)
{
this->_str = new char [1];
this->_str[0] = 0;
this->_size = 0;
}
GString::GString(const char *str)
{
this->_size = 0;
while (str[this->_size])
this->_size++;
this->_str = new char[this->_size];
for (unsigned int i = 0; i < this->_size + 1; ++i)
this->_str[i] = str[i];
}
GString::GString(const GString &s)
{
this->_size = s._size;
this->_str = new char[this->_size + 1];
for (unsigned int i = 0; i < this->_size + 1; ++i)
this->_str[i] = s._str[i];
}
GString::GString(const GString *s)
{
this->_size = s->_size;
this->_str = new char[this->_size + 1];
for (unsigned int i = 0; i < this->_size + 1; ++i)
this->_str[i] = s->_str[i];
}
GString::GString(bool test)
{
if (test == true)
{
this->_str = new char[5];
this->_str[0] = 't';
this->_str[1] = 'r';
this->_str[2] = 'u';
this->_str[3] = 'e';
this->_str[4] = 0;
this->_size = 4;
}
else
{
this->_str = new char[6];
this->_str[0] = 'f';
this->_str[1] = 'a';
this->_str[2] = 'l';
this->_str[3] = 's';
this->_str[4] = 'e';
this->_str[5] = 0;
this->_size = 5;
}
}
GString::GString(const std::string &s)
{
this->_size = s.size();
this->_str = new char[this->_size + 1];
for (unsigned int i = 0; i < this->_size + 1; i++)
this->_str[i] = s[i];
}
GString::GString(const GChar &c)
{
this->_str = new char[2];
this->_str[0] = c.GetChar();
this->_str[1] = 0;
this->_size = 1;
}
GString::GString(const GChar *c)
{
this->_str = new char[2];
this->_str[0] = c->GetChar();
this->_str[1] = 0;
this->_size = 1;
}
GString::GString(char c)
{
this->_str = new char[2];
this->_str[0] = c;
this->_str[1] = 0;
this->_size = 1;
}
GString::GString(unsigned char c)
{
this->_str = new char[2];
this->_str[0] = c;
this->_str[1] = 0;
this->_size = 1;
}
#if defined (WIN32) | defined (_WIN32) | defined (__WIN32) | defined (WIN) | defined (WIN64) | defined (__WIN64)
GString::GString(const std::wstring &wstr)
{
this->_size = wstr.size();
this->_str = new char[this->_size + 1];
for (unsigned int i = 0; i < this->_size + 1; ++i)
this->_str[i] = (char)wstr[i];
}
GString::GString(const wchar_t *str)
{
unsigned int i = 0;
while(str[i])
i++;
this->_size = i;
this->_str = new char[this->_size + 1];
for (unsigned int i = 0; i < this->_size + 1; ++i)
this->_str[i] = (char)str[i];
}
GString::GString(const CString &s)
{
this->_size = s.GetLength();
this->_str = (char*)LPCTSTR(s);
}
#endif
GString::~GString(void)
{
}
GString::GString(float nbr, unsigned int precision)
{
if (precision > 9)
precision = 9;
float save = nbr;
unsigned int i = 0;
while ((int)save > 0)
{
save /= 10;
i++;
}
save = nbr;
this->_size = i + precision + 1;
this->_str = new char[this->_size + 1];
i = 0;
for (; (int)save > 0; ++i)
{
this->_str[this->_size - i - 1 - precision - 1] = ((int)save % 10) + '0';
save /= 10;
}
this->_str[i++] = '.';
save = nbr - (int)nbr;
for (unsigned int j = 0; j < precision; ++j, ++i)
{
save *= 10;
this->_str[i] = ((int)save % 10) + '0';
}
this->_str[i] = 0;
}
GString::GString(double nbr, unsigned int precision)
{
if (precision > 9)
precision = 9;
double save = nbr;
unsigned int i = 0;
while ((int)save > 0)
{
save /= 10;
i++;
}
save = nbr;
this->_size = i + precision + 1;
this->_str = new char[this->_size + 1];
i = 0;
for (; (int)save > 0; ++i)
{
this->_str[this->_size - i - 1 - precision - 1] = ((int)save % 10) + '0';
save /= 10;
}
this->_str[i++] = '.';
save = nbr - (int)nbr;
for (unsigned int j = 0; j < precision; ++j, ++i)
{
save *= 10;
this->_str[i] = ((int)save % 10) + '0';
}
this->_str[i] = 0;
}
GString::GString(int nbr)
{
int saveNbr = nbr;
bool isNeg = false;
this->_size = 0;
if (nbr < 0)
{
isNeg = true;
nbr *= -1;
saveNbr *= -1;
this->_size++;
}
while (nbr > 0)
{
nbr /= 10;
this->_size++;
}
this->_str = new char[this->_size];
if (isNeg == true)
this->_str[0] = '-';
for (unsigned int i = 0; i < this->_size && saveNbr > 0; ++i)
{
this->_str[this->_size - i - 1] = (saveNbr % 10) + '0';
saveNbr /= 10;
}
this->_str[this->_size] = 0;
}
GString::GString(unsigned int nbr)
{
unsigned int saveNbr = nbr;
this->_size = 0;
while (nbr > 0)
{
nbr /= 10;
this->_size++;
}
this->_str = new char[this->_size];
for (unsigned int i = 0; i < this->_size && saveNbr > 0; ++i)
{
this->_str[this->_size - i - 1] = (saveNbr % 10) + '0';
saveNbr /= 10;
}
this->_str[this->_size] = 0;
}
GString::GString(short nbr)
{
short saveNbr = nbr;
bool isNeg = false;
this->_size = 0;
if (nbr < 0)
{
isNeg = true;
nbr *= -1;
saveNbr *= -1;
this->_size++;
}
while (nbr > 0)
{
nbr /= 10;
this->_size++;
}
this->_str = new char[this->_size];
if (isNeg == true)
this->_str[0] = '-';
for (unsigned int i = 0; i < this->_size && saveNbr > 0; ++i)
{
this->_str[this->_size - i - 1] = (saveNbr % 10) + '0';
saveNbr /= 10;
}
this->_str[this->_size] = 0;
}
GString::GString(unsigned short nbr)
{
unsigned short saveNbr = nbr;
this->_size = 0;
while (nbr > 0)
{
nbr /= 10;
this->_size++;
}
this->_str = new char[this->_size];
for (unsigned int i = 0; i < this->_size && saveNbr > 0; ++i)
{
this->_str[this->_size - i - 1] = (saveNbr % 10) + '0';
saveNbr /= 10;
}
this->_str[this->_size] = 0;
}
GString::GString(long nbr)
{
long saveNbr = nbr;
bool isNeg = false;
this->_size = 0;
if (nbr < 0)
{
isNeg = true;
nbr *= -1;
saveNbr *= -1;
this->_size++;
}
while (nbr > 0)
{
nbr /= 10;
this->_size++;
}
this->_str = new char[this->_size];
if (isNeg == true)
this->_str[0] = '-';
for (unsigned int i = 0; i < this->_size && saveNbr > 0; ++i)
{
this->_str[this->_size - i - 1] = (saveNbr % 10) + '0';
saveNbr /= 10;
}
this->_str[this->_size] = 0;
}
GString::GString(unsigned long nbr)
{
unsigned long saveNbr = nbr;
this->_size = 0;
while (nbr > 0)
{
nbr /= 10;
this->_size++;
}
this->_str = new char[this->_size];
for (unsigned int i = 0; i < this->_size && saveNbr > 0; ++i)
{
this->_str[this->_size - i - 1] = (saveNbr % 10) + '0';
saveNbr /= 10;
}
this->_str[this->_size] = 0;
}
// METHODE
unsigned int GString::Length(void) const
{
return (this->_size);
}
unsigned int GString::Size(void) const
{
return (this->_size);
}
unsigned int GString::Count(const GString &find, CaseOption op)
{
int pos = 0;
int nbr = 0;
while (true)
{
pos = this->Find(find, pos, op);
if (pos == -1)
return (nbr);
nbr++;
pos += find.Size();
}
}
int GString::LastIndexOf(char c , IndexOption op)
{
if (op == GString::INDEX_ALL_CHAR)
{
for (int i = this->Length() - 1; i > 0; --i)
{
if (this->_str[i] == c)
return (i);
}
}
else if (op == GString::INDEX_THIS_CHAR)
{
for (int i = this->Length() - 1; i > 0; --i)
{
if (this->_str[i] != c)
return (i - 1);
}
}
return (-1);
}
int GString::LastIndexOf(char b , char e, IndexOption op)
{
for (unsigned int i = this->Length() - 1; i > 0; ++i)
{
if (op == GString::INDEX_ALL_CHAR)
{
if (this->_str[i] >= b && this->_str[i] <= e)
return (i);
}
else if (op == GString::INDEX_THIS_CHAR)
{
if (this->_str[i] < b || this->_str[i] > e)
return (i - 1);
}
}
return (-1);
}
int GString::FirstIndexOf(char c, IndexOption op)
{
if (op == GString::INDEX_ALL_CHAR)
{
for (unsigned int i = 0; i < this->Length(); ++i)
{
if (this->_str[i] == c)
return (i);
}
}
else if (op == GString::INDEX_THIS_CHAR)
{
for (unsigned int i = 0; i < this->Length(); ++i)
{
if (this->_str[i] != c)
return (i);
}
}
return (-1);
}
int GString::FirstIndexOf(char c, char d, IndexOption op)
{
if (op == GString::INDEX_ALL_CHAR)
{
for (unsigned int i = 0; i < this->Length(); ++i)
{
if (this->_str[i] >= c && this->_str[i] <= d)
return (i);
}
}
else if (op == GString::INDEX_THIS_CHAR)
{
for (unsigned int i = 0; i < this->Length(); ++i)
{
if (this->_str[i] < c || this->_str[i] > d)
return (i);
}
}
return (-1);
}
int GString::Find(const GString &f, CaseOption c)
{
if (f.Size() > this->_size)
return (-1);
int k = -1;
for (unsigned int i = 0, j = 0; i < this->_size + 1; ++i)
{
if (f._str[j] == 0)
return (k);
GChar c1(this->_str[i]);
GChar c2(f._str[j]);
if (c == GString::CASE_INSENSITIVE)
{
c1 = c1.ToLower();
c2 = c2.ToLower();
}
if (c1 == c2)
{
if (k == -1)
k = i;
j++;
}
else
{
k = -1;
j = 0;
}
}
return (-1);
}
int GString::Find(const GString &f, unsigned int pos, CaseOption c)
{
if (f.Size() > this->_size)
return (-1);
int k = -1;
for (unsigned int i = pos, j = 0; i < this->_size + 1; ++i)
{
if (f._str[j] == 0)
return (k);
GChar c1(this->_str[i]);
GChar c2(f._str[j]);
if (c == GString::CASE_INSENSITIVE)
{
c1 = c1.ToLower();
c2 = c2.ToLower();
}
if (c1 == c2)
{
if (k == -1)
k = i;
j++;
}
else
{
k = -1;
j = 0;
}
}
return (-1);
}
void GString::Clear(void)
{
this->_str = new char[1];
this->_str[0] = 0;
this->_size = 0;
}
void GString::Truncate(unsigned int pos)
{
if (pos > this->_size)
return ;
for (unsigned int i = pos; i < this->_size; ++i)
this->_str[i] = 0;
}
bool GString::IsNumeric(void)
{
for (unsigned int i = 0; i < this->_size; ++i)
{
if (this->_str[i] < '0' || this->_str[i] > '9')
return (false);
}
return (true);
}
bool GString::IsAlpha(void)
{
for (unsigned int i = 0; i < this->_size; ++i)
{
if (this->_str[i] < 'a' || this->_str[i] > 'z')
return (false);
if (this->_str[i] < 'A' || this->_str[i] > 'Z')
return (false);
}
return (true);
}
bool GString::IsEmpty(void) const
{
if (this->_size)
return (false);
return (true);
}
bool GString::StartWith(const GString &str)
{
unsigned int i = 0;
if (str.Length() > this->Length())
return (false);
while (this->_str[i])
{
if (this->_str[i] != str._str[i])
return (false);
}
return (true);
}
bool GString::EndWith(const GString &str)
{
unsigned int i = 0;
unsigned int size1 = this->Length() - 1;
unsigned int size2 = str.Length() - 1;
if ( size2 > size1)
return (false);
while (this->_str[size1 - i])
{
if (this->_str[size1 - i] != str._str[size2 - i])
return (false);
}
return (true);
}
bool GString::Compare(const GString &s)
{
if (s.Size() != this->Size())
return (false);
for (unsigned int i = 0; i < this->Size(); ++i)
{
if (this->_str[i] != s._str[i])
return (false);
}
return (true);
}
GStringList &GString::Split(const GString &f, SplitOption splitop , CaseOption caseop)
{
GStringList *vect = new GStringList;
int pos = 0, pos1 = 0, pos2 = 0;
while (true)
{
pos1 = pos;
pos = this->Find(f, pos, caseop);
if (pos == -1)
break;
pos2 = pos;
GString ne = this->Substr(pos1, pos2);
if ((splitop == GString::SKIP_EMPTY_PARTS && ne.Size() != 0) || splitop == GString::KEEP_EMPTY_PARTS)
vect->PushBack(ne);
pos += f.Size();
}
if (pos1 != 0)
{
GString ne = this->Substr(pos1, this->Size());
if ((splitop == GString::SKIP_EMPTY_PARTS && ne.Size() != 0) || splitop == GString::KEEP_EMPTY_PARTS)
vect->PushBack(ne);
}
return (*vect);
}
GString &GString::Fill(char c) const
{
unsigned int size = this->Length();
char *newStr = new char[size + 1];
for (unsigned int i = 0; i < size; ++i)
newStr[i] = c;
newStr[size] = 0;
GString *G = new GString(newStr);
return (*G);
}
GString &GString::Fill(char c, unsigned int n) const
{
unsigned int size = this->Length();
char *newStr = new char[size + 1];
for (unsigned int i = 0; i < size; ++i)
{
if (i < n)
newStr[i] = c;
else
newStr[i] = this->_str[i];
}
newStr[this->_size] = 0;
GString *G = new GString(newStr);
return (*G);
}
GString &GString::ToUpper(void) const
{
char *newStr = new char[this->_size + 1];
for (unsigned int i = 0; i < this->_size; ++i)
{
if (this->_str[i] >= 'a' && this->_str[i] <= 'z')
newStr[i] = this->_str[i] - 32;
else
newStr[i] = this->_str[i];
}
newStr[this->_size] = 0;
GString *G = new GString(newStr);
return (*G);
}
GString &GString::ToLower(void) const
{
char *newStr = new char[this->_size + 1];
for (unsigned int i = 0; i < this->_size; ++i)
{
if (this->_str[i] >= 'A' && this->_str[i] <= 'Z')
newStr[i] = this->_str[i] + 32;
else
newStr[i] = this->_str[i];
}
newStr[this->_size] = 0;
GString *G = new GString(newStr);
return (*G);
}
GString &GString::Substr(unsigned int d, unsigned int f) const
{
if (f > this->_size)
{
GString *G = new GString(this->_str);
return (*G);
}
char *newStr = new char[f - d + 1];
for (unsigned int i = d, j = 0; i < f; ++i)
newStr[j++] = this->_str[i];
newStr[f - d] = 0;
GString *G = new GString(newStr);
return (*G);
}
GString &GString::Implode(GStringList &list, const GString &sep, SplitOption op)
{
GString *n = new GString();
for (unsigned int i = 0; i < list.Size(); ++i)
{
if (op == GString::SKIP_EMPTY_PARTS && list[i].Size() == 0)
continue;
if (i != 0)
*n += sep;
*n += list[i];
}
return (*n);
}
GString &GString::Join(GStringList &list, const GString &sep, SplitOption op)
{
return (GString::Implode(list, sep, op));
}
GString &GString::Implode(GStringList &list, SplitOption op)
{
GString *n = new GString();
for (unsigned int i = 0; i < list.Size(); ++i)
{
if (op == GString::SKIP_EMPTY_PARTS && list[i].Size() == 0)
continue;
*n += list[i];
}
return (*n);
}
GString &GString::Join(GStringList &list, SplitOption op)
{
return (GString::Implode(list, op));
}
GString &GString::Remove(unsigned int pos, unsigned int nb) const
{
if (pos + nb > this->_size)
{
GString *G = new GString(this->_str);
return (*G);
}
char *newStr = new char[this->_size - nb];
for (unsigned int i = 0, j = 0; i < this->_size; ++i)
{
if (i < pos || i >= pos + nb)
newStr[j++] = this->_str[i];
}
newStr[this->_size - nb] = 0;
GString *G = new GString(newStr);
return (*G);
}
GString &GString::Remove(const GString &g, CaseOption c)
{
int pos = this->Find(g, c);
if (pos == -1)
return (*(new GString(this->_str)));
return (this->Remove(pos, g.Size()));
}
GString &GString::RightJustified(unsigned int nbr, char c)
{
if (nbr <= this->_size)
return (*(new GString(this->_str)));
char *newStr = new char[nbr + 1];
for (unsigned int i = 0, j = 0; i < nbr; ++i)
{
if (i < nbr - this->_size)
newStr[i] = c;
else
newStr[i] = this->_str[j++];
}
newStr[nbr - this->_size + 1] = 0;
GString *G = new GString(newStr);
return (*G);
}
GString &GString::LeftJustified(unsigned int nbr, char c)
{
if (nbr <= this->_size)
return (*(new GString(this->_str)));
char *newStr = new char[nbr + 1];
for (unsigned int i = 0; i < nbr; ++i)
{
if (i < this->_size)
newStr[i] = this->_str[i];
else
newStr[i] = c;
}
newStr[nbr - this->_size + 1] = 0;
GString *G = new GString(newStr);
return (*G);
}
GString &GString::Insert(unsigned int pos, const GString &c) const
{
if (pos > this->_size)
return (*(new GString(this->_str)));
char *newStr = new char[this->_size + c.Size()];
for (unsigned int i = 0, j = 0; i < this->_size + 1; ++i)
{
if (pos == i)
{
for (; j < c.Size(); ++j)
newStr[i + j] = c._str[j];
newStr[i + j] = this->_str[i];
}
else
newStr[i + j] = this->_str[i];
}
GString *G = new GString(newStr);
return (*G);
}
GString &GString::Replace(const GString &f, const GString &r, CaseOption op)
{
GString *g = new GString(this->_str);
int pos = 1;
while (pos != -1)
{
pos = g->Find(f, op);
if (pos != -1)
{
*g = g->Remove(pos, f.Size());
*g = g->Insert(pos, r);
}
}
return (*g);
}
GString &GString::LeftTrim(TrimOption op)
{
int indexEnd = 0;
if (op == GString::TRIM_BLANK)
{
for (unsigned int i = 0; i < this->_size; ++i)
{
if (this->_str[i] != '\n' && this->_str[i] != '\t' && this->_str[i] != ' ')
{
indexEnd = i;
break;
}
}
}
else if (op == GString::TRIM_SPACE)
indexEnd = this->FirstIndexOf(' ', GString::INDEX_THIS_CHAR);
else if (op == GString::TRIM_TAB)
indexEnd = this->FirstIndexOf('\t', GString::INDEX_THIS_CHAR);
else if (op == GString::TRIM_ENTER)
indexEnd = this->FirstIndexOf('\n', GString::INDEX_THIS_CHAR);
else if (op == GString::TRIM_NUMBER)
indexEnd = this->FirstIndexOf('0', '9', GString::INDEX_THIS_CHAR);
else if (op == GString::TRIM_ALPHA)
{
for (unsigned int i = 0; i < this->_size; ++i)
{
if (this->_str[i] < 'A' || (this->_str[i] > 'Z' && this->_str[i] < 'a') || this->_str[i] > 'z')
{
indexEnd = i;
break;
}
}
}
else if (op == GString::TRIM_ALPHANUM)
{
for (unsigned int i = 0; i < this->_size; ++i)
{
if (this->_str[i] < '0' || (this->_str[i] > '9' && this->_str[i] < 'A') || (this->_str[i] > 'Z' && this->_str[i] < 'a') || this->_str[i] > 'z')
{
indexEnd = i;
break;
}
}
}
return (this->Substr(indexEnd, this->Size()));
}
GString &GString::RightTrim(TrimOption op)
{
int indexEnd = 0;
if (op == GString::TRIM_BLANK)
{
for (int i = this->_size - 1; i > 0; --i)
{
if (this->_str[i] != '\n' && this->_str[i] != '\t' && this->_str[i] != ' ')
{
indexEnd = i;
break;
}
}
}
else if (op == GString::TRIM_SPACE)
indexEnd = this->LastIndexOf(' ', GString::INDEX_THIS_CHAR);
else if (op == GString::TRIM_TAB)
indexEnd = this->LastIndexOf('\t', GString::INDEX_THIS_CHAR);
else if (op == GString::TRIM_ENTER)
indexEnd = this->LastIndexOf('\n', GString::INDEX_THIS_CHAR);
else if (op == GString::TRIM_NUMBER)
indexEnd = this->LastIndexOf('0', '9', GString::INDEX_THIS_CHAR);
else if (op == GString::TRIM_ALPHA)
{
for (int i = this->_size - 1; i > 0; --i)
{
if (this->_str[i] < 'A' || (this->_str[i] > 'Z' && this->_str[i] < 'a') || this->_str[i] > 'z')
{
indexEnd = i;
break;
}
}
}
else if (op == GString::TRIM_ALPHANUM)
{
for (int i = this->_size -1; i > 0; --i)
{
if (this->_str[i] < '0' || (this->_str[i] > '9' && this->_str[i] < 'A') || (this->_str[i] > 'Z' && this->_str[i] < 'a') || this->_str[i] > 'z')
{
indexEnd = i;
break;
}
}
}
return (this->Substr(0, indexEnd));
}
GString &GString::Trim(TrimOption op)
{
GString *g = new GString();
*g = this->RightTrim(op);
*g = g->LeftTrim(op);
return (*g);
}
// CONVERSION
#if defined (WIN32) | defined (_WIN32) | defined (__WIN32) | defined (WIN64) | defined (__WIN64)
std::wstring &GString::ToWString(void) const
{
unsigned int size = this->_size;
wchar_t *ws = new wchar_t[size + 1];
for (unsigned int i = 0; i < size + 1; i++)
ws[i] = this->_str[i];
std::wstring *s = new std::wstring(ws);
return (*s);
}
CString &GString::ToCString(void) const
{
CString *s = new CString(this->_str);
return (*s);
}
wchar_t *GString::ToWChar_t(void) const
{
unsigned int size = this->_size;
wchar_t *ws = new wchar_t[size + 1];
for (unsigned int i = 0; i < size + 1; i++)
ws[i] = this->_str[i];
return (ws);
}
LPCSTR GString::ToLPCSTR(void) const
{
LPCSTR test = this->_str;
return (test);
}
#endif
bool GString::ToBool(void) const
{
GString f("false");
if (f == GString(this->_str))
return (false);
GString t("true");
if (t == GString(this->_str))
return (true);
throw GException(GException::NOT_A_BOOLEEN);
}
std::string &GString::ToStdString(void) const
{
std::string *s = new std::string(this->_str);
return (*s);
}
char *GString::ToChar(void) const
{
char *newStr = new char[this->Size() + 1];
for (unsigned int i = 0; i < this->Size() + 1; ++i)
newStr[i] = this->_str[i];
return (newStr);
}
// SURCHARGES OPERATEURS
std::ostream &operator<<(std::ostream &out, const GString &s)
{
out << s._str;
return (out);
}
std::ostream &operator<<(std::ostream &out, const GString *s)
{
out << s->_str;
return (out);
}
char GString::operator[](unsigned int j)
{
return (this->_str[j]);
}
bool GString::operator==(const GString &s)
{
return (this->Compare(s));
}
bool GString::operator>(const GString &s)
{
for (unsigned int i = 0; i < this->_size + 1; ++i)
{
if (this->_str[i] > s._str[i])
return (true);
if (this->_str[i] < s._str[i])
return (false);
}
return (false);
}
bool GString::operator<(const GString &s)
{
for (unsigned int i = 0; i < this->_size + 1; ++i)
{
if (this->_str[i] < s._str[i])
return (true);
if (this->_str[i] > s._str[i])
return (false);
}
return (false);
}
GString &GString::operator=(const GString &s)
{
this->_size = s.Size();
this->_str = new char[this->_size + 1];
for (unsigned int i = 0; i < this->_size + 1; ++i)
this->_str[i] = s._str[i];
return (*this);
}
GString &GString::operator+=(const GString &s)
{
this->_size += s.Size();
char *newStr = new char[this->_size + 1];
unsigned int i = 0;
for (; i < this->_size - s.Size(); ++i)
newStr[i] = this->_str[i];
for (unsigned int j = 0; j < s.Size() + 1; ++j, ++i)
newStr[i] = s._str[j];
this->_str = newStr;
return (*this);
}
GString &GString::operator+(const GString &s)
{
this->_size += s.Size();
char *newStr = new char[this->_size + 1];
unsigned int i = 0;
for (; i < this->_size - s.Size(); ++i)
newStr[i] = this->_str[i];
for (unsigned int j = 0; j < s.Size() + 1; ++j, ++i)
newStr[i] = s._str[j];
this->_str = newStr;
return (*(new GString(newStr)));
}
Historique
- 29 juillet 2009 23:17:26 :
- Suppression des std::cout a l'intérieur du code
Sources de la même categorie
Commentaires et avis
Discussions en rapport avec ce code source dans le forum
classe [ par cyrinelahsini ]
Salut, j'ai défini deux classes : class tableau et class chaine. dans la class chaine, j'ai défini une fonction chtab qui convertitune chain
probleme d'une chaine de caracteres [ par bilaloch ]
Bonjour a tous,J'ai un ptit probleme au niveau d'une manipulation sur une chaine de caracteres. Voici le code : std::string *position = <FONT color=#
Chaine de caractere en C [ par YURIX ]
J'ai un probléme avec une chaine de caracterechar choix1;printf("Test des axes L,T et V : AX\n");printf("\nVotre ch
[.net c++] classe String [ par stgi02 ]
bonsoir ,j'ai utlisé le code suivant ;String * line;comment le contenu de line qui est une chaine de caractère va être intégré
[.net c++] classe String [ par stgi02 ]
bonjour je prog sur visual studio.net c++je voudrai savoir si il n'y a pas de problème si j'utilise String* line1 dans une fonction et un autre String
longueur tableau de chaune de caractere [ par Marco59190 ]
Salut tout le monde :D J'ai un petit problème... J'ai un tableau de chaine de caractère comme par exemple : char *mot10[] = {"ordinateur","multimedia"
Interaction entre objets [ par kharrat ]
Salut,Je cherche à implémenter une relation d'association 1-1 entre 2 objets de 2 classes différentes.Mon code:---------------------------------------
pb avec les string !!! [ par ElectricalMan ]
slt,j'ai qqs "erreurs" à la compilation ! c à s'arracher les cheveux, pouvez vous m'aider svp : #include <iostream.h> #include <fstream.h>
lire une chaine de caractere dans un fichier et la camparer aux autres chaines dans le meme fichier [ par leiloula ]
j'ai pa pu programmé un truc qui me permet de lire une chaine de caractere dans un fichier et la camparer aux autres chaines dans le meme fichier
aide pour un mini projet [ par foufi5 ]
salut dans le cadre de mes etudes je suis amené à réaliser une calculette simple avec + * / - et puissance. le probleme c'est que au de
|
Derniers Blogs
IMAGINE CUP 2012, MAKE A SIGN EN FINALEIMAGINE CUP 2012, MAKE A SIGN EN FINALE par junarnoalg
Voilà qui est fait, la nouvelle est officielle ! L'équipe belge "Make a Sign" va au pays des kangourous défendre son projet dans la catégorie Software Design. http://www.imaginecup.com/CompetitionsContent/Competition/WorldwideFinalists.aspx V...
Cliquez pour lire la suite de l'article par junarnoalg KINECT 1.5 IS OUT !KINECT 1.5 IS OUT ! par Vko
La version 1.5 du Kinect For Microsoft vient tout juste de sortir ! Plein de nouveautés: Tracking de squelette en Near Mode Détection en position assise Détection faciale avec un SDK dédié Documentation et des guideline (enfin) Un out...
Cliquez pour lire la suite de l'article par Vko LES ACTUALITéS DE LA SEMAINE SUR C2I.FR (14 MAI - 20 MAI) LES ACTUALITéS DE LA SEMAINE SUR C2I.FR (14 MAI - 20 MAI) par richardc
Mise à jour des Web API du 14 Mai
Réservez dès maintenant votre journée du 20 juin pour le Windows Azure Dev Camp 2012 à Paris
Mise à jour de Team Foundation Service
MechCommander 2 sur Windows 8
Entity Framework 5 Release Candidate e...
Cliquez pour lire la suite de l'article par richardc REACTIVE EXTENSIONS : CONSOMMER DES SERVICES AVEC RX PARTIE 3, LES PIèGES à éVITERREACTIVE EXTENSIONS : CONSOMMER DES SERVICES AVEC RX PARTIE 3, LES PIèGES à éVITER par Groc
Une mauvaise utilisation de rx lors de l'écriture d'une couche d'accès à des services peut conduire à des cas embarassants avec des erreurs mal gérées, des appels qui ne partent lorsqu'ils le devraient, et même des résultats incorrects . le tout nuis...
Cliquez pour lire la suite de l'article par Groc SHAREPOINT BLOG SITE, PROBLèME D'ARCHIVESSHAREPOINT BLOG SITE, PROBLèME D'ARCHIVES par junarnoalg
Dernièrement, nous avons migré le site
myTIC
vers un nouveau serveur SharePoint 2010. Dans les contenus que nous vouloins récupérer, nous avions un certain nombre de blogs.
Nous avons utilisé les commandes Power...
Cliquez pour lire la suite de l'article par junarnoalg
Forum
MATRICE TEMPLATEMATRICE TEMPLATE par hjr2610
Cliquez pour lire la suite par hjr2610 RE : SAC A DOS RE : SAC A DOS par hadjkaddour
Cliquez pour lire la suite par hadjkaddour
Logiciels
sDEVIS-FACTURES vlPRO (8.1.0.3)SDEVIS-FACTURES VLPRO (8.1.0.3)sDEVIS-FACTURES vlPRO a été mis au point pour les particuliers, créateurs, entrepreneurs, artisa... Cliquez pour télécharger sDEVIS-FACTURES vlPRO 974 Application Server (12.2.4.6)974 APPLICATION SERVER (12.2.4.6)Développez de puissantes applications dans un environnement de 'cloud computing', clusterisé, séc... Cliquez pour télécharger 974 Application Server vPicture (1.4.2.1)VPICTURE (1.4.2.1)Avec vPicture, hébergez vos images facilement et rapidement.
vPicture est un utilitaire simple, ... Cliquez pour télécharger vPicture Easy-Planning (2.2.1.6)EASY-PLANNING (2.2.1.6)Easy-Planning permet de créer des plannings sous la représentation de diagrammes et est adapté au... Cliquez pour télécharger Easy-Planning COM-BACKUP (2.0)COM-BACKUP (2.0)
COM-BACKUP est un logiciel de sauvegarde qui permet de planifier les sauvegardes de vos dossiers ...
Cliquez pour télécharger COM-BACKUP
|