Gstring - gestion des chaines de caractères

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 / Exemple :


#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))); }

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.