<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Codigo Manso &#187; strings</title>
	<atom:link href="http://www.codigomanso.com/en/tag/strings/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.codigomanso.com</link>
	<description>Programación, informática y tecnología</description>
	<lastBuildDate>Sun, 21 Aug 2011 10:54:29 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>The C++ class std::string sucks!</title>
		<link>http://www.codigomanso.com/en/2009/10/la-clase-stdstring-de-c-es-una-patrana/</link>
		<comments>http://www.codigomanso.com/en/2009/10/la-clase-stdstring-de-c-es-una-patrana/#comments</comments>
		<pubDate>Wed, 07 Oct 2009 08:15:24 +0000</pubDate>
		<dc:creator>Pau Sanchez</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[Programacion]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[std::string]]></category>
		<category><![CDATA[strings]]></category>
		<category><![CDATA[unicode]]></category>
		<category><![CDATA[utf-8]]></category>

		<guid isPermaLink="false">http://www.codigomanso.com/es/?p=699</guid>
		<description><![CDATA[Still today, after some time without programming in C++, I wake up at night with nightmares about string class.
If you have programmed in C++ you probably know what I&#8217;m talking about.It&#8217;s HORRIBLE. Probably, the designer (or designers) of the standard string class though something like:
&#8220;Oh My Good!! We are going to make the best string [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">Still today, after some time without programming in C++, I wake up at night with nightmares about string class.</p>
<p style="text-align: justify;">If you have programmed in C++ you probably know what I&#8217;m talking about.<strong>It&#8217;s HORRIBLE.</strong> Probably, the designer (or designers) of the standard string class though something like:</p>
<p style="text-align: justify; padding-left: 30px;"><strong>&#8220;Oh My Good!! We are going to make the best string class ever!! We are going to create a base class, then we will have iterators, and we will use low-level functions so everyone could do everything he wants using this core class.&#8221;</strong></p>
<p style="text-align: justify;">To me, this class has born because mental masturbation. Probably nowadays, some of the people who made possible this class, regret about having created it this way.</p>
<p style="text-align: justify;">Of course, this is my not-that-humble opinion.</p>
<p style="text-align: justify;">From my point of view, a standard class for strings should be able to do some <strong>useful</strong> operations <strong>easily</strong>. For god sake!! It&#8217;s a class which millions of programmers are going to use!!</p>
<p style="text-align: justify;">Just an example, <strong>replace</strong> method receives two iterators (for the text to be removed) and then a string (the new text to be inserted).</p>
<p style="text-align: justify;">OK, this sounds fine to me, BUT isn&#8217;t it the most common use for that function doing a &#8220;search-and-replace&#8221;? Isn&#8217;t it more confortable to write, and easy to read, something like <b>str.replace(&#8220;house&#8221;, &#8220;home&#8221;)</b> instead of initializing two iterators, and then do a <b>str.replace(it_start, it_end, &#8220;home&#8221;)</b></p>
<p style="text-align: justify;">And this is probably a bad example. Let&#8217;s find another example. How would you transform a string to uppercase in C++?</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>size_t i <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> str.<span style="color: #202020;">length</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   str<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> toupper <span style="color: #009900;">&#40;</span>str<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p style="text-align: justify;">¡¡Come on!! I could do that in C!</p>
<p style="text-align: justify;">After a couple of minutes I realized that the only reason for using C++ string class is not having to deal with memory allocation (important), having an easy way of appending another string (using <b>+=</b> or <b>append</b>), checking string length (<b>size</b>), and probably the find methods. Everything else is just not as useful as it could be.</p>
<p>That being said, a class with this basic functionality could be created in less than half an hour.</p>
<p style="text-align: justify;">Finally, the lack of support for unicode is  ****** (censured). Unicode should have implemented as the standard for C/C++ compilers long time ago, and should have been supported for C++ strings as well. This my friend, is another story&#8230; let&#8217;s have this discussion another time.</p>
<p style="text-align: justify;">Kind regards to all those people who made the string class possible!!&#8230; <strong>I tell you from my heart :p</strong></p>
<p style="text-align: justify;">For the people interested in a unicode compatible, and portable libraries for string handling, take a look at the following links:</p>
<ul>
<li><a href="http://site.icu-project.org/" target="_blank">International Components for Unicode (ICU)</a>: ICU is a mature, widely used set of C/C++ and Java libraries providing Unicode and Globalization support for software applications</li>
<li><a href="http://utfcpp.sourceforge.net/" target="_blank">UTF8-CPP</a>: UTF-8 with C++ in a Portable Way</li>
</ul>
<p style="text-align: justify;">PS: Finally, I want to apologize because my English is still not that good for converting my feelings into words. So if you understand spanish and want to read what my heart is really saying, please read the spanish version of this article <img src='http://www.codigomanso.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.codigomanso.com/en/2009/10/la-clase-stdstring-de-c-es-una-patrana/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>

