2000-04-21 22:33:34 +02:00
|
|
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
|
|
|
|
<HTML>
|
|
|
|
<HEAD>
|
|
|
|
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
|
2000-07-11 23:45:08 +02:00
|
|
|
<META NAME="AUTHOR" CONTENT="pme@sources.redhat.com (Phil Edwards)">
|
configopts.html, [...]: Remove many EGCS references...
* docs/configopts.html, docs/install.html, docs/17_intro/BADNAMES,
docs/17_intro/howto.html, docs/18_support/howto.html,
docs/19_diagnostics/howto.html, docs/20_util/howto.html,
docs/21_strings/howto.html, docs/22_locale/howto.html,
docs/23_containers/howto.html, docs/24_iterators/howto.html,
docs/25_algorithms/howto.html, docs/26_numerics/howto.html,
docs/27_io/howto.html, docs/ext/howto.html, docs/faq/index.html:
Remove many EGCS references; use current absolute URLs on
gcc.gnu.org or sources.redhat.com for messages in list archives.
* docs/faq/index.txt: Regenerate.
From-SVN: r36988
2000-10-21 02:51:50 +02:00
|
|
|
<META NAME="KEYWORDS" CONTENT="HOWTO, libstdc++, GCC, g++, libg++, STL">
|
2000-04-21 22:33:34 +02:00
|
|
|
<META NAME="DESCRIPTION" CONTENT="HOWTO for the libstdc++ chapter 22.">
|
|
|
|
<META NAME="GENERATOR" CONTENT="vi and eight fingers">
|
|
|
|
<TITLE>libstdc++-v3 HOWTO: Chapter 22</TITLE>
|
|
|
|
<LINK REL=StyleSheet HREF="../lib3styles.css">
|
2000-12-10 05:04:56 +01:00
|
|
|
<!-- $Id: howto.html,v 1.8 2000/12/03 23:47:47 jsm28 Exp $ -->
|
2000-04-21 22:33:34 +02:00
|
|
|
</HEAD>
|
|
|
|
<BODY>
|
|
|
|
|
|
|
|
<H1 CLASS="centered"><A NAME="top">Chapter 22: Localization</A></H1>
|
|
|
|
|
2000-08-31 03:17:53 +02:00
|
|
|
<P>Chapter 22 deals with the C++ localization facilities.
|
2000-04-21 22:33:34 +02:00
|
|
|
</P>
|
|
|
|
|
|
|
|
|
|
|
|
<!-- ####################################################### -->
|
|
|
|
<HR>
|
|
|
|
<H1>Contents</H1>
|
|
|
|
<UL>
|
2000-08-31 03:17:53 +02:00
|
|
|
<LI><A HREF="#1">Bjarne Stroustrup on Locales</A>
|
|
|
|
<LI><A HREF="#2">Nathan Myers on Locales</A>
|
2000-10-04 08:44:25 +02:00
|
|
|
<LI><A HREF="#3">class locale</A>
|
|
|
|
<LI><A HREF="#4">class codecvt</A>
|
|
|
|
<LI><A HREF="#5">class ctype</A>
|
|
|
|
<LI><A HREF="#6">Correct Transformations</A>
|
2000-04-21 22:33:34 +02:00
|
|
|
</UL>
|
|
|
|
|
|
|
|
<HR>
|
|
|
|
|
|
|
|
<!-- ####################################################### -->
|
|
|
|
|
|
|
|
<H2><A NAME="1">Stroustrup on Locales</A></H2>
|
|
|
|
<P>Dr. Bjarne Stroustrup has released a
|
|
|
|
<A HREF="http://www.research.att.com/~bs/3rd_loc0.html">pointer</A>
|
|
|
|
to Appendix D of his book,
|
|
|
|
<A HREF="http://www.research.att.com/~bs/3rd.html">The C++
|
|
|
|
Programming Language (3rd Edition)</A>. It is a detailed
|
|
|
|
description of locales and how to use them.
|
|
|
|
</P>
|
2000-09-19 23:44:30 +02:00
|
|
|
<P>He also writes:
|
|
|
|
<BLOCKQUOTE><EM>
|
|
|
|
Please note that I still consider this detailed description of
|
|
|
|
locales beyond the needs of most C++ programmers. It is written
|
|
|
|
with experienced programmers in mind and novices will do best to
|
|
|
|
avoid it.
|
|
|
|
</EM></BLOCKQUOTE>
|
|
|
|
</P>
|
2000-04-21 22:33:34 +02:00
|
|
|
<P>Return <A HREF="#top">to top of page</A> or
|
|
|
|
<A HREF="../faq/index.html">to the FAQ</A>.
|
|
|
|
</P>
|
|
|
|
|
|
|
|
<HR>
|
2000-08-31 03:17:53 +02:00
|
|
|
<H2><A NAME="2">Nathan Myers on Locales</A></H2>
|
|
|
|
<P> An article entitled "The Standard C++ Locale" was published in
|
|
|
|
Dr. Dobb's Journal and can be found
|
|
|
|
<A HREF="http://www.cantrip.org/locale.html">here</A>
|
|
|
|
</P>
|
|
|
|
<P>Return <A HREF="#top">to top of page</A> or
|
|
|
|
<A HREF="../faq/index.html">to the FAQ</A>.
|
|
|
|
</P>
|
|
|
|
|
|
|
|
<HR>
|
2000-10-04 08:44:25 +02:00
|
|
|
<H2><A NAME="5">class locale</A></H2>
|
|
|
|
<P> Notes made during the implementation of locales can be found
|
|
|
|
<A HREF="locale.html">here</A>.
|
|
|
|
</P>
|
|
|
|
<P>Return <A HREF="#top">to top of page</A> or
|
|
|
|
<A HREF="../faq/index.html">to the FAQ</A>.
|
|
|
|
</P>
|
|
|
|
|
|
|
|
<HR>
|
|
|
|
<H2><A NAME="4">class codecvt</A></H2>
|
2000-08-31 03:17:53 +02:00
|
|
|
<P> Notes made during the implementation of codecvt can be found
|
2000-08-25 10:52:56 +02:00
|
|
|
<A HREF="codecvt.html">here</A>.
|
2000-04-21 22:33:34 +02:00
|
|
|
</P>
|
2000-08-31 03:17:53 +02:00
|
|
|
|
|
|
|
<P> The following is the abstract from the implementation notes:
|
|
|
|
<BLOCKQUOTE>
|
|
|
|
The standard class codecvt attempts to address conversions
|
|
|
|
between different character encoding schemes. In particular, the
|
|
|
|
standard attempts to detail conversions between the
|
|
|
|
implementation-defined wide characters (hereafter referred to as
|
|
|
|
wchar_t) and the standard type char that is so beloved in classic
|
|
|
|
"C" (which can now be referred to as narrow characters.)
|
|
|
|
This document attempts to describe how the GNU libstdc++-v3
|
|
|
|
implementation deals with the conversion between wide and narrow
|
|
|
|
characters, and also presents a framework for dealing with the huge
|
|
|
|
number of other encodings that iconv can convert, including Unicode
|
|
|
|
and UTF8. Design issues and requirements are addressed, and examples
|
|
|
|
of correct usage for both the required specializations for wide and
|
|
|
|
narrow characters and the implementation-provided extended
|
|
|
|
functionality are given.
|
|
|
|
</BLOCKQUOTE>
|
|
|
|
|
2000-04-21 22:33:34 +02:00
|
|
|
<P>Return <A HREF="#top">to top of page</A> or
|
|
|
|
<A HREF="../faq/index.html">to the FAQ</A>.
|
|
|
|
</P>
|
|
|
|
|
2000-08-31 03:17:53 +02:00
|
|
|
<HR>
|
2000-10-04 08:44:25 +02:00
|
|
|
<H2><A NAME="5">class ctype</A></H2>
|
2000-08-31 03:17:53 +02:00
|
|
|
<P> Notes made during the implementation of ctype can be found
|
|
|
|
<A HREF="ctype.html">here</A>.
|
|
|
|
</P>
|
|
|
|
<P>Return <A HREF="#top">to top of page</A> or
|
|
|
|
<A HREF="../faq/index.html">to the FAQ</A>.
|
|
|
|
</P>
|
2000-04-21 22:33:34 +02:00
|
|
|
|
2000-09-19 23:44:30 +02:00
|
|
|
<HR>
|
2000-10-04 08:44:25 +02:00
|
|
|
<H2><A NAME="6">Correct Transformations</A></H2>
|
2000-09-19 23:44:30 +02:00
|
|
|
<!-- Jumping directly here from chapter 21. -->
|
|
|
|
<P>A very common question on newsgroups and mailing lists is, "How
|
|
|
|
do I do <foo> to a character string?" where <foo> is
|
|
|
|
a task such as changing all the letters to uppercase, to lowercase,
|
|
|
|
testing for digits, etc. A skilled and conscientious programmer
|
|
|
|
will follow the question with another, "And how do I make the
|
|
|
|
code portable?"
|
|
|
|
</P>
|
|
|
|
<P>(Poor innocent programmer, you have no idea the depths of trouble
|
|
|
|
you are getting yourself into. 'Twould be best for your sanity if
|
|
|
|
you dropped the whole idea and took up basket weaving instead. No?
|
|
|
|
Fine, you asked for it...)
|
|
|
|
</P>
|
|
|
|
<P>The task of changing the case of a letter or classifying a character
|
|
|
|
as numeric, graphical, etc, all depends on the cultural context of the
|
|
|
|
program at runtime. So, first you must take the portability question
|
|
|
|
into account. Once you have localized the program to a particular
|
|
|
|
natural language, only then can you perform the specific task.
|
|
|
|
Unfortunately, specializing a function for a human language is not
|
|
|
|
as simple as declaring
|
|
|
|
<TT> extern "Danish" int tolower (int); </TT>.
|
|
|
|
</P>
|
|
|
|
<P>The C++ code to do all this proceeds in the same way. First, a locale
|
|
|
|
is created. Then member functions of that locale are called to
|
|
|
|
perform minor tasks. Continuing the example from Chapter 21, we wish
|
|
|
|
to use the following convenience functions:
|
|
|
|
<PRE>
|
|
|
|
namespace std {
|
|
|
|
template <class charT>
|
|
|
|
charT
|
|
|
|
toupper (charT c, const locale& loc) const;
|
|
|
|
template <class charT>
|
|
|
|
charT
|
|
|
|
tolower (charT c, const locale& loc) const;
|
|
|
|
}</PRE>
|
|
|
|
This function extracts the appropriate "facet" from the
|
|
|
|
locale <EM>loc</EM> and calls the appropriate member function of that
|
|
|
|
facet, passing <EM>c</EM> as its argument. The resulting character
|
|
|
|
is returned.
|
|
|
|
</P>
|
|
|
|
<P>For the C/POSIX locale, the results are the same as calling the
|
|
|
|
classic C <TT>toupper/tolower</TT> function that was used in previous
|
|
|
|
examples. For other locales, the code should Do The Right Thing.
|
|
|
|
</P>
|
|
|
|
<P>Of course, these functions take a second argument, and the
|
|
|
|
transformation algorithm's operator argument can only take a single
|
|
|
|
parameter. So we write simple wrapper structs to handle that.
|
|
|
|
</P>
|
|
|
|
<P>The next-to-final version of the code started in Chapter 21 looks like:
|
|
|
|
<PRE>
|
|
|
|
#include <iterator> // for back_inserter
|
|
|
|
#include <locale>
|
|
|
|
#include <string>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <cctype> // old <ctype.h>
|
|
|
|
|
|
|
|
struct Toupper
|
|
|
|
{
|
|
|
|
Toupper (std::locale const& l) : loc(l) {;}
|
|
|
|
char operator() (char c) { return std::toupper(c,loc); }
|
|
|
|
private:
|
|
|
|
std::locale const& loc;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Tolower
|
|
|
|
{
|
|
|
|
Tolower (std::locale const& l) : loc(l) {;}
|
|
|
|
char operator() (char c) { return std::tolower(c,loc); }
|
|
|
|
private:
|
|
|
|
std::locale const& loc;
|
|
|
|
};
|
|
|
|
|
|
|
|
int main ()
|
|
|
|
{
|
|
|
|
std::string s ("Some Kind Of Initial Input Goes Here");
|
|
|
|
Toupper up ( std::locale("C") );
|
|
|
|
Tolower down ( std::locale("C") );
|
|
|
|
|
|
|
|
// Change everything into upper case
|
|
|
|
std::transform (s.begin(), s.end(), s.begin(),
|
|
|
|
up
|
|
|
|
);
|
|
|
|
|
|
|
|
// Change everything into lower case
|
|
|
|
std::transform (s.begin(), s.end(), s.begin(),
|
|
|
|
down
|
|
|
|
);
|
|
|
|
|
|
|
|
// Change everything back into upper case, but store the
|
|
|
|
// result in a different string
|
|
|
|
std::string capital_s;
|
|
|
|
std::transform (s.begin(), s.end(), std::back_inserter(capital_s),
|
|
|
|
up
|
|
|
|
);
|
|
|
|
}</PRE>
|
|
|
|
</P>
|
|
|
|
<P>The final version of the code uses <TT>bind2nd</TT> 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
|
|
|
|
handle it.
|
|
|
|
</P>
|
|
|
|
<P>Return <A HREF="#top">to top of page</A> or
|
|
|
|
<A HREF="../faq/index.html">to the FAQ</A>.
|
|
|
|
</P>
|
|
|
|
|
|
|
|
|
2000-04-21 22:33:34 +02:00
|
|
|
|
|
|
|
|
|
|
|
<!-- ####################################################### -->
|
|
|
|
|
|
|
|
<HR>
|
|
|
|
<P CLASS="fineprint"><EM>
|
|
|
|
Comments and suggestions are welcome, and may be sent to
|
2000-07-11 23:45:08 +02:00
|
|
|
<A HREF="mailto:pme@sources.redhat.com">Phil Edwards</A> or
|
configopts.html, [...]: Remove many EGCS references...
* docs/configopts.html, docs/install.html, docs/17_intro/BADNAMES,
docs/17_intro/howto.html, docs/18_support/howto.html,
docs/19_diagnostics/howto.html, docs/20_util/howto.html,
docs/21_strings/howto.html, docs/22_locale/howto.html,
docs/23_containers/howto.html, docs/24_iterators/howto.html,
docs/25_algorithms/howto.html, docs/26_numerics/howto.html,
docs/27_io/howto.html, docs/ext/howto.html, docs/faq/index.html:
Remove many EGCS references; use current absolute URLs on
gcc.gnu.org or sources.redhat.com for messages in list archives.
* docs/faq/index.txt: Regenerate.
From-SVN: r36988
2000-10-21 02:51:50 +02:00
|
|
|
<A HREF="mailto:gdr@gcc.gnu.org">Gabriel Dos Reis</A>.
|
2000-12-10 05:04:56 +01:00
|
|
|
<BR> $Id: howto.html,v 1.8 2000/12/03 23:47:47 jsm28 Exp $
|
2000-04-21 22:33:34 +02:00
|
|
|
</EM></P>
|
|
|
|
|
|
|
|
|
|
|
|
</BODY>
|
|
|
|
</HTML>
|