ucn_string.cc

Go to the documentation of this file.
00001 /*
00002    The lestes compiler suite
00003    Copyright (C) 2002, 2003, 2004, 2005 Miroslav Tichy
00004    Copyright (C) 2002, 2003, 2004, 2005 Petr Zika
00005    Copyright (C) 2002, 2003, 2004, 2005 Vojtech Hala
00006    Copyright (C) 2002, 2003, 2004, 2005 Jiri Kosina
00007    Copyright (C) 2002, 2003, 2004, 2005 Pavel Sanda
00008    Copyright (C) 2002, 2003, 2004, 2005 Jan Zouhar
00009    Copyright (C) 2002, 2003, 2004, 2005 Rudolf Thomas
00010 
00011    This program is free software; you can redistribute it and/or modify
00012    it under the terms of the GNU General Public License as published by
00013    the Free Software Foundation; version 2 of the License.
00014 
00015    This program is distributed in the hope that it will be useful,
00016    but WITHOUT ANY WARRANTY; without even the implied warranty of
00017    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018    GNU General Public License for more details.
00019 
00020    See the full text of the GNU General Public License version 2, and
00021    the limitations in the file doc/LICENSE.
00022 
00023    By accepting the license the licensee waives any and all claims
00024    against the copyright holder(s) related in whole or in part to the
00025    work, its use, and/or the inability to use it.
00026  
00027  */
00028 /*! \file
00029   \brief String of ucn characters.
00030 
00031   Declaration of standard string template specialized for ucn character type.
00032 */
00033 
00034 #include <lestes/std/ucn_string.hh>
00035 #include <ostream>
00036 #include <iomanip>
00037 #include <sstream>
00038 
00039 package(lestes);
00040 package(std);
00041 
00042 /*!
00043   Converts ucn string, escapes extended characters with backslash u or U.
00044   Any external characters are considered to have host encoding.
00045   \return Host string with representation of the ucn string.
00046 */
00047 lstring ucn_string::to_host_string(void) const
00048 {
00049         hchar c;
00050         ucn u;
00051         ulint x;
00052 
00053         ::std::stringbuf sb;
00054         ::std::ostream os(&sb);
00055         
00056         for (ucn_string::const_iterator it = begin(), last = end(); it != last; ++it) {
00057                 u = *it;
00058 
00059                 if (character::is_external(u)) {
00060                         c = static_cast<hchar>(character::extract_value(u));
00061                         os << c;
00062                 } else if (character::is_basic(u)) {
00063                         c = character::to_host(u);
00064                         os << c;
00065                 } else {
00066                         x = character::extract_value(u);
00067                         if (x <= 0xFFFF) {
00068                                 os << "\\u" << ::std::hex << ::std::setfill('0') << ::std::setw(4) << x;
00069                         } else {
00070                                 os << "\\U" << ::std::hex << ::std::setfill('0') << ::std::setw(8) << x;
00071                         }
00072                 }
00073         }
00074 
00075         return sb.str();
00076 }
00077 
00078 /*!
00079   Prints ucn string to stream, escapes extended characters with backslash u or U.
00080   Any external characters are considered to have host encoding.
00081   \param os  The stream to write to.
00082   \param us  The string to print.
00083   \todo pt remove
00084 */
00085 ::std::ostream& operator<<(::std::ostream &os, const ucn_string &us)
00086 {
00087         return os << us.to_host_string();
00088 }
00089 
00090 end_package(std);
00091 end_package(lestes);
00092 
00093 /* vim: set ft=lestes : */

Generated on Mon Feb 12 18:23:44 2007 for lestes by doxygen 1.5.1-20070107