-
February 14th, 2010, 01:49 PM
#1
[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.
"A problem well stated is a problem half solved.” - Charles F. Kettering
-
February 14th, 2010, 07:36 PM
#2
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
-
February 15th, 2010, 12:14 AM
#3
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....
"A problem well stated is a problem half solved.” - Charles F. Kettering
-
February 15th, 2010, 12:27 AM
#4
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
-
February 15th, 2010, 12:31 AM
#5
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.
Last edited by potatoCode; February 15th, 2010 at 12:41 AM.
-
February 15th, 2010, 02:34 PM
#6
Re: Checking characters in Glib::ustring
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:-
error C2955: 'Glib::ustring_Iterator' : use of class template requires template argument list
Originally Posted by potatoCode
Edit:
May I ask, why you're not using ustring::iterator?
Oops.... 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!
"A problem well stated is a problem half solved.” - Charles F. Kettering
-
February 15th, 2010, 11:50 PM
#7
Re: Checking characters in Glib::ustring
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
Last edited by potatoCode; February 15th, 2010 at 11:58 PM.
Reason: added the word
-
February 16th, 2010, 06:05 AM
#8
Re: Checking characters in Glib::ustring
Originally Posted by John E
Oops.... 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!
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
-
February 16th, 2010, 07:24 AM
#9
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..!
"A problem well stated is a problem half solved.” - Charles F. Kettering
-
July 21st, 2021, 03:57 AM
#10
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
Last edited by John E; July 21st, 2021 at 12:18 PM.
"A problem well stated is a problem half solved.” - Charles F. Kettering
-
July 21st, 2021, 12:22 PM
#11
Re: Checking characters in Glib::ustring
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
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?
"A problem well stated is a problem half solved.” - Charles F. Kettering
-
July 21st, 2021, 01:01 PM
#12
Re: Checking characters in Glib::ustring
Is it possible to convert it to std::wstring?
Victor Nijegorodov
-
July 22nd, 2021, 02:54 AM
#13
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
"A problem well stated is a problem half solved.” - Charles F. Kettering
-
July 22nd, 2021, 03:20 AM
#14
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.
Victor Nijegorodov
-
July 22nd, 2021, 05:45 AM
#15
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!!
"A problem well stated is a problem half solved.” - Charles F. Kettering
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|