Dear Rooters
I have written the following function to remove a substring:
TString RemoveSubString(const char *name, const char *substr)
{
TString outname = TString(name);
char *tmpname = new char[strlen(name) + 1];
char *delname = tmpname;
Int_t len = 0;
char *pos;
tmpname = strcpy(tmpname,name);
pos = strstr(tmpname,substr);
len = tmpname - pos;
if (len < 0) outname.Resize(TMath::Abs(len));
else outname = tmpname + strspn(tmpname,substr);
delete [] delname;
return outname;
}//RemoveSubString
When I test this function as macro.C I get the following
correct output:
root [0] .L macro.C
root [1] RemoveSubString(“My_Name_Delete”,“Delete”)
(class TString)"My_Name_"
root [2] RemoveSubString(“My_Name_Delete”,“DELETE”)
(class TString)"My_Name_Delete"
root [3]
Now I add the following debugging code to my function:
TString RemoveSubString(const char *name, const char *substr)
{
TString outname = TString(name);
char *tmpname = new char[strlen(name) + 1];
char *delname = tmpname;
Int_t len = 0;
char *pos;
tmpname = strcpy(tmpname,name);
pos = strstr(tmpname,substr);
len = tmpname - pos;
if (len < 0) outname.Resize(TMath::Abs(len));
else outname = tmpname + strspn(tmpname,substr);
cout << "tmpname= " << tmpname << endl;
cout << "pos= " << pos << endl;
cout << "len= " << len << endl;
delete [] delname;
return outname;
}//RemoveSubString
When I test this function as macro.C I get the following
strange and incorrect output:
root [3] .L macro.C
root [4] RemoveSubString(“My_Name_Delete”,“Delete”)
tmpname= My_Name_Delete
pos= Delete
len= -8
(class TString)"My_Name_"
root [5] RemoveSubString(“My_Name_Delete”,“DELETE”)
tmpname= My_Name_Delete
root [6] RemoveSubString(“My_Name_Delete”,“Delete”)
root [7] .q
pos=
Calling RemoveSubString() the first time gives still the
correct answer, but calling it again suppresses all output!
Does anybody know, why I get this strange behavior?
Is there a hidden error in function RemoveSubString()?
My system: root 4.00/03 from 26 March 2004 on MacOS X 10.3.1
Thank you in advance for your help.
Best regards and Happy Eastern
Christian