473,664 Members | 3,035 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 6329
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.sam sung.comwrote in message
news:11******** *************@q 66g2000hsg.goog legroups.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.sam sung.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*********@gma il.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
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::r eplace() 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.c om (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::r eplace() 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_stri ng::replace have two
very different semantics: std::replace (or std::replace_if )
replaces according to the value; std::basic_stri ng::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*********@gma il.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
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
12160
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 = http://www.pdr.com\nhttps://extw3c.pdr.com/prism/b2b/schemas/TitleNotification.xsd;
1
2376
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 usuage allowes in instance doucments in case of substitution goups. I tried using abstarct complex type/element with substitution groups and I attached the code below. In XX1.xsd which is imported and a reference to complex abstract type is made...
1
5431
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 ds2 = &a;
4
6912
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 of a type whose name is stored in a constant. For example: Const FORM_NAME_1 as String = "frmThisForm" Const FORM_NAME_2 as String = "frmThatForm"
5
4414
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
12074
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 = "fred.abc"; $f =~ s/(.*)\.abc/$1.def/; print "$f\n";
2
1273
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 at the moment!) I find myself needing to restrict an element to contain either an integer in the range of 01 - 99 OR the value "*9". Because of the asterisk an simple integer field is not sufficient so I suspect I need to use a substitution...
11
2514
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 class object." This is the design in question: //////////////////////// Two abstract data types: class Document
0
1628
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 case is when the .xsd has an element that holds an array of substitutionGroup'ed items. Following is an example of a matching set of xsd, xml, xsd.exe command to generate C#, and the output C#. When running a unit-test on the C#, the...
0
8437
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8861
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8549
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8636
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7375
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4185
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2764
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 we have to send another system
2
2003
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1759
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.