Re: help using Glib::ustring



On Thu, 2007-12-20 at 20:37 +0000, Chris Vine wrote:
> On Wed, 2007-12-19 at 18:54 -0500, Jamiil Abduqadir wrote:
> > I am porting my old code to Gtkmm, I have started with the most
> > fundamental of applications, a string manipulation class.
> > What I am trying to do is very simple, but the new paradigm has me a
> > bit confused, to convert a string to upper case here is my code 
> > 
> > const Glib::ustring& jme::strtools::toUpper( const Glib::ustring& s )
> > {
> >     //global variable
> >     tmp.clear();
> >     tmp = s;
> >     //remove non-printable characters
> >     tmp = this->trimIt( tmp ); 
> >     //Iterate through the string until the end of the is found 
> >     for ( Glib::ustring::iterator i = tmp.begin(); i != tmp.end();
> > ++i ) {
> >         *i = toupper( i ); <<=== the old version
> >     }
> >     return tmp; 
> > }
> > i = i.uppercase()  ?? did not work
> > any suggestions?
> 
> UTF-8 is a multibyte representation of unicode.  You cannot write to a
> Glib::ustring::interator because the size of the new character (in
> multibyte representation) may be different from the size of the existing
> one.  That is the reason behind the behaviour, but the way it is
> enforced is by having Glib::ustring_Iterator<T>::operator*() return a
> new object and not a reference.  In fact, it returns a gunichar object
> (a wide character) and not its internal representation (narrow
> characters).
> 
> See also
> http://www.gtkmm.org/docs/glibmm-2.4/docs/reference/html/classGlib_1_1ustring__Iterator.html#_details

Actually on rereading this I realise I that what didn't work was not:
  *i = toupper( i )

which won't work for the reason I have given, but
  i = i.uppercase()

The reason for the failure of the second expression is that 'i' is an
interator and not a Glib::ustring object.
*i = i->uppercase() would work but be completely bizarre as the function
returns what you say is a global object, so what's the point?

Chris




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]