blob: cbbb87c6acf3dbfcf38f1ff3ce5a22ed9d7dd338 [file] [log] [blame]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<title>Boost.Locale: wconversions.cpp</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
$(document).ready(initResizable);
</script>
</head>
<body>
<div id="top"><!-- do not remove this div! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr style="height: 56px;">
<td id="projectlogo"><img alt="Logo" src="boost-small.png"/></td>
<td style="padding-left: 0.5em;">
<div id="projectname">Boost.Locale
</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- Generated by Doxygen 1.7.6.1 -->
<div id="navrow1" class="tabs">
<ul class="tablist">
<li><a href="index.html"><span>Main&#160;Page</span></a></li>
<li><a href="modules.html"><span>Modules</span></a></li>
<li><a href="namespaces.html"><span>Namespaces</span></a></li>
<li><a href="annotated.html"><span>Classes</span></a></li>
<li><a href="files.html"><span>Files</span></a></li>
<li><a href="examples.html"><span>Examples</span></a></li>
</ul>
</div>
</div>
<div id="side-nav" class="ui-resizable side-nav-resizable">
<div id="nav-tree">
<div id="nav-tree-contents">
</div>
</div>
<div id="splitbar" style="-moz-user-select:none;"
class="ui-resizable-handle">
</div>
</div>
<script type="text/javascript">
initNavTree('wconversions_8cpp-example.html','');
</script>
<div id="doc-content">
<div class="header">
<div class="headertitle">
<div class="title">wconversions.cpp</div> </div>
</div><!--header-->
<div class="contents">
<p>Example of using various text conversion functions with wide strings.</p>
<div class="fragment"><pre class="fragment"><span class="comment">//</span>
<span class="comment">// Copyright (c) 2009-2011 Artyom Beilis (Tonkikh)</span>
<span class="comment">//</span>
<span class="comment">// Distributed under the Boost Software License, Version 1.0. (See</span>
<span class="comment">// accompanying file LICENSE_1_0.txt or copy at</span>
<span class="comment">// http://www.boost.org/LICENSE_1_0.txt)</span>
<span class="comment">//</span>
<span class="comment">//</span>
<span class="comment">// ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !</span>
<span class="comment">//</span>
<span class="comment">// BIG FAT WARNING FOR Microsoft Visual Studio Users</span>
<span class="comment">//</span>
<span class="comment">// YOU NEED TO CONVERT THIS SOURCE FILE ENCODING TO UTF-8 WITH BOM ENCODING.</span>
<span class="comment">//</span>
<span class="comment">// Unfortunately MSVC understands that the source code is encoded as</span>
<span class="comment">// UTF-8 only if you add useless BOM in the beginning.</span>
<span class="comment">//</span>
<span class="comment">// So, before you compile &quot;wide&quot; examples with MSVC, please convert them to text</span>
<span class="comment">// files with BOM. There are two very simple ways to do it:</span>
<span class="comment">//</span>
<span class="comment">// 1. Open file with Notepad and save it from there. It would convert </span>
<span class="comment">// it to file with BOM.</span>
<span class="comment">// 2. In Visual Studio go File-&gt;Advances Save Options... and select</span>
<span class="comment">// Unicode (UTF-8 with signature) Codepage 65001</span>
<span class="comment">//</span>
<span class="comment">// Note: once converted to UTF-8 with BOM, this source code would not</span>
<span class="comment">// compile with other compilers, because no-one uses BOM with UTF-8 today</span>
<span class="comment">// because it is absolutely meaningless in context of UTF-8.</span>
<span class="comment">//</span>
<span class="comment">// ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !</span>
<span class="comment">//</span>
<span class="preprocessor">#include &lt;boost/locale.hpp&gt;</span>
<span class="preprocessor">#include &lt;boost/algorithm/string/case_conv.hpp&gt;</span>
<span class="preprocessor">#include &lt;iostream&gt;</span>
<span class="preprocessor">#include &lt;ctime&gt;</span>
<span class="keywordtype">int</span> main()
{
<span class="keyword">using namespace </span>boost::locale;
<span class="keyword">using namespace </span>std;
<span class="comment">// Create system default locale</span>
<a name="_a0"></a><a class="code" href="classboost_1_1locale_1_1generator.html" title="the major class used for locale generation">generator</a> gen;
locale loc=gen(<span class="stringliteral">&quot;&quot;</span>);
locale::global(loc);
wcout.imbue(loc);
<span class="comment">// This is needed to prevent C library to</span>
<span class="comment">// convert strings to narrow </span>
<span class="comment">// instead of C++ on some platforms</span>
std::ios_base::sync_with_stdio(<span class="keyword">false</span>);
wcout&lt;&lt;L<span class="stringliteral">&quot;Correct case conversion can&#39;t be done by simple, character by character conversion&quot;</span>&lt;&lt;endl;
wcout&lt;&lt;L<span class="stringliteral">&quot;because case conversion is context sensitive and not 1-to-1 conversion&quot;</span>&lt;&lt;endl;
wcout&lt;&lt;L<span class="stringliteral">&quot;For example:&quot;</span>&lt;&lt;endl;
wcout&lt;&lt;L<span class="stringliteral">&quot; German grüßen correctly converted to &quot;</span>&lt;&lt;<a name="a1"></a><a class="code" href="group__convert.html#ga2ceae621801e8cf4f77c60d1e3047ae8">to_upper</a>(L<span class="stringliteral">&quot;grüßen&quot;</span>)&lt;&lt;L<span class="stringliteral">&quot;, instead of incorrect &quot;</span>
&lt;&lt;boost::to_upper_copy(std::wstring(L<span class="stringliteral">&quot;grüßen&quot;</span>))&lt;&lt;endl;
wcout&lt;&lt;L<span class="stringliteral">&quot; where ß is replaced with SS&quot;</span>&lt;&lt;endl;
wcout&lt;&lt;L<span class="stringliteral">&quot; Greek ὈΔΥΣΣΕΎΣ is correctly converted to &quot;</span>&lt;&lt;<a name="a2"></a><a class="code" href="group__convert.html#ga4a3eb15f42f5cbae7bdd00c9e9cac222">to_lower</a>(L<span class="stringliteral">&quot;ὈΔΥΣΣΕΎΣ&quot;</span>)&lt;&lt;L<span class="stringliteral">&quot;, instead of incorrect &quot;</span>
&lt;&lt;boost::to_lower_copy(std::wstring(L<span class="stringliteral">&quot;ὈΔΥΣΣΕΎΣ&quot;</span>))&lt;&lt;endl;
wcout&lt;&lt;L<span class="stringliteral">&quot; where Σ is converted to σ or to ς, according to position in the word&quot;</span>&lt;&lt;endl;
wcout&lt;&lt;L<span class="stringliteral">&quot;Such type of conversion just can&#39;t be done using std::toupper that work on character base, also std::toupper is &quot;</span>&lt;&lt;endl;
wcout&lt;&lt;L<span class="stringliteral">&quot;not fully applicable when working with variable character length like in UTF-8 or UTF-16 limiting the correct &quot;</span>&lt;&lt;endl;
wcout&lt;&lt;L<span class="stringliteral">&quot;behavoir to BMP or ASCII only&quot;</span>&lt;&lt;endl;
}
<span class="comment">// vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4</span>
<span class="comment">// boostinspect:noascii</span>
</pre></div> </div><!-- contents -->
</div><!-- contents -->
</div>
<div id="nav-path" class="navpath">
<ul>
<li class="footer">
&copy; Copyright 2009-2012 Artyom Beilis, Distributed under the <a href="http://www.boost.org/LICENSE_1_0.txt">Boost Software License</a>, Version 1.0.
</li>
</ul>
</div>
</body>
</html>