473,507 Members | 2,388 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Substitution in the C++ string class

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

Jun 9 '07 #1
6 6318
Generic Usenet Account wrote:
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!

--
Ian Collins.
Jun 9 '07 #2

"Generic Usenet Account" <us****@sta.samsung.comwrote in message
news:11*********************@q66g2000hsg.googlegro ups.com...
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.

--
Cheers,
Robbie Hatley
lone wolf aatt well dott com
triple-dubya dott Tustin Free Zone dott org
Jun 9 '07 #3
On Fri, 08 Jun 2007 18:04:33 -0700, Generic Usenet Account wrote:
>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
Jun 9 '07 #4
On Jun 9, 3:04 am, Generic Usenet Account <use...@sta.samsung.com>
wrote:
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: ja*********@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

Jun 9 '07 #5
On Sat, 09 Jun 2007 19:51:47 -0000, James Kanze wrote:
>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
Jun 10 '07 #6
On Jun 10, 12:01 pm, rpbg...@yahoo.com (Roland Pibinger) wrote:
On Sat, 09 Jun 2007 19:51:47 -0000, James Kanze wrote:
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: ja*********@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

Jun 10 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
12139
by: AP | last post by:
Hi, I have a class that I wish to serialize to XML, part of which looks like this: public class TitleNotification { public string NoNameSpaceSchemaLocation =...
1
2368
by: kollareddy | last post by:
Hi all, I am new to xml/xsd world. I want to know the differences between complex type and element being abstract and if both can be declared so, in case of substitution goups.Also can xsi:type...
1
5413
by: Rodolfo | last post by:
Hello, there's another languages that can do a macro substitution, how can I do this in Csharp. This is an example of what I want to do Dataset ds = new Dataset; string a = "ds"; DataSet...
4
6898
by: Don | last post by:
I think "macro substitution" is the correct term for what I want to do, but, to be sure, here is a description of what I'd like to know is possible: I want to be able to create a create an object...
5
4400
by: Murali | last post by:
In Python, dictionaries can have any hashable value as a string. In particular I can say d = {} d = "Right" d = "Wrong" d = "test" In order to print "test" using % substitution I can say
4
12067
by: Ian | last post by:
Hi, Hopefully a simple question but my brain is hurting... I want to make a regex substitution, using search and replace patterns contained in variables. What I want to do is: $f =...
2
1261
by: Steve | last post by:
Hi, I'm currently teaching myself about XML schems at the same time as specifying the XML document for a project I've been given to write. (I'm new to the XML world, so progress is a little slow...
11
2500
by: mailforpr | last post by:
Is this design well-formed? It contradicts the LSP and Design by contract anyhow. LSP tells us that "In class hierarchies, it should be possible to treat a specialized object as if it were a base...
0
1611
by: greenstone | last post by:
Hi, I am trying to figure out how to get XSD.EXE (when generating C# classes from an .xsd) to add the XmlArrayItemAttribute Deserization Annotations to my C# generated class output: My specific...
0
7111
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7319
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
5042
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1542
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
760
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
412
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.