Discussion:
Substitution in the C++ string class
(too old to reply)
Generic Usenet Account
2007-06-09 01:04:33 UTC
Permalink
I was extremely surprised to learn that the extremely rich C++ string
API does not have even a single menthod devoted to string substitution
i.e. given a string, replace all instances of pattern-1 in the string
with pattern-2. There are API methods for finding and replacing, but
none on pattern substitution.

Although I have developed an implementation for this (posted to the
comp.sources.d newsgroup), does anyone have the background why it was
not deemed necessary to provide this functionality in the standard C+
+ string API in the first place?

Thanks,
Song
Generic Usenet Account
2007-06-09 01:05:58 UTC
Permalink
Post by Generic Usenet Account
Although I have developed an implementation for this (posted to the
comp.sources.d newsgroup),
Here it goes:


#ifndef _TRANSFORM_STRING_H_
#define _TRANSFORM_STRING_H_

#include <string>

using namespace std;

//////////////////////////////////////////////////////
//
// Functor that transforms a string by susbstituting
// one pattern with another.
//
// Returns true if the string is altered, false otherwise
//
//////////////////////////////////////////////////////

struct transform_string
{
bool operator()(string& str, const string& origPattern, const
string& newPattern)
{
bool status = false;

if(origPattern.empty())
return status;

if(origPattern == newPattern)
return status;

status = true;
if(str == origPattern)
{
str = newPattern;
return status;
}

string::size_type priIdx = 0, secIdx = 0;
while((priIdx=str.find(origPattern, secIdx)) != string::npos)
{
status = true;
str.replace(priIdx, origPattern.length(), newPattern);
secIdx = priIdx+newPattern.length();
}

if(!secIdx) status = false;

return status;
}
};

#endif //_TRANSFORM_STRING_H_
Ian Collins
2007-06-09 01:21:44 UTC
Permalink
Post by Generic Usenet Account
I was extremely surprised to learn that the extremely rich C++ string
API does not have even a single menthod devoted to string substitution
i.e. given a string, replace all instances of pattern-1 in the string
with pattern-2. There are API methods for finding and replacing, but
none on pattern substitution.
Have you looked at std::tr1::regex (from boost)?
Post by Generic Usenet Account
Although I have developed an implementation for this (posted to the
comp.sources.d newsgroup), does anyone have the background why it was
not deemed necessary to provide this functionality in the standard C+
+ string API in the first place?
There were many things not included in the standard library, so I guess
regular expressions were one of them!
--
Ian Collins.
Robbie Hatley
2007-06-09 09:17:38 UTC
Permalink
Post by Generic Usenet Account
I was extremely surprised to learn that the extremely rich C++ string
API does not have even a single menthod devoted to string substitution
i.e. given a string, replace all instances of pattern-1 in the string
with pattern-2. There are API methods for finding and replacing, but
none on pattern substitution.
I felt the same, and I also had to impliement "Substitute()", using
a non-std regex engine from djgpp. I've always thought this was one
of the biggest and most egregious ommissions from the std. lib.
Post by Generic Usenet Account
Although I have developed an implementation for this (posted to the
comp.sources.d newsgroup), does anyone have the background why it was
not deemed necessary to provide this functionality in the standard C+
+ string API in the first place?
(Shrugs.) Perhaps the std. committee felt it was a feature better left
to libraries other than the standard library.
--
Cheers,
Robbie Hatley
lone wolf aatt well dott com
triple-dubya dott Tustin Free Zone dott org
Roland Pibinger
2007-06-09 09:31:44 UTC
Permalink
Post by Generic Usenet Account
I was extremely surprised to learn that the extremely rich C++ string
API does not have even a single menthod devoted to string substitution
Although I have developed an implementation for this (posted to the
comp.sources.d newsgroup), does anyone have the background why it was
not deemed necessary to provide this functionality in the standard C+
+ string API in the first place?
Not many developers are really satisfied with the 'C++ string'. At
first sight your implementation is a little intricate, esp. 'status'.
IMO, it should also be changed so that at most one dynamic allocation
is performed within the function. Otherwise it may be inefficient for
longer strings when the new token is longer than the replaced.
Moreover, I don't see why you implement the function as operator().
--
Roland Pibinger
"The best software is simple, elegant, and full of drama" - Grady Booch
James Kanze
2007-06-09 19:51:47 UTC
Permalink
Post by Generic Usenet Account
I was extremely surprised to learn that the extremely rich C++ string
API does not have even a single menthod devoted to string substitution
i.e. given a string, replace all instances of pattern-1 in the string
with pattern-2. There are API methods for finding and replacing, but
none on pattern substitution.
Although I have developed an implementation for this (posted to the
comp.sources.d newsgroup), does anyone have the background why it was
not deemed necessary to provide this functionality in the standard C+
+ string API in the first place?
Probably because it doesn't belong there. (Of course, there are
some things that are there that don't belong there, like all of
the find functions, etc.)

For the most part, the philosophy behind the standard library is
that the containers contain, and define sequences, and that
there are separate algorithms which work on sequences. Thus, I
can use std::replace (from <algorithm>) on a string, but also on
a vector, a deque or a list. The same thing holds for regular
expressions, which have been added to the standard; I can do a
regular expression search and replace on a vector<int>, for
example, if that's what I need.

--
James Kanze (Gabi Software) email: ***@gmail.com
Conseils en informatique orient�e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S�mard, 78210 St.-Cyr-l'�cole, France, +33 (0)1 30 23 00 34
Roland Pibinger
2007-06-10 10:01:19 UTC
Permalink
Post by James Kanze
Thus, I
can use std::replace (from <algorithm>) on a string, but also on
a vector, a deque or a list.
OTOH, what basic_string::replace() does (or the desired replace_all())
cannot be done with std::replace().
--
Roland Pibinger
"The best software is simple, elegant, and full of drama" - Grady Booch
James Kanze
2007-06-10 11:23:23 UTC
Permalink
Post by Roland Pibinger
Post by James Kanze
Thus, I
can use std::replace (from <algorithm>) on a string, but also on
a vector, a deque or a list.
OTOH, what basic_string::replace() does (or the desired replace_all())
cannot be done with std::replace().
Yes. There are a very few fundamental operations on string
which are just for string. Replace and its derivatives (insert,
append, erase) are examples. But note that despite having the
same name, std::replace and std::basic_string::replace have two
very different semantics: std::replace (or std::replace_if)
replaces according to the value; std::basic_string::replace
replaces according to position.

In other containers, you don't have replace, but you do have
insert and erase; operations which modify the topology of the
container are generally members.

The original question concerned something somewhat more complex,
since it involved a replace operation changing topology, but
dependent on value, and not position. As such, it certainly
doesn't fit as a member (because of value), and can't be done
with the classical algorithms, because they generally don't
support changing topology. Thus, a totally new component,
regex.

--
James Kanze (Gabi Software) email: ***@gmail.com
Conseils en informatique orient�e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S�mard, 78210 St.-Cyr-l'�cole, France, +33 (0)1 30 23 00 34
Loading...