Substitution in the C++ string class

  • Thread starter Generic Usenet Account
  • Start date
G

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?

Thanks,
Song
 
I

Ian Collins

Generic said:
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)?
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!
 
R

Robbie Hatley

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.
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.
 
R

Roland Pibinger

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().
 
J

James Kanze

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.
 
R

Roland Pibinger

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().
 
J

James Kanze

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top