Commit 088de0bc authored by Daniel Elstner's avatar Daniel Elstner
Browse files

:wstring_to_utf8): New function to convert an std::wstring to UTF-8. This

* src/stringutils.{cc,h} (Util::wstring_to_utf8): New function to
convert an std::wstring to UTF-8.  This is a temporary measure
to deal with the same locale issues as discussed in the comments
to bug #399216.
(Util::int_to_string): Use a wide character stream and convert
the result to UTF-8 with Util::wstring_to_utf8().
* src/statusline.cc (CounterBox::stringstream_): Change to
std::wostringstream, as above.
(CounterBox::number_to_string): Use Util::wstring_to_utf8().

svn path=/trunk/; revision=650
parent 81975891
2007-02-18 Daniel Elstner <daniel.kitta@gmail.com>
* src/stringutils.{cc,h} (Util::wstring_to_utf8): New function to
convert an std::wstring to UTF-8. This is a temporary measure
to deal with the same locale issues as discussed in the comments
to bug #399216.
(Util::int_to_string): Use a wide character stream and convert
the result to UTF-8 with Util::wstring_to_utf8().
* src/statusline.cc (CounterBox::stringstream_): Change to
std::wostringstream, as above.
(CounterBox::number_to_string): Use Util::wstring_to_utf8().
2007-02-06 Daniel Elstner <daniel.kitta@gmail.com>
* configure.ac (ACLOCAL_FLAGS): Declare variable as precious
......
......@@ -19,6 +19,7 @@
*/
#include "statusline.h"
#include "stringutils.h"
#include "translation.h"
#include <gmodule.h>
......@@ -68,7 +69,7 @@ private:
int digits_range_;
int widest_digit_;
int second_widest_digit_;
std::ostringstream stringstream_;
std::wostringstream stringstream_;
Glib::ustring number_to_string(int number);
......@@ -158,9 +159,9 @@ void CounterBox::set_count(int count)
Glib::ustring CounterBox::number_to_string(int number)
{
stringstream_.str(std::string());
stringstream_.str(std::wstring());
stringstream_ << number;
return Glib::locale_to_utf8(stringstream_.str());
return Util::wstring_to_utf8(stringstream_.str());
}
/*
......
......@@ -36,7 +36,7 @@
namespace
{
typedef std::pair<int,char> ModPos;
typedef std::pair<int, char> ModPos;
class ScopedTypeClass
{
......@@ -316,6 +316,56 @@ int parse_capture_index(std::string::const_iterator& p, std::string::const_itera
} // anonymous namespace
/*
* Convert the content of an std::wstring to UTF-8. Using wide strings is
* necessary when dealing with localized stream formatting, for the reasons
* outlined here: http://bugzilla.gnome.org/show_bug.cgi?id=399216
*
* Direct use of wide strings in regexxer is a temporary measure. Thus,
* this function should be removed once Glib::compose() and Glib::format()
* are available in glibmm.
*/
Glib::ustring Util::wstring_to_utf8(const std::wstring& str)
{
class ScopedCharArray
{
private:
char* ptr_;
ScopedCharArray(const ScopedCharArray&);
ScopedCharArray& operator=(const ScopedCharArray&);
public:
explicit ScopedCharArray(char* ptr) : ptr_ (ptr) {}
~ScopedCharArray() { g_free(ptr_); }
char* get() const { return ptr_; }
};
GError* error = 0;
#ifdef __STDC_ISO_10646__
// Avoid going through iconv if wchar_t always contains UCS-4.
glong n_bytes = 0;
const ScopedCharArray buf (g_ucs4_to_utf8(reinterpret_cast<const gunichar*>(str.data()),
str.size(), 0, &n_bytes, &error));
#else
gsize n_bytes = 0;
const ScopedCharArray buf (g_convert(reinterpret_cast<const char*>(str.data()),
str.size() * sizeof(std::wstring::value_type),
"UTF-8", "WCHAR_T", 0, &n_bytes, &error));
#endif /* !__STDC_ISO_10646__ */
if (G_UNLIKELY(error))
{
g_warning("%s", error->message);
g_error_free(error);
return Glib::ustring();
}
return Glib::ustring(buf.get(), buf.get() + n_bytes);
}
bool Util::validate_encoding(const std::string& encoding)
{
// GLib just ignores some characters that aren't used in encoding names,
......@@ -541,7 +591,7 @@ Glib::ustring Util::substitute_references(const Glib::ustring& substitution,
}
else if (*p == '$' && p + 1 != pend)
{
std::pair<int,int> bounds;
std::pair<int, int> bounds;
if (Glib::Ascii::isdigit(*++p) || (*p == '{' && std::find(p + 1, pend, '}') != pend))
{
......@@ -596,7 +646,7 @@ Glib::ustring Util::substitute_references(const Glib::ustring& substitution,
Glib::ustring Util::int_to_string(int number)
{
std::ostringstream output;
std::wostringstream output;
try // don't abort if the user-specified locale doesn't exist
{
......@@ -609,7 +659,7 @@ Glib::ustring Util::int_to_string(int number)
output << number;
return Glib::locale_to_utf8(output.str());
return Util::wstring_to_utf8(output.str());
}
Glib::ustring Util::filename_short_display_name(const std::string& filename)
......
......@@ -32,7 +32,9 @@ namespace Gdk { class Color; }
namespace Util
{
typedef std::vector< std::pair<int,int> > CaptureVector;
typedef std::vector< std::pair<int, int> > CaptureVector;
Glib::ustring wstring_to_utf8(const std::wstring& str);
bool validate_encoding(const std::string& encoding);
bool encodings_equal(const std::string& lhs, const std::string& rhs);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment