From c558c60c115adb132ab9b4fb9662d23184c3272b Mon Sep 17 00:00:00 2001 From: pme Date: Mon, 17 Sep 2001 23:24:40 +0000 Subject: 2001-09-17 Phil Edwards * docs/html/configopts.html: HTML to XHTML change. Lowercase tags. * docs/html/documentation.html: Likewise. * docs/html/explanations.html: Likewise. * docs/html/install.html: Likewise. * docs/html/17_intro/howto.html: Likewise. * docs/html/18_support/howto.html: Likewise. * docs/html/19_diagnostics/howto.html: Likewise. * docs/html/20_util/howto.html: Likewise. * docs/html/21_strings/howto.html: Likewise. * docs/html/22_locale/codecvt.html: Likewise. * docs/html/22_locale/ctype.html: Likewise. * docs/html/22_locale/howto.html: Likewise. * docs/html/22_locale/locale.html: Likewise. * docs/html/22_locale/messages.html: Likewise. * docs/html/23_containers/howto.html: Likewise. * docs/html/24_iterators/howto.html: Likewise. * docs/html/25_algorithms/howto.html: Likewise. * docs/html/26_numerics/howto.html: Likewise. * docs/html/27_io/howto.html: Likewise. * docs/html/ext/howto.html: Likewise. * docs/html/faq/index.html: Likewise. * docs/html/faq/index.txt: Regenerated. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@45668 138bc75d-0d04-0410-961f-82ee72b054a4 --- libstdc++-v3/docs/html/21_strings/howto.html | 276 +++++++++++++-------------- 1 file changed, 138 insertions(+), 138 deletions(-) (limited to 'libstdc++-v3/docs/html/21_strings/howto.html') diff --git a/libstdc++-v3/docs/html/21_strings/howto.html b/libstdc++-v3/docs/html/21_strings/howto.html index 80254269ff9..bf47e406c41 100644 --- a/libstdc++-v3/docs/html/21_strings/howto.html +++ b/libstdc++-v3/docs/html/21_strings/howto.html @@ -1,60 +1,60 @@ - - - - - - - - libstdc++-v3 HOWTO: Chapter 21 - - - - + + + + + + + + libstdc++-v3 HOWTO: Chapter 21 + + + + -

Chapter 21: Strings

+

Chapter 21: Strings

-

Chapter 21 deals with the C++ strings library (a welcome relief). -

+

Chapter 21 deals with the C++ strings library (a welcome relief). +

-
-

Contents

- +
+

Contents

+ -
+
-

MFC's CString

-

A common lament seen in various newsgroups deals with the Standard +

MFC's CString

+

A common lament seen in various newsgroups deals with the Standard string class as opposed to the Microsoft Foundation Class called CString. Often programmers realize that a standard portable answer is better than a proprietary nonportable one, but in porting their application from a Win32 platform, they discover that they are relying on special functons offered by the CString class. -

-

Things are not as bad as they seem. In - this - message, Joe Buck points out a few very important things: -

+

+
+

The old libg++ library had a function called form(), which did much the same thing. But for a Standard solution, you should use the stringstream classes. These are the bridge between the iostream hierarchy and the string class, and they operate with regular @@ -80,8 +80,8 @@ return output_stream.str(); } -

-

A serious problem with CString is a design bug in its memory +

+

A serious problem with CString is a design bug in its memory allocation. Specifically, quoting from that same message:

    CString suffers from a common programming error that results in
@@ -105,40 +105,40 @@
    If you replace CString with string in the above function, the
    performance is O(n).
       
-

-

Joe Buck also pointed out some other things to keep in mind when +

+

Joe Buck also pointed out some other things to keep in mind when comparing CString and the Standard string class: -

+

+

Return to top of page or + to the FAQ. +

-
-

A case-insensitive string class

-

The well-known-and-if-it-isn't-well-known-it-ought-to-be - Guru of the Week +


+

A case-insensitive string class

+

The well-known-and-if-it-isn't-well-known-it-ought-to-be + Guru of the Week discussions held on Usenet covered this topic in January of 1998. Briefly, the challenge was, "write a 'ci_string' class which is identical to the standard 'string' class, but is @@ -154,57 +154,57 @@ // still case-preserving, of course assert( strcmp( s.c_str(), "AbCdE" ) == 0 ); assert( strcmp( s.c_str(), "abcde" ) != 0 ); -

+

-

The solution is surprisingly easy. The original answer pages +

The solution is surprisingly easy. The original answer pages on the GotW website were removed into cold storage, in preparation for - a - published book of GotW notes. Before being + a + published book of GotW notes. Before being put on the web, of course, it was posted on Usenet, and that - posting containing the answer is available - here. -

-

See? Told you it was easy!

-

Added June 2000: The May issue of C++ Report contains - a fascinating article by Matt Austern (yes, the Matt Austern) + posting containing the answer is available + here. +

+

See? Told you it was easy!

+

Added June 2000: The May issue of C++ Report contains + a fascinating article by Matt Austern (yes, the Matt Austern) on why case-insensitive comparisons are not as easy as they seem, - and why creating a class is the wrong way to go about it in + and why creating a class is the wrong way to go about it in production code. (The GotW answer mentions one of the principle difficulties; his article mentions more.) -

-

Basically, this is "easy" only if you ignore some things, +

+

Basically, this is "easy" only if you ignore some things, things which may be too important to your program to ignore. (I chose to ignore them when originally writing this entry, and am surprised that nobody ever called me on it...) The GotW question and answer remain useful instructional tools, however. -

-

Added September 2000: James Kanze provided a link to a - Unicode - Technical Report discussing case handling, which provides some +

+

Added September 2000: James Kanze provided a link to a + Unicode + Technical Report discussing case handling, which provides some very good information. -

-

Return to top of page or - to the FAQ. -

+

+

Return to top of page or + to the FAQ. +

-
-

Breaking a C++ string into tokens

-

The Standard C (and C++) function strtok() leaves a lot to +


+

Breaking a C++ string into tokens

+

The Standard C (and C++) function strtok() leaves a lot to be desired in terms of user-friendliness. It's unintuitive, it destroys the character string on which it operates, and it requires you to handle all the memory problems. But it does let the client code decide what to use to break the string into pieces; it allows you to choose the "whitespace," so to speak. -

-

A C++ implementation lets us keep the good things and fix those +

+

A C++ implementation lets us keep the good things and fix those annoyances. The implementation here is more intuitive (you only call it once, not in a loop with varying argument), it does not affect the original string at all, and all the memory allocation is handled for you. -

-

It's called stringtok, and it's a template function. It's given - in this file in a less-portable form than +

+

It's called stringtok, and it's a template function. It's given + in this file in a less-portable form than it could be, to keep this example simple (for example, see the comments on what kind of string it will accept). The author uses a more general (but less readable) form of it for parsing command @@ -223,39 +223,39 @@ :is: :a: :test: - with all the whitespace removed. The original s is still - available for use, ls will clean up after itself, and - ls.size() will return how many tokens there were. -

-

As always, there is a price paid here, in that stringtok is not + with all the whitespace removed. The original s is still + available for use, ls will clean up after itself, and + ls.size() will return how many tokens there were. +

+

As always, there is a price paid here, in that stringtok is not as fast as strtok. The other benefits usually outweight that, however. - Another version of stringtok is given - here, suggested by Chris King and tweaked by Petr Prikryl, + Another version of stringtok is given + here, suggested by Chris King and tweaked by Petr Prikryl, and this one uses the transformation functions mentioned below. If you are comfortable with reading the new function names, this version is recommended as an example. -

-

Added February 2001: Mark Wilden pointed out that the - standard std::getline() function can be used with standard - istringstreams to perform +

+

Added February 2001: Mark Wilden pointed out that the + standard std::getline() function can be used with standard + istringstreams to perform tokenizing as well. Build an istringstream from the input text, and then use std::getline with varying delimiters (the three-argument signature) to extract tokens into a string. -

-

Return to top of page or - to the FAQ. -

+

+

Return to top of page or + to the FAQ. +

-
-

Simple transformations

-

Here are Standard, simple, and portable ways to perform common - transformations on a string instance, such as "convert +


+

Simple transformations

+

Here are Standard, simple, and portable ways to perform common + transformations on a string instance, such as "convert to all upper case." The word transformations is especially apt, because the standard template function - transform<> is used. -

-

This code will go through some iterations (no pun). Here's the + transform<> is used. +

+

This code will go through some iterations (no pun). Here's the simplistic version usually seen on Usenet:

    #include <string>
@@ -277,31 +277,31 @@
    std::transform (s.begin(), s.end(), capital_s.begin(), tolower); 
Note that these calls all involve the global C locale through the use of the C functions - toupper/tolower. This is absolutely guaranteed to work -- - but only if the string contains only characters - from the basic source character set, and there are only + toupper/tolower. This is absolutely guaranteed to work -- + but only if the string contains only characters + from the basic source character set, and there are only 96 of those. Which means that not even all English text can be represented (certain British spellings, proper names, and so forth). So, if all your input forevermore consists of only those 96 characters (hahahahahaha), then you're done. -

-

At minimum, you can write short wrappers like +

+

At minimum, you can write short wrappers like

    char toLower (char c)
    {
       return tolower(static_cast<unsigned char>(c));
    }
-

-

The correct method is to use a facet for a particular locale +

+

The correct method is to use a facet for a particular locale and call its conversion functions. These are discussed more in Chapter 22; the specific part is - here, which shows the + here, which shows the final version of this code. (Thanks to James Kanze for assistance and suggestions on all of this.) -

-

Another common operation is trimming off excess whitespace. Much +

+

Another common operation is trimming off excess whitespace. Much like transformations, this task is trivial with the use of string's - find family. These examples are broken into multiple + find family. These examples are broken into multiple statements for readability:

    std::string  str (" \t blah blah blah    \n ");
@@ -313,26 +313,26 @@
    // trim trailing whitespace
    notwhite = str.find_last_not_of(" \t\n"); 
    str.erase(notwhite+1); 
- Obviously, the calls to find could be inserted directly - into the calls to erase, in case your compiler does not + Obviously, the calls to find could be inserted directly + into the calls to erase, in case your compiler does not optimize named temporaries out of existance. -

-

Return to top of page or - to the FAQ. -

+

+

Return to top of page or + to the FAQ. +

-
-

+


+

Comments and suggestions are welcome, and may be sent to -the mailing list. -
$Id: howto.html,v 1.2 2001/02/07 00:03:20 pme Exp $ -

+the mailing list. +
$Id: howto.html,v 1.3 2001/04/03 00:26:55 pme Exp $ +

- - + + -- cgit v1.2.3