[RESOLVED] Checking characters in Glib::ustring
Can anyone give my some code for iterating through a Glib::ustring and obtaining the characters one-by-one? I assumed Glib::ustring_Iterator would help me but no matter what I do with it I can't make anything that'll compile. It's either a bit more complex than I thought or I'm missing something blindingly simple. :(
Re: Checking characters in Glib::ustring
Hi John E
I'm not sure if this is what you're looking for but this compiles okay with GCC 4.4.1
Code:
#include <iostream>
#include <iterator>
#include <algorithm>
#include <glibmm.h>
int main()
{
Glib::ustring cow("cow");
std::copy(cow.begin(), cow.end(), std::ostream_iterator<gchar>(std::cout, " "));
}
I haven't used Glib, but a quick look at the documentation, I suppose you could do pretty much the same thing with the ustring as you would with std::string
Re: Checking characters in Glib::ustring
Thanks for the suggestion. Yes, it does work if I copy the string to a buffer but that kinda defeats the object of using the template library. What I don't understand is why I can't do this:-
Code:
Glib::ustring cow("cow");
Glib::ustring_Iterator<gunichar> iter = cow.begin();
Obviously I've misunderstood the syntax because the above code doesn't compile. The problem is that I've tried numerous variants on the same kind of theme but I can't get anything along those lines to compile. Unfortunately, I can't find any examples anywhere either.... :cry:
Re: Checking characters in Glib::ustring
Code:
Glib::ustring cow("cow");
Glib::ustring_Iterator<gunichar> iter = cow.begin();
Why do you need to state the template argument?
Code:
Glib::ustring cow("cow");
Glib::ustring_Iterator iter = cow.begin();
I would have expected this to be the correct way to do this.
Regards,
Paul McKenzie
Re: Checking characters in Glib::ustring
I see 2 overloads for ustring_Iterator
Code:
inline ustring_Iterator();
inline ustring_Iterator(const ustring_Iterator<std::string::iterator>& other);
where gunichar is unsigned int and is the ustring::value_type
so i think the code should be
Code:
Glib::ustring_Iterator<std::string::const_iterator> iter = cow.begin();
Edit:
May I ask, why you're not using ustring::iterator?
Edit:
Wait, I posted the wrong code
Code:
typedef ustring_Iterator<std::string::iterator> iterator;
typedef ustring_Iterator<std::string::const_iterator> const_iterator;
as you can see ustring_Iterator takes string's iterators,
so I suppose passing gunichar to string::iterator which expects a single byte char type won't work.
Re: Checking characters in Glib::ustring
Quote:
Originally Posted by
Paul McKenzie
Why do you need to state the template argument?
Code:
Glib::ustring cow("cow");
Glib::ustring_Iterator iter = cow.begin();
I would have expected this to be the correct way to do this.
Thanks Paul but that's one of the variations I tried. However, it gave me this compiler error:-
Quote:
error C2955: 'Glib::ustring_Iterator' : use of class template requires template argument list
:cry:
Quote:
Originally Posted by
potatoCode
Edit:
May I ask, why you're not using ustring::iterator?
Oops.... :blush: When I found Glib::ustring_Iterator it didn't occur to me to look for anything else but Glib::ustring::iterator is indeed the solution to my problem. I had a feeling that I must have missed something obvious! :o
Re: Checking characters in Glib::ustring
Quote:
I had a feeling that I must have missed something obvious!
John E, I might still be a newbie here, but I know exactly how that feels :wave:
Re: Checking characters in Glib::ustring
Quote:
Originally Posted by
John E
Oops.... :blush: When I found Glib::ustring_Iterator it didn't occur to me to look for anything else but Glib::ustring::iterator is indeed the solution to my problem. I had a feeling that I must have missed something obvious! :o
Now I see why it seemed strange that the iterator didn't automatically know the type it's iterating over without specifying the template argument.
The actual type is ustring::iterator. When I first looked at it, this is what I thought a ustring_iterator meant, but obviously, they are two different things.
Regards,
Paul McKenzie
Re: Checking characters in Glib::ustring
Yes, I suspect the fact that "Iterator" uses a capital 'I' (Glib::ustring_Iterator) was possibly intended to give some kind of clue that it isn't the same thing as Glib::ustring::iterator but I must admit, that subtlety was lost on me..! :D
Re: Checking characters in Glib::ustring
Sorry to resurrect an ancient thread but this is vaguely connected....
Code:
Glib::ustring infin = _("\u221e"); // Should display an infinity sign
The above code displays an infinity sign when compiled with gcc but for me with MSVC it seems to produce a question mark. I've tried VS2019 and also a much earlier version of MSVC (VC8).
And yet according to my Windows Character Map, the hex character 221e is indeed an infinity sign.
Just wondering if anyone else could try it? The question mark is visible, even in the Debugger window.
[Edit...] I've a feeling that locale_from_utf8() should be helping here - e.g.
Code:
Glib::ustring infin = Glib::locale_from_utf8 ("\u221e");
or maybe this....
Code:
std::string infin = Glib::locale_from_utf8 ("\u221e");
but they both still give me a question mark for some reason... What am I missing :confused:
Re: Checking characters in Glib::ustring
Quote:
Originally Posted by
John E
or maybe this....
Code:
std::string infin = Glib::locale_from_utf8 ("\u221e");
but they both still give me a question mark for some reason... What am I missing :confused:
Something just occurred to me... Glib::locale_from_utf8() returns std::string - so maybe std::string only supports simple strings - i.e. strings which can be represented by single characters?
And maybe the infinity sign can't be represented by a single char?
Re: Checking characters in Glib::ustring
Is it possible to convert it to std::wstring?
Re: Checking characters in Glib::ustring
Thanks Victor - this won't compile (gives me error C2440: 'initializing': cannot convert from 'Glib::ustring' to 'std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t>>')
Code:
Glib::ustring infin = _("\u221e");
std::wstring strTest = infin;
but interestingly, this initialises correctly:-
Code:
std::wstring strTest = L"\u221e";
So my next thought was to try this - except that it also gives C2440
Code:
std::wstring strTest = L"\u221e";
Glib::ustring infin = strTest; // <--- C2440
Re: Checking characters in Glib::ustring
Hi John,
I guess there should exist some functions in Glib to convert from UTF8 to UNICODE and vice versa. However, I never used this library and I don't have any documentation either.
Re: Checking characters in Glib::ustring
Good suggestion Victor. I found a function called g_utf16_to_utf8()
Code:
std::wstring infin = L"\u221e"; // Initialises to an infinity sign
gchar* winCompatibleInfin = g_utf16_to_utf8 (reinterpret_cast<gunichar2 const*>(infin.c_str()), -1, NULL, NULL, NULL);
winCompatibleInfin looks a bit weird when viewed in the debugger - but in the finished app it does print out an infinity sign!!