Page 1 of 1

Whats next to convert function

Posted: Thu Aug 16, 2007 10:43 pm
by kommven
I'm looking for a parallel function similar to convert.

My requirement is to change a matching input string (if found) to a output string defined.

convert does pretty much the same but for a single character and not for a string.

Any ideas or solutions are appreciated.

K-

Posted: Thu Aug 16, 2007 11:23 pm
by ray.wurlod
Search the forum. DSGuru2B published a parallel function that mimics the Ereplace() function from server jobs.

Posted: Thu Aug 16, 2007 11:25 pm
by jhmckeever
... you'll find it <a href="viewtopic.php?t=106358">here</a>

Posted: Fri Aug 17, 2007 6:46 am
by iDomz
jhmckeever wrote:... you'll find it <a href="viewtopic.php?t=106358">here</a>
A slightly different version here.. I would be happy if somebody can guide me on how to free the return pointer. DSGuru2B's method will not work because free is called after return.

Code: Select all

#include <string>
using namespace std;

char *pxEreplace(char *Inp, char *Sub, char *Rep, int Occur, int Pos) 
{ 
	string inpString(Inp);
	string subString(Sub);
	string repString(Rep);
	int iter, count = 0; 
	Pos = Pos<=1?1:Pos;
	Occur = Occur <= 0?inpString.length():Occur; 
	for(iter = inpString.find(subString, Pos); iter != string::npos; iter = inpString.find(subString, iter))
	{
		count++;
		inpString.replace(iter,subString.length(),repString);
		iter++;
		if(count == Occur ) break;
	}
	char * retvalue = new char[inpString.size()+1];
	strcpy(retvalue,inpString.c_str());
	inpString.~string();
	subString.~string();
	repString.~string();
	return retvalue;
}  
Cheers,
D

Posted: Fri Aug 17, 2007 11:00 am
by kommven
I will try this out and update.
Thanks,
K-

Posted: Fri Aug 17, 2007 2:21 pm
by DSguru2B
Nevermind the free after the return. You can take that out. Its presence has no meaning except that I had it there just for experimentation. The DSEngine will free the memory of the variable used in the return command.