Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ Core » no String::Replace() ?
Re: no String::Replace() ? [message #29215 is a reply to message #29210] Mon, 11 October 2010 10:11 Go to previous messageGo to previous message
dolik.rce is currently offline  dolik.rce
Messages: 1789
Registered: August 2008
Location: Czech Republic
Ultimate Contributor

luzr wrote on Sun, 10 October 2010 20:01

OK, based on public request, I started inspired with Koldo's code and here is what I have added to String:

template <class B>
void AString<B>::Replace(const tchar *find, int findlen, const tchar *replace, int replacelen)
{
	String r;
	int i = 0;
	const tchar *p = Begin();
	for(;;) {
		int j = Find(findlen, find, i);
		if(j < 0)
			break;
		r.Cat(p + i, j - i);
		r.Cat(replace, replacelen);
		i = j + findlen;
	}
	r.Cat(p + i, GetCount() - i);
	*this = r;
}


Please check the algo...

There are also

	void   Replace(const String& find, const String& replace) { Replace(~find, find.GetCount(), ~replace, replace.GetCount()); }
	void   Replace(const tchar *find, const tchar *replace)   { Replace(find, strlen(find), replace, strlen(replace)); }



Hi Mirek,
It fails to compile for me (Linux + GCC or Clang). It complains that Begin() and GetCount() are undefined names. It should be
template <class B>
void AString<B>::Replace(const tchar *find, int findlen, const tchar *replace, int replacelen)
{
	String r;
	int i = 0;
	const tchar *p = B::Begin(); // Added "B::"
	for(;;) {
		int j = Find(findlen, find, i);
		if(j < 0)
			break;
		r.Cat(p + i, j - i);
		r.Cat(replace, replacelen);
		i = j + findlen;
	}
	r.Cat(p + i, B::GetCount() - i); // Added "B::"
	*this = r;
}


Honza
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Compile errror in ctrllib/update.cpp
Next Topic: PROPOSAL: SerializeStore helper
Goto Forum:
  


Current Time: Sun Jun 16 14:02:30 CEST 2024

Total time taken to generate the page: 0.01653 seconds