From dd768f5f255f86bb60893f823b0ffa21ae85890d Mon Sep 17 00:00:00 2001 From: Jonathan Wakely Date: Wed, 20 Nov 2002 01:12:02 +0000 Subject: [PATCH] howto.html: Fix example code... 2002-11-20 Jonathan Wakely * docs/html/21_strings/howto.html: Fix example code, cite Gaby's explanation of "" error with toupper/tolower. * docs/html/22_locale/howto.html: Be more consistent with example in 21_strings. From-SVN: r59284 --- libstdc++-v3/ChangeLog | 7 +++ libstdc++-v3/docs/html/21_strings/howto.html | 52 ++++++++++++++------ libstdc++-v3/docs/html/22_locale/howto.html | 22 +++++---- 3 files changed, 57 insertions(+), 24 deletions(-) diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index d86158a48e6..34338f7ae13 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,10 @@ +2002-11-20 Jonathan Wakely + + * docs/html/21_strings/howto.html: Fix example code, cite Gaby's + explanation of "" error with toupper/tolower. + * docs/html/22_locale/howto.html: Be more consistent with + example in 21_strings. + 2002-11-19 John Gustafsson * docs/html/20_util/howto.html: Fix typo. diff --git a/libstdc++-v3/docs/html/21_strings/howto.html b/libstdc++-v3/docs/html/21_strings/howto.html index cba9c3f21d4..d862467b2a9 100644 --- a/libstdc++-v3/docs/html/21_strings/howto.html +++ b/libstdc++-v3/docs/html/21_strings/howto.html @@ -278,19 +278,32 @@ #include <algorithm> #include <cctype> // old <ctype.h> - std::string s ("Some Kind Of Initial Input Goes Here"); + struct ToLower + { + char operator() (char c) const { return std::tolower(c); } + }; + + struct ToUpper + { + char operator() (char c) const { return std::toupper(c); } + }; - // Change everything into upper case - std::transform (s.begin(), s.end(), s.begin(), toupper); + int main() + { + std::string s ("Some Kind Of Initial Input Goes Here"); + + // Change everything into upper case + std::transform (s.begin(), s.end(), s.begin(), ToUpper()); - // Change everything into lower case - std::transform (s.begin(), s.end(), s.begin(), tolower); + // Change everything into lower case + std::transform (s.begin(), s.end(), s.begin(), ToLower()); - // Change everything back into upper case, but store the - // result in a different string - std::string capital_s; - capital_s.reserve(s.size()); - std::transform (s.begin(), s.end(), capital_s.begin(), tolower); + // Change everything back into upper case, but store the + // result in a different string + std::string capital_s; + capital_s.resize(s.size()); + std::transform (s.begin(), s.end(), capital_s.begin(), ToUpper()); + }

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 -- @@ -301,19 +314,28 @@ 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 +

Note that the + ToUpper and ToLower function objects + are needed because toupper and tolower + are overloaded names (declared in <cctype> and + <locale>) so the template-arguments for + transform<> cannot be deduced, as explained in + this + message. + At minimum, you can write short wrappers like

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

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 - final version of this code. (Thanks to James Kanze for assistance - and suggestions on all of this.) + Correct Transformations, + 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 like transformations, this task is trivial with the use of string's diff --git a/libstdc++-v3/docs/html/22_locale/howto.html b/libstdc++-v3/docs/html/22_locale/howto.html index bd2526ddb4c..d6a340c9e54 100644 --- a/libstdc++-v3/docs/html/22_locale/howto.html +++ b/libstdc++-v3/docs/html/22_locale/howto.html @@ -168,18 +168,18 @@ #include <algorithm> #include <cctype> // old <ctype.h> - struct Toupper + struct ToUpper { - Toupper(std::locale const& l) : loc(l) {;} - char operator() (char c) { return std::toupper(c,loc); } + ToUpper(std::locale const& l) : loc(l) {;} + char operator() (char c) const { return std::toupper(c,loc); } private: std::locale const& loc; }; - struct Tolower + struct ToLower { - Tolower(std::locale const& l) : loc(l) {;} - char operator() (char c) { return std::tolower(c,loc); } + ToLower(std::locale const& l) : loc(l) {;} + char operator() (char c) const { return std::tolower(c,loc); } private: std::locale const& loc; }; @@ -187,9 +187,9 @@ int main () { std::string s("Some Kind Of Initial Input Goes Here"); - std::locale loc_c("C"); - Toupper up(loc_c); - Tolower down(loc_c); + std::locale loc_c("C"); + ToUpper up(loc_c); + ToLower down(loc_c); // Change everything into upper case. std::transform(s.begin(), s.end(), s.begin(), up); @@ -202,6 +202,10 @@ std::string capital_s; std::transform(s.begin(), s.end(), std::back_inserter(capital_s), up); } +

The ToUpper and ToLower structs can be + generalized for other character types by making operator() + a member function template. +

The final version of the code uses bind2nd to eliminate the wrapper structs, but the resulting code is tricky. I have not shown it here because no compilers currently available to me will -- 2.30.2