Remplacement au sein de chaines [évolution des bstring... ;) ]

Contenu du snippet

suite a plsr questions & conseils sur le forum

voici une source consacrer au remplacement de chaines au sein d'une autre

juste pour memoire : actuellement

namespace BIBLI_BC{
class BString
{
...
private:
bool remplacePremierAParB(const BString& a, const BString& b);
void majTaille();
...
unsigned int i_taille;
char*c_chaine;

};
};
(repris ultérieurement avec des m_ comme membre)

Source / Exemple :


void BString::supprimeOccurence(const BString& test)
{
/// --------------------------------------------------------------------------
/// ---------------- BString::supprimeOccurence(BString test) ----------------
/// --------------------------------------------------------------------------
/// -----  Objectif	: supprimer les occurences d'une sous-chaine ds la chaine stockée
/// -----  PreCond	: /
/// -----  PostCond	: /
/// -----  Etat		: 1			(-1<0<1<2)
/// --------------------------------------------------------------------------
/// -----  BString test	: valeur de test
/// --------------------------------------------------------------------------
/// -----  Var Muettes (cf.partie préc) (1)	: test
/// -----  Var In  (1)	: test
	remplaceAParB(test,"");
}

bool BString::remplacePremierAParB(const BString& a, const BString& b)
{
/// ---------------------------------------------------------------------------------------------
/// ---------------- BString::remplacePremierAParB(BString a ,BString b) -> bool ----------------
/// ---------------------------------------------------------------------------------------------
/// -----  Objectif	: remplacer la 1ere ocurence d'une chaîne par une autre dans celle de la classe
/// -----  PreCond	: /
/// -----  PostCond	: /
/// -----  Etat		: 1			(-1<0<1<2)
/// -----  MaJ 06/08/04 : accélération,cas mm taille....
/// -----  MaJ 18/08/04 : accélération : dbl ptr ,cas taille>.... & memmove !!! & memcpy
/// ---------------------------------------------------------------------------------------------
/// -----  BString a	: chaîne à remplacer
/// -----  BString b	: chaîne de remplacement
/// ---------------------------------------------------------------------------------------------
/// -----  retour (bool)	: cf.obj
/// ---------------------------------------------------------------------------------------------
/// -----  Var Utilisées de la classe (1)	: i_taille
/// -----  Var Muettes (cf.partie préc) (2)	: a ,b
/// -----  Var Internes à la fonction (3)	: i ,l ,plomp
/// -----  Var In  (3)	: a ,b ,i_taille
	unsigned int i,l=a.i_taille;
	if(l==0)
		PB1("BString::remplacePremierAParB\nval à remplacer vide");
	if(l>i_taille)
		return false;
	if(l==b.i_taille)
	{
		for(i=0;i<(i_taille-l+1);i++)
			if(c_chaine[i]==a.c_chaine[0])
			{
				if(l==1||a==deA(i,i+l-1))
				{
					//for(j=0;j<l;j++)c_chaine[i+j]=b.c_chaine[j];
					memcpy(&(c_chaine[i]),b.c_chaine,b.i_taille);//dest,src,taille
					return true;
				}
			}
		return false;
	}
	else
	{
		if(l>b.i_taille) // new 18/08/04
		{
			for(i=0;i<(i_taille-l+1);i++)
				if(c_chaine[i]==a.c_chaine[0])
				{
					//if(l==1||a.operator==(deA(i,i+l-1)))
					if(l==1||a==deA(i,i+l-1))
					{
						//for(j=0;j<b.i_taille;j++)c_chaine[i+j]=b.c_chaine[j];
						memcpy(&(c_chaine[i]),b.c_chaine,b.i_taille);//dest,src,taille
						memmove(&(c_chaine[i+b.i_taille]),&(c_chaine[i+l]),i_taille -i-l+1);//dest,src,taille
						majTaille();
						return true;
					}
				}
		}
			return false;
	}
	BString plomp;
	for(i=0;i<(i_taille-l+1);i++)
		if(c_chaine[i]==a.c_chaine[0])
		{
			plomp=deA(i,i+l-1);
			if(plomp==a)
			{
				BString plomp="";	//masquage !!!
				if(i>0)
					plomp=deA(0,i-1);
				plomp+=b;
				if(i<(i_taille-l))
					plomp+=depuis(i+l);	//-1
				(*this)=plomp;
				return true;
			}
		}
	return false;
}

bool BString::remplaceAParB(const BString a, const BString b, bool recursif)
{
/// -----------------------------------------------------------------------------------------------------
/// ---------------- BString::remplaceAParB(BString a ,BString b ,bool recursif) -> bool ----------------
/// -----------------------------------------------------------------------------------------------------
/// -----  Objectif	: remplacer les ocurences d'une chaîne par une autre dans celle de la classe
/// -----  PreCond	: A pas dans B
/// -----  PostCond	: /
/// -----  Etat		: 1			(-1<0<1<2)
/// -----  MaJ 18/12/03: accélération: non récusrsivité & var de recursivité
/// -----  MaJ 06/08/04 : accélération,cas mm taille....
/// -----  MaJ 18/08/04 : accélération : dbl ptr ,cas taille>.... & memmove !!! & memcpy
/// -----------------------------------------------------------------------------------------------------
/// -----  BString a	: chaîne à remplacer
/// -----  BString b	: chaîne de remplacement
/// -----  bool recursif(par défaut : 'false')	: effectuer recursivement la modif?
/// -----------------------------------------------------------------------------------------------------
/// -----  retour (bool)	: Si au - 1 cht a été effectué
/// -----------------------------------------------------------------------------------------------------
/// -----  Var Utilisées de la classe (1)	: i_taille
/// -----  Var Muettes (cf.partie préc) (3)	: a ,b ,recursif
/// -----  Var Internes à la fonction (4)	: i ,l ,plomp ,rep
/// -----  Var In  (4)	: a ,b ,i_taille ,recursif
	bool rep=false;
	unsigned int i,l=a.i_taille;
	if(l==0)
		PB1("BString::remplaceAParB\nval de test nulle");
	if(recursif)
		while(remplacePremierAParB(a,b))
			rep=true;
	else
	{
		if(l>i_taille)
			return false;
		if(l==b.i_taille)
		{
			for(i=0;i<(i_taille-l+1);i++)
				if(c_chaine[i]==a.c_chaine[0])
				{
					if(l==1 || a==deA(i,i+l-1))
					{
						//for(j=0;i<l;j++)c_chaine[i+j]=b.c_chaine[j];
						memcpy(&(c_chaine[i]),b.c_chaine,b.i_taille);//dest,src,taille
						rep=true;
						i+=l-1;
					}
				}
		}
		else
		if(l>b.i_taille) // new 18/08/04
		{
			/// nota : je C, bruNews, j'aurai pu utilisé des char*, C pareil, non? - B
			//rappel: i:ptr actu sur c_chaine
			//rappel: l=a.i_taille
			for(i=0;i<(i_taille-l+1);i++)
				if(c_chaine[i]==a.c_chaine[0])
				{
					if(l==1||a==deA(i,i+l-1))
					{
						//for(j=0;j<b.i_taille;j++)c_chaine[i+j]=b.c_chaine[j];
						memcpy(&(c_chaine[i]),b.c_chaine,b.i_taille);//dest,src,taille

						/// Nota : peut-etre serait-il plus judicieu de pas faire ce mem move a chaque fois...
						///+		cela ns ralentit il vraiement?
						memmove(&(c_chaine[i+b.i_taille]),&(c_chaine[i+l]),i_taille -i-l+1);//dest,src,taille
						rep=true;
						i+=b.i_taille-1;
					}
					
					
				}
			majTaille();
		}
		else
			for(i=0;i<(i_taille-l+1);i++)
				if(c_chaine[i]==a.c_chaine[0])
				{
					if(l==1 || a==deA(i,i+l-1))
					{
						BString plomp="";
						if(i>0)
							plomp=deA(0,i-1);
						plomp+=b;
						if(i<(i_taille-l))
							plomp+=depuis(i+l);	//-1
						(*this)=plomp;
						rep=true;
						i+=b.i_taille-1;	//new 04/01/04
					}
				}
	}
	return rep;
}

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.